Assuming two tables, A[a_id, b_id]
and B[b_id,c]
.
I need to execute the HQL query of form "From A a ORDER BY a.b.c"
, while b
is nullable in class A
.
The query,however, returns only instances of A
which have non-null b
property.
This happens because Hibernate generates SQL of form "SELECT FROM A,B WHERE A.b_id = B.b_id ORDER BY B.c"
What is the way to return all instances of A
with those having null
in b
to appear first/last?
What about :
The left join takes care of making the join, even if the
b_fk
property on the java entity (not the table) is null.Edited : Sorry, I mentionned sorting the nulls differently. To sort (not taking nulls into account), you can specify 'desc' to inverse the sort order (default = 'asc'). For nulls, I believe Hibernate lets the default database order... Try it yourself on your database to see what happens (sorry for misleading in first version of post).
A lot of information can be found in Hibernate's reference documentation :
http://docs.jboss.org/hibernate/stable/core/reference/en/html/queryhql.html
The rest usually depends on the database you use ...
I have the same problem and I have resolve it like this :
It's in HQL syntax !
The "LEFT JOIN" allows to display line when the child is NULL.
In fact, if you don't specify the join, hibernate creates automatically an "INNER JOIN" which remove the NULL child.