且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Hibernate HQL:如何使用复杂的左连接获取

更新时间:2023-02-06 21:47:35

这里有几个问题:


  1. 你发布的HQL并不是真正的HQL :-)它直接与SQL相关。对于HQL,您需要使用映射关联来连接表。请参阅下面的改进版本。

  2. 据我所知, ON 子句不支持括号中的多个连接条件。 / li>
  3. NoSuchMethodError 不是Hibernate会抛出的东西:-)你可能有一个旧版本的 antlr.jar 在你的类路径中的某个地方,它会被代替Hibernate期望的地方。找到它并将其删除。

  1. 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.
  2. As far as I'm aware ON clause does not support multiple joined conditions in parenthesis.
  3. NoSuchMethodError is not something Hibernate would throw :-) You likely have an older version of antlr.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.