where子句中的HQL隐式连接生成交叉连接而不是内连接

Rot*_*tem 8 hibernate hql

我正在使用Hibernate 3.6和MSSQL 2012.

执行此HQL时

select tbl.state from Property tbl where tbl.state = 1 and tbl.entity.state = 1 and
tbl.entity.className = 'com....' and tbl.fieldName = 'fieldName'
Run Code Online (Sandbox Code Playgroud)

我得到这个SQL

select property0_.State as col_0_0_ from Properties property0_ cross join Entities
entity1_ where property0_.refEntityid=entity1_.id and property0_.State=1 and
entity1_.State=1 and entity1_.ClassName='com....' and property0_.FieldName='fieldName'
Run Code Online (Sandbox Code Playgroud)

*注意where子句中的交叉连接添加条件.

根据Hibernate docs https://docs.jboss.org/hibernate/core/3.5/reference/en/html/queryhql.html#queryhql-joins-forms

隐式连接应生成内连接.

我注意到有一个开放的错误https://hibernate.atlassian.net/browse/HHH-7707可能是指这个问题,但没有人回答,它已经打开一年了.

我很感激有关此问题的任何信息.谢谢.

PS.我很清楚使用隐式连接不是编写HQL的正确方法,但我现在无法做任何事情.

JB *_*zet 9

您的连接是内连接,但使用旧方法在where子句中添加条件:

where property0_.refEntityid=entity1_.id
Run Code Online (Sandbox Code Playgroud)

而不是用它

inner join Entities entity1_ on property0_.refEntityid=entity1_.id
Run Code Online (Sandbox Code Playgroud)

结果完全一样.

在HQL中使用隐式连接根本不是问题,只要您了解他们正在做什么.

  • 阅读完您的答案后,我回到了 hibernate 文档中的“连接语法形式”部分,并重新阅读了几次。现在我开始意识到在那里使用的术语“内连接”对于内连接语法来说不是必需的,而只是用于内连接操作,可以通过两种方式完成,正确的内连接或交叉连接添加 where 子句. 所以现在,我可以重新开始吃饭、睡觉和洗澡了,让它去吧。谢谢@jb-nizet (2认同)