【发布时间】:2013-02-25 13:48:46
【问题描述】:
我正在使用休眠 3.6.10.Final 和 MySQL 5.5。
我写了一个这样的动态查询:
"from " + clazz.getName()+ " ORDER BY "+sortField+" "+sortDirection
我的休眠实体具有多对一的父/子关系。 如果查询是按父字段排序的,hibernate 会生成这个 hql 选择:
select parent0_.ID as ID10_, parent0_.ID_CHILD as ID6_10_
from parent parent0_
order by parent0_.PARENTFIELD ASC
如果查询是按子字段排序的,我有以下 hql:
select parent0_.ID as ID10_, parent0_.ID_CHILD as ID6_10_
from parent parent0_
cross join child1_
where parent0_.ID_CHILD = child1_.ID
order by child1_.CHILDFIELD ASC
第二个查询返回的结果较少,因为 parent0_.ID_CHILD 可以为空。 有没有办法强制休眠生成左连接?
我需要这样的东西
select parent0_.ID as ID10_,
parent0_.ID_CHILD as ID6_10_
from
parent parent0_
left join
child child1_
on
parent0_.ID_CHILD = child1_.ID
order by child1_.CHILDFIELD ASC
【问题讨论】:
-
这是一个答案吗?我不想为我的父实体和子实体编写特定查询。我想重用那个通用查询,强制休眠也接受带有 null 孩子的父母。
-
那么你需要一个带有显式左连接的查询。没有办法解决它。
child.parent.someProperty将始终生成内部连接。阅读文档。就在里面。
标签: mysql hibernate hql left-join cross-join