来自 Hibernate 3.6 文档:
您可以使用 HQL with 关键字提供额外的连接条件。
from Cat as cat
left join cat.kittens as kitten
with kitten.bodyWeight > 10.0
该with
子句允许对 JOIN 条件(ON 子句)添加限制。JPQL中有这样的东西吗?
当我运行以下 JPQL 时:
select c from ContainerDef c left join fetch c.displayState ds where c.id = 1 and ds.user.id = 2
生成以下 SQL:
select
...
from
CONTAINER_DEF containerd0_
left outer join
USER_CONTAINERDEF displaysta1_
on containerd0_.CONTAINERDEF_ID=displaysta1_.CONTAINERDEF_ID
where
containerd0_.CONTAINERDEF_ID=?
and displaysta1_.AUTHUSER_ID=?
真正应该生成的是:
select
...
from
CONTAINER_DEF containerd0_
left outer join
USER_CONTAINERDEF displaysta1_
on containerd0_.CONTAINERDEF_ID=displaysta1_.CONTAINERDEF_ID
and displaysta1_.AUTHUSER_ID=?
where
containerd0_.CONTAINERDEF_ID=?
我确定我错过了 HQL 的正确 JPQL 子句with
。