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
有帮助吗?

解决方案

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();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top