Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HQL implicit join in where clause generate cross join instead of inner join

Tags:

hibernate

hql

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.

like image 603
Rotem Avatar asked Nov 27 '13 11:11

Rotem


2 Answers

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.

like image 96
JB Nizet Avatar answered Oct 28 '22 04:10

JB Nizet


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

like image 23
Rajdeep Avatar answered Oct 28 '22 06:10

Rajdeep