在JPA 2中,使用CriteriaQuery,如何计算结果
我对JPA 2比较陌生,它是CriteriaBuilder / CriteriaQuery API:
CriteriaQuery
javadoc
CriteriaQuery
在Java EE 6教程中
我想计算一个CriteriaQuery的结果而不实际检索它们。 这是可能的,我没有find任何这样的方法,唯一的办法就是这样做:
CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<MyEntity> cq = cb .createQuery(MyEntityclass); // initialize predicates here return entityManager.createQuery(cq).getResultList().size();
而这不可能是正确的做法…
有解决scheme吗?
MyEntity
types的查询将返回MyEntity
。 你想要一个Long
查询。
CriteriaBuilder qb = entityManager.getCriteriaBuilder(); CriteriaQuery<Long> cq = qb.createQuery(Long.class); cq.select(qb.count(cq.from(MyEntity.class))); cq.where(/*your stuff*/); return entityManager.createQuery(cq).getSingleResult();
显然,你会想要build立你的expression与你在示例中跳过的任何限制和分组等。
我已经使用cb.createQuery()(不带结果types参数)对此进行了sorting:
public class Blah() { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery query = criteriaBuilder.createQuery(); Root<Entity> root; Predicate whereClause; EntityManager entityManager; Class<Entity> domainClass; ... Methods to create where clause ... public Blah(EntityManager entityManager, Class<Entity> domainClass) { this.entityManager = entityManager; this.domainClass = domainClass; criteriaBuilder = entityManager.getCriteriaBuilder(); query = criteriaBuilder.createQuery(); whereClause = criteriaBuilder.equal(criteriaBuilder.literal(1), 1); root = query.from(domainClass); } public CriteriaQuery<Entity> getQuery() { query.select(root); query.where(whereClause); return query; } public CriteriaQuery<Long> getQueryForCount() { query.select(criteriaBuilder.count(root)); query.where(whereClause); return query; } public List<Entity> list() { TypedQuery<Entity> q = this.entityManager.createQuery(this.getQuery()); return q.getResultList(); } public Long count() { TypedQuery<Long> q = this.entityManager.createQuery(this.getQueryForCount()); return q.getSingleResult(); } }
希望能帮助到你 :)
CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Long> cq = cb.createQuery(Long.class); cq.select(cb.count(cq.from(MyEntity.class))); return em.createQuery(cq).getSingleResult();
由于其他答案是正确的,但太简单了,所以为了完整,我在下面的代码片段中提出了在复杂的 JPA Criteria查询(具有多个联接,提取,条件)上执行SELECT COUNT
。
这是稍微修改这个答案 。
public <T> long count(final CriteriaBuilder cb, final CriteriaQuery<T> selectQuery, Root<T> root) { CriteriaQuery<Long> query = createCountQuery(cb, selectQuery, root); return this.entityManager.createQuery(query).getSingleResult(); } private <T> CriteriaQuery<Long> createCountQuery(final CriteriaBuilder cb, final CriteriaQuery<T> criteria, final Root<T> root) { final CriteriaQuery<Long> countQuery = cb.createQuery(Long.class); final Root<T> countRoot = countQuery.from(criteria.getResultType()); doJoins(root.getJoins(), countRoot); doJoinsOnFetches(root.getFetches(), countRoot); countQuery.select(cb.count(countRoot)); countQuery.where(criteria.getRestriction()); countRoot.alias(root.getAlias()); return countQuery.distinct(criteria.isDistinct()); } @SuppressWarnings("unchecked") private void doJoinsOnFetches(Set<? extends Fetch<?, ?>> joins, Root<?> root) { doJoins((Set<? extends Join<?, ?>>) joins, root); } private void doJoins(Set<? extends Join<?, ?>> joins, Root<?> root) { for (Join<?, ?> join : joins) { Join<?, ?> joined = root.join(join.getAttribute().getName(), join.getJoinType()); joined.alias(join.getAlias()); doJoins(join.getJoins(), joined); } } private void doJoins(Set<? extends Join<?, ?>> joins, Join<?, ?> root) { for (Join<?, ?> join : joins) { Join<?, ?> joined = root.join(join.getAttribute().getName(), join.getJoinType()); joined.alias(join.getAlias()); doJoins(join.getJoins(), joined); } }
希望能节省一些时间。
因为恕我直言,JPA标准API不直观也不太可读。
这有点棘手,取决于您使用的JPA 2实现,这个实现适用于EclipseLink 2.4.1,但不适用于Hibernate,这里是EclipseLink的通用CriteriaQuery计数:
public static Long count(final EntityManager em, final CriteriaQuery<?> criteria) { final CriteriaBuilder builder=em.getCriteriaBuilder(); final CriteriaQuery<Long> countCriteria=builder.createQuery(Long.class); countCriteria.select(builder.count(criteria.getRoots().iterator().next())); final Predicate groupRestriction=criteria.getGroupRestriction(), fromRestriction=criteria.getRestriction(); if(groupRestriction != null){ countCriteria.having(groupRestriction); } if(fromRestriction != null){ countCriteria.where(fromRestriction); } countCriteria.groupBy(criteria.getGroupList()); countCriteria.distinct(criteria.isDistinct()); return em.createQuery(countCriteria).getSingleResult(); }
您也可以使用投影:
ProjectionList projection = Projections.projectionList(); projection.add(Projections.rowCount()); criteria.setProjection(projection); Long totalRows = (Long) criteria.list().get(0);
使用countDistinct
而不是count
。