问题描述
假设有两个表A[a_id, b_id]
和B[b_id,c]
.
我需要执行形式为"From A a ORDER BY a.b.c"
的HQL查询,而b
在类A
中为空.
I need to execute the HQL query of form "From A a ORDER BY a.b.c"
, while b
is nullable in class A
.
但是,该查询仅返回具有非null b
属性的A
实例.发生这种情况是因为Hibernate生成形式为"SELECT FROM A,B WHERE A.b_id = B.b_id ORDER BY B.c"
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"
用b
中具有null
的那些实例返回A
的所有实例的方法是什么?
What is the way to return all instances of A
with those having null
in b
to appear first/last?
推荐答案
我有同样的问题,并且已经解决了:
I have the same problem and I have resolve it like this :
SELECT a
FROM a
LEFT JOIN a.b b
LEFT JOIN b.c c
ORDER BY c.id
它采用HQL语法!
当孩子为NULL时,"LEFT JOIN"允许显示行.
The "LEFT JOIN" allows to display line when the child is NULL.
实际上,如果您未指定连接,则hibernate会自动创建一个"INNER JOIN"来删除NULL子级.
In fact, if you don't specify the join, hibernate creates automatically an "INNER JOIN" which remove the NULL child.
这篇关于HQL:可为空的属性的按属性排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!