Hibernate – HQL’With ‘clause in JPQL

From the Hibernate 3.6 documentation:

You may supply extra join conditions using the HQL with keyword.

from Cat as cat
left join cat.kittens as kitten
with kitten.bodyWeight> 10.0

This with clause allows in the JOIN condition ( ON clause) to add restrictions. Is there such a thing in JPQL?

When I run the following JPQL:

select c from ContainerDef c left join fetch c.displayState ds where c.id = 1 and ds.user .id = 2

Generate the following 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=?

What should really be produced is:

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=?

I I’m sure I missed the correct JPQL clause of HQL.

No, there is no such feature in JPQL. JPA 2.1 The topic list mentions the join support for specific conditions:

— support for outer joins with ON conditions;

So maybe JPQL will have it in the future.

From the Hibernate 3.6 documentation:

You may supply extra join conditions using the HQL with keyword.

from Cat as cat
left join cat.kittens as kitten
with kitten.bodyWeight> 10.0< /pre>

This with clause allows to add restrictions on the JOIN condition (ON clause). Is there such a thing in JPQL?

When I run the following JPQL:

select c from ContainerDef c left join fetch c.displayState ds where c.id = 1 and ds.user .id = 2

Generate the following 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=?

What should really be produced is:

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=?

I I'm sure I missed the correct JPQL clause of HQL.

No, there is no such function in JPQL. The addition of specific conditions is mentioned in the JPA 2.1 topic list Support:

— support for outer joins with ON conditions;

So maybe JPQL will have it in the future.

Leave a Comment

Your email address will not be published.