JPA Criteria API

JPA Criteria API


Count
CriteriaBuilder critBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<Long> critQuery = criteriaBuilder.createQuery(Long.class);
Root<Foo> root = critQuery.from(Foo.class);
critQuery.select(critBuilder.countDistinct(root));
int count = entityManager.createQuery(critQuery).getSingleResult().intValue();


 

 Result

CriteriaBuilder critBuilder = entityManager.getCriteriaBuilder(); 
CriteriaQuery<Long> critQuery = criteriaBuilder.createQuery(Long.class);
Root<Foo> root = critQuery.from(Foo.class);
critQuery.select(root).distinct(true);
List<Foo> result = entityManager.createQuery(critQuery).getResultList();

你可能感兴趣的:(JPA Criteria API)