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

时间:2013-11-27 11:15:08

标签: 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'

我得到了这个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'

*请注意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的正确方法,但我现在无法做任何事情。

2 个答案:

答案 0 :(得分:9)

您的联接是一个内部联接,但使用的旧语法包括在where子句中添加条件:

where property0_.refEntityid=entity1_.id

而不是

inner join Entities entity1_ on property0_.refEntityid=entity1_.id

结果完全一样。

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

答案 1 :(得分:1)

使用HQL时,在执行连接操作时始终使用正确的别名。您的查询应该是这样的:

var pathParts = filepath.split('\\');
var lastPath = pathParts[pathParts - 2] + @"\" + pathParts[pathParts - 1];

否则,如果您尝试在HQL中使用select tbl.state from Property tbl left join tbl.entity entity where tbl.state = 1 and entity.state = 1 and entity.className = 'com....' and tbl.fieldName = 'fieldName' ,它将始终创建一个crossJoin