List of usage examples for javax.persistence.criteria CriteriaBuilder countDistinct
Expression<Long> countDistinct(Expression<?> x);
From source file:org.easy.criteria.CriteriaProcessor.java
/** * Counts the result found for the given criteria * //from w w w . jav a 2 s . co m * @param criteria * @param distinct * @return */ public <T> long count(final CriteriaComposer<T> criteria, boolean distinct) { log.trace("CriteriaProcessor.count"); Preconditions.checkNotNull(criteria); Class<T> forClass = criteria.getEntityClass(); CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Long> criteriaQuery = criteriaBuilder.createQuery(Long.class); Root<T> root = criteriaQuery.from(forClass); log.debug("root =" + forClass.getName()); if (distinct) criteriaQuery.select(criteriaBuilder.countDistinct(root)); else criteriaQuery.select(criteriaBuilder.count(root)); List<Predicate> predicates = new ArrayList<Predicate>(); if (criteria != null) { criteria.generateJoins(root); criteria.generateWhere(criteriaBuilder, predicates); } criteriaQuery.where(predicates.toArray(new Predicate[predicates.size()])); TypedQuery<Long> query = entityManager.createQuery(criteriaQuery); long result = query.getSingleResult(); log.debug("CriteriaProcessor.count =" + result); return result; }
From source file:org.exoplatform.social.addons.storage.dao.jpa.query.RelationshipQueryBuilder.java
/** * Builds the Typed Query/* ww w .j av a 2 s . c o m*/ * @return */ public TypedQuery<Long> buildCount() { EntityManager em = EntityManagerHolder.get(); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Long> criteria = cb.createQuery(Long.class); Root<Connection> connection = criteria.from(Connection.class); Predicate predicate = null; //owner if (this.owner != null) { predicate = cb.equal(connection.get(Connection_.senderId), owner.getId()); } //status if (this.status != null) { if (Relationship.Type.PENDING.equals(this.status)) { predicate = cb.and(predicate, addInClause(cb, connection.get(Connection_.status), types)); } else { predicate = cb.and(predicate, cb.equal(connection.get(Connection_.status), this.status)); } } CriteriaQuery<Long> select = criteria.select(cb.countDistinct(connection)); select.where(predicate); return em.createQuery(select); }
From source file:org.exoplatform.social.addons.storage.dao.jpa.query.RelationshipQueryBuilder.java
public TypedQuery<Long> buildFilterCount() { EntityManager em = EntityManagerHolder.get(); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Long> criteria = cb.createQuery(Long.class); Root<Connection> relationship = criteria.from(Connection.class); Join<Connection, Profile> receiver = relationship.join(Connection_.receiver); CriteriaQuery<Long> select = criteria.select(cb.countDistinct(relationship)); ///*from w ww .j a v a2s .c o m*/ select.where(buildPredicateFilter(cb, receiver, relationship)); // return em.createQuery(select); }
From source file:org.springframework.data.jpa.repository.support.SimpleJpaRepository.java
/** * Creates a new count query for the given {@link Specification}. * /*from w w w .j av a 2s . co m*/ * @param spec can be {@literal null}. * @return */ protected TypedQuery<Long> getCountQuery(Specification<T> spec) { CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<Long> query = builder.createQuery(Long.class); Root<T> root = applySpecificationToCriteria(spec, query); if (query.isDistinct()) { query.select(builder.countDistinct(root)); } else { query.select(builder.count(root)); } return em.createQuery(query); }