tags:

views:

342

answers:

1

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?

A: 

What about :

from A a left join a.b_fk b order by b.c

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 ...

KLE