Hibernate HQL:如何使用复杂的左连接获取 [英] Hibernate HQL: how to use a complex left join fetch
问题描述
我想在以下情况发生时在TASK表上添加一个左连接:
LEFT JOIN FETCH
PROMPT p on(t.id = p.task.id 和p.applicationName ('XXX'))
I want to add a left join on TASK table when the following condition occurs: LEFT JOIN FETCH PROMPT p on (t.id = p.task.id and p.applicationName in ('XXX'))
以下是我的hql查询:
Here is my hql query:
select
distinct t
from
TASK t
LEFT JOIN FETCH
SERVER ser
on t.id=ser.task_id
LEFT JOIN FETCH
APPLICATION app
on ser.id=app.server_id
LEFT JOIN FETCH
PROMPT p on (t.id = p.task.id and p.applicationName in ('XXX'))
where
t.id=ser.task.id
and ser.id=app.server
and app.name in ('XXX')
order by t.id
我得到以下异常,可能是由于on关键字:
I get the following exception, probably due to "on" keyword:
java.lang.NoSuchMethodError: org.hibernate.hql.antlr.HqlBaseParser.recover(Lantlr/RecognitionException;Lantlr/collections/impl/BitSet;)V
at org.hibernate.hql.antlr.HqlBaseParser.queryRule(HqlBaseParser.java:771)
有什么想法?
Any ideas?
class Task {
private String taskId;
private Set<ServerDetails> servers;
}
class ServerDetails {
private String id;
private Set<ApplicationDetails> applications;
}
class ApplicationDetails {
private String id;
}
Class Prompt {
private String applicationName;
}
如何使用左连接获取使用我的条件p.applicationName in('XXX')?
How can i use left join fetch using my condition p.applicationName in ('XXX')?
推荐答案
这里有几个问题:
- 你发布的HQL并不是真正的HQL :-)它直接与SQL相关。对于HQL,您需要使用映射关联来连接表。请参阅下面的改进版本。
- 据我所知,
ON
子句不支持括号中的多个连接条件。 / li>
-
NoSuchMethodError
不是Hibernate会抛出的东西:-)你可能有一个旧版本的antlr.jar
在你的类路径中的某个地方,它会被代替Hibernate期望的地方。找到它并将其删除。
- Your HQL as you've posted it is not really HQL :-) It's straight up SQL. For HQL you need use mapped associations to join tables. See below for an improved version.
- As far as I'm aware
ON
clause does not support multiple joined conditions in parenthesis. NoSuchMethodError
is not something Hibernate would throw :-) You likely have an older version ofantlr.jar
somewhere in your classpath and it gets picked up in place of the one expected by Hibernate. Find it and remove it.
如果没有看到您的映射,以下内容可能不准确,但我会试着写更多适当的HQL:
Without seeing your mappings the following is likely inaccurate, but I'll take a stab at writing the more appropriate HQL:
select distinct t
from TASK t
left join fetch t.server ser
left join fetch ser.application app
left join t.prompt p with p.applicationName in ('XXX')
order by t.id
请注意,提示符
未被取用,因为您无法使用连接取出
连同与
条件。如果需要关联,您可以用内部联合抓取
和
替换它。
Note that prompt
is not fetched because you can't use join fetch
together with with
condition. You can replace it with inner join fetch
and where
if the association is required.
如果您在解决classpath问题后仍然遇到问题,请随时发布映射,如果您需要实际的HQL帮助。
If you're still having trouble after the classpath issue is resolved, feel free to post your mappings if you need help with actual HQL.
这篇关于Hibernate HQL:如何使用复杂的左连接获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!