JPA子查询

CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<MemberAccountItem> cq = cb.createQuery(MemberAccountItem.class);
Root<MemberAccountItem> root = cq.from(MemberAccountItem.class);
cq.select(root);

// 子查询
Subquery<Long> sq = cq.subquery(Long.class);
Root<MemberAccountItem> root2 = sq.from(MemberAccountItem.class);
sq.select(cb.max(root2.get(MemberAccountItem_.numberProcessed)));
sq.where(cb.equal(root2.get(MemberAccountItem_.company), company));

cq.where(cb.and(cb.equal(root.get(MemberAccountItem_.company), company),
cb.equal(root.get(MemberAccountItem_.numberProcessed), sq)));

你可能感兴趣的:(jpa,子查询,subquery)