Example usage for javax.persistence.criteria CriteriaBuilder countDistinct

List of usage examples for javax.persistence.criteria CriteriaBuilder countDistinct

Introduction

In this page you can find the example usage for javax.persistence.criteria CriteriaBuilder countDistinct.

Prototype

Expression<Long> countDistinct(Expression<?> x);

Source Link

Document

Create an aggregate expression applying the count distinct operation.

Usage

From source file:eu.uqasar.service.AbstractService.java

public <T extends Persistable> long countAll(Class<T> clazz) {
    logger.infof("counting all %ss ...", getReadableClassName(clazz));
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> criteria = cb.createQuery(Long.class);
    Root<T> root = criteria.from(clazz);
    criteria.select(cb.countDistinct(root));
    return em.createQuery(criteria).getSingleResult();
}

From source file:eu.uqasar.service.AbstractService.java

/**
 *
 * @return//w ww  . ja  v  a2  s  .c o m
 */
public long countAll() {
    logger.infof("counting all %ss ...", readableClassName);
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> criteria = cb.createQuery(Long.class);
    Root<T> root = criteria.from(clazz);
    criteria.select(cb.countDistinct(root));
    return em.createQuery(criteria).getSingleResult();
}

From source file:eu.uqasar.service.ProcessService.java

public long countAllFiltered(final ProcessesFilterStructure filter) {
    logger.infof("counting all Processes matching the filter %s ...", filter);
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> criteria = cb.createQuery(Long.class);
    Root<Process> from = criteria.from(Process.class);
    List<Predicate> predicates = getFilterPredicates(filter, cb, from);
    if (!predicates.isEmpty()) {
        criteria.where(cb.and(predicates.toArray(new Predicate[predicates.size()])));
    }/*from   w w  w  .j ava 2  s .  co m*/
    criteria.select(cb.countDistinct(from));
    return em.createQuery(criteria).getSingleResult();
}

From source file:com.alliander.osgp.adapter.ws.infra.specifications.JpaDeviceSpecifications.java

@Override
public Specification<Device> isManagedExternally(final Boolean isManagedExternally)
        throws ArgumentNullOrEmptyException {
    if (isManagedExternally == null) {
        throw new ArgumentNullOrEmptyException("isManagedExternally");
    }/*from  w  w  w .j  ava 2s  . c o  m*/

    return new Specification<Device>() {

        @Override
        public Predicate toPredicate(final Root<Device> deviceRoot, final CriteriaQuery<?> query,
                final CriteriaBuilder cb) {
            final Subquery<Long> subquery = query.subquery(Long.class);
            final Root<DeviceAuthorization> deviceAuthorizationRoot = subquery.from(DeviceAuthorization.class);
            subquery.select(cb.countDistinct(deviceAuthorizationRoot));
            subquery.where(cb.equal(deviceAuthorizationRoot.get("device"), deviceRoot.<Long>get("id")));
            if (isManagedExternally) {
                return cb.greaterThan(subquery, Long.valueOf(1));
            } else {
                return cb.lessThanOrEqualTo(subquery, Long.valueOf(1));
            }
        }
    };
}

From source file:eu.uqasar.service.AbstractService.java

protected Long performDistinctCountWithEqualPredicate(final Attribute<T, ?> attr, Object value) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> query = cb.createQuery(Long.class);
    Root<T> root = query.from(clazz);
    query.where(cb.equal(root.get(attr.getName()), value));
    query.select(cb.countDistinct(root));
    return em.createQuery(query).getSingleResult();
}

From source file:com.sishuok.es.common.repository.support.SimpleBaseRepository.java

/**
 * Creates a new count query for the given {@link org.springframework.data.jpa.domain.Specification}.
 *
 * @param spec can be {@literal null}.//w ww  .  j a  v  a  2  s  . c  o m
 * @return
 */
private TypedQuery<Long> getCountQuery(Specification<M> spec) {

    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Long> query = builder.createQuery(Long.class);

    Root<M> root = applySpecificationToCriteria(spec, query);

    if (query.isDistinct()) {
        query.select(builder.countDistinct(root));
    } else {
        query.select(builder.count(root));
    }

    TypedQuery<Long> q = em.createQuery(query);
    repositoryHelper.applyEnableQueryCache(q);
    return q;
}

From source file:cn.guoyukun.spring.jpa.repository.support.SimpleBaseRepository.java

/**
 * Creates a new count query for the given {@link org.springframework.data.jpa.domain.Specification}.
 *
 * @param spec can be {@literal null}.//w  ww .ja  v  a2s  .c  o m
 * @return
 */
protected TypedQuery<Long> getCountQuery(Specification<M> spec) {

    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Long> query = builder.createQuery(Long.class);

    Root<M> root = applySpecificationToCriteria(spec, query);

    if (query.isDistinct()) {
        query.select(builder.countDistinct(root));
    } else {
        query.select(builder.count(root));
    }

    TypedQuery<Long> q = em.createQuery(query);
    repositoryHelper.applyEnableQueryCache(q);
    return q;
}

From source file:eu.uqasar.service.user.UserService.java

public long countAllFiltered(final UserFilterStructure filter) {
    logger.infof("counting all Users matching the filter %s ...", filter);
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> criteria = cb.createQuery(Long.class);
    Root<User> from = criteria.from(User.class);
    List<Predicate> predicates = getFilterPredicates(filter, cb, from);
    if (!predicates.isEmpty()) {
        criteria.where(cb.and(predicates.toArray(new Predicate[predicates.size()])));
    }/*from   w w  w. j  av  a2  s .c  om*/
    criteria.select(cb.countDistinct(from));
    return em.createQuery(criteria).getSingleResult();
}

From source file:org.querybyexample.jpa.GenericRepository.java

/**
 * Count the number of E instances./*from  w  ww.  j  av  a  2  s . c o m*/
 *
 * @param entity a sample entity whose non-null properties may be used as
 * search hint
 * @param searchParameters carries additional search information
 * @return the number of entities matching the search.
 */
@Transactional(readOnly = true)
public int findCount(E entity, SearchParameters sp) {
    checkNotNull(entity, "The entity cannot be null");
    checkNotNull(sp, "The searchParameters cannot be null");

    if (sp.hasNamedQuery()) {
        return byNamedQueryUtil.numberByNamedQuery(sp).intValue();
    }
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();

    CriteriaQuery<Long> criteriaQuery = builder.createQuery(Long.class);
    Root<E> root = criteriaQuery.from(type);

    if (sp.getDistinct()) {
        criteriaQuery = criteriaQuery.select(builder.countDistinct(root));
    } else {
        criteriaQuery = criteriaQuery.select(builder.count(root));
    }

    // predicate
    Predicate predicate = getPredicate(root, builder, entity, sp);
    if (predicate != null) {
        criteriaQuery = criteriaQuery.where(predicate);
    }

    orderByUtil.forceJoinOrder(root, sp);

    TypedQuery<Long> typedQuery = entityManager.createQuery(criteriaQuery);

    applyCacheHints(typedQuery, sp);
    Long count = typedQuery.getSingleResult();

    if (count != null) {
        return count.intValue();
    } else {
        log.warn("findCount returned null");
        return 0;
    }
}