HQL clause in JPQL

From the Hibernate 3.6 documentation:

You can provide additional join conditions using the HQL keyword with the keyword.

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

This with clause allows you to add a constraint on the JOIN clause (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 

The following SQL is created:

 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 really needs to happen 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 am sure that I am missing the correct JPQL clause for HQL with .

+4
source share
3 answers

No, there is no such function in JPQL. Support for joining a specific condition is mentioned in the JPA 2.1 topic list :

- support for external connections with ON conditions;

So maybe JPQL will be in the future.

+1
source

But you can do it using the criteria API

  Criteria crit = session.createCriteria(Cat.class); crit.createAlias("kittens", "kitten", Criteria.LEFT_JOIN, Restrictions.gt("weight", 10.0); List<Cat> catsWithFatKittens = crit.list(); 

This is the barely documented signature of Criteria.createAlias() with the Restriction object as the fourth parameter. It works so well that in order to learn functionality, you need to study the API criteria.

+1
source

JPA 2.1 added support Enable connection .

It is also a better name choice than a HQL clause with a condition.

+1
source

Source: https://habr.com/ru/post/1433868/


All Articles