HQL implicit join in where clause generate cross j

2019-02-06 20:28发布

I'm using Hibernate 3.6 and MSSQL 2012.

When executing this 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'

I'm getting this 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'

*Notice the cross join and the added condition in the where clause.

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

implicit join should generate to inner join.

I've noticed that there is an open bug https://hibernate.atlassian.net/browse/HHH-7707 that may be referring to this problem but no one answered and it's opened for a year already.

I would appreciate any information about this issue. Thank you.

PS. I'm well aware that using implicit joins is not the right way of writing HQL but I can't do anything about this right now.

标签: hibernate hql
2条回答
Explosion°爆炸
2楼-- · 2019-02-06 20:39

When using HQL, always use proper aliases when performing join operations.Your query should be something like that:

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'

Otherwise if you try to use tbl.entity.someProperty, in HQL, it will always create a crossJoin

查看更多
beautiful°
3楼-- · 2019-02-06 20:45

Your join is an inner join, but using the old syntax consisting in adding a condition in the where clause:

where property0_.refEntityid=entity1_.id

instead of doing it with

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

The result is exactly the same.

And using implicit joins in HQL is not a problem at all, as long as you understand what they're doing.

查看更多
登录 后发表回答