JPA查询结果总数count的使用

CriteriaBuilder critBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery critQuery = criteriaBuilder.createQuery(Long.class);
Root root = critQuery.from(Foo.class);

critQuery.select(critBuilder.countDistinct(root));
int count = entityManager.createQuery(critQuery).getSingleResult().intValue();

 

 

 

CriteriaBuilder critBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery critQuery = criteriaBuilder.createQuery(Long.class);
Root root = critQuery.from(Foo.class);

critQuery.select(root).distinct(true);
List result = entityManager.createQuery(critQuery).getResultList();

 

你可能感兴趣的:(JPA)