Question

How would I translate this JPQL query into Criteria API?

select count(p) from Person p join p.glanceList g where p.duration < 1000 and g
.duration < 1000
Was it helpful?

Solution

Try this one.

CriteriaQuery<Long> q = cb.createQuery(Long.class);
Root<Person> person = q.from(Person.class);
Join<Person,Glance> glance = person.join("glanceList", JoinType.INNER);
q.select(cb.count(person))
.where(cb.lt(person.get("duration"), 1000), cb.lt(glance.get("duration"), 1000));

Assuming Glance is the class of the entity in the join.

And

CriteriaBuilder cb = em.getCriteriaBuilder();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top