【发布时间】:2012-09-05 19:40:02
【问题描述】:
来自 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 的 with 的正确 JPQL 子句。
【问题讨论】:
-
来自 Hibernate 文档:
HQL also defines a WITH clause to qualify the join conditions. Again, this is specific to HQL; JPQL does not define this feature.docs.jboss.org/hibernate/orm/4.1/devguide/en-US/html/… -
请注意,在 Hibernate 中,您也可以在 JPQL 中使用
with。 -
@axtavt 我试过了,它抛出了 IllegalArgumentException。