JPA Criteria API加入

时间:2014-03-18 20:46:11

标签: jpa criteria-api

如何将此JPQL查询转换为Criteria API?

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

1 个答案:

答案 0 :(得分:3)

试试这个。

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

假设Glance是联接中实体的类。

并且

CriteriaBuilder cb = em.getCriteriaBuilder();