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:com.ocs.dynamo.dao.query.JpaQueryBuilder.java

/**
 * Creates a query that performs a count
 * //ww w .ja  va  2  s . com
 * @param entityManager
 *            the entity manager
 * @param entityClass
 *            the entity class
 * @param filter
 *            the filter to apply
 * @param distinct
 *            whether to return only distinct results
 * @return
 */
public static <T> CriteriaQuery<Long> createCountQuery(EntityManager entityManager, Class<T> entityClass,
        Filter filter, boolean distinct) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Long> cq = builder.createQuery(Long.class);
    Root<T> root = cq.from(entityClass);

    cq.select(distinct ? builder.countDistinct(root) : builder.count(root));

    Predicate p = createPredicate(filter, builder, root);
    if (p != null) {
        cq.where(p);
    }
    return cq;
}

From source file:org.jdal.dao.jpa.JpaUtils.java

/**
 * Create a row count CriteriaQuery from a CriteriaQuery
 * @param em entity manager//from  ww w .java2  s .  co  m
 * @param criteria source criteria
 * @return row count CriteriaQuery
 */
public static <T> CriteriaQuery<Long> countCriteria(EntityManager em, CriteriaQuery<T> criteria) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Long> countCriteria = builder.createQuery(Long.class);
    copyCriteriaWithoutSelectionAndOrder(criteria, countCriteria);

    Expression<Long> countExpression;

    if (criteria.isDistinct()) {
        countExpression = builder.countDistinct(findRoot(countCriteria, criteria.getResultType()));
    } else {
        countExpression = builder.count(findRoot(countCriteria, criteria.getResultType()));
    }

    return countCriteria.select(countExpression);
}

From source file:com.zergiu.tvman.dao.impl.TVShowDaoImpl.java

@Override
public Long getTVShowsCount() {
    CriteriaBuilder criteria = em.getCriteriaBuilder();
    CriteriaQuery<Long> query = criteria.createQuery(Long.class);
    query.select(criteria.countDistinct(query.from(TVShow.class)));
    Long count = em.createQuery(query).getSingleResult();
    log.debug("all count: " + count);
    return count;
}

From source file:com.zergiu.tvman.dao.impl.TVShowDaoImpl.java

@Override
public Long getActiveTVShowsCount() {
    CriteriaBuilder criteria = em.getCriteriaBuilder();
    CriteriaQuery<Long> query = criteria.createQuery(Long.class);
    Root<TVShow> show = query.from(TVShow.class);
    query.select(criteria.countDistinct(show));
    query.where(criteria.equal(show.<TVShowStatus>get("status"), TVShowStatus.Continuing));
    Long count = em.createQuery(query).getSingleResult();
    log.debug("active count: " + count);
    return count;
}

From source file:eu.uqasar.service.company.CompanyService.java

/**
 * /* w  ww  . j a  va 2s .co  m*/
 * @param companyId
 * @return
 */
public boolean companyExists(Long companyId) {
    logger.info(String.format("checking if Company with ID %d exists ...", companyId));
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> criteria = cb.createQuery(Long.class);
    Root<Company> from = criteria.from(Company.class);
    criteria.where(cb.equal(from.get(Company_.id), companyId));
    criteria.select(cb.countDistinct(from));
    return (em.createQuery(criteria).getSingleResult().longValue() == 1);
}

From source file:org.osiam.resource_server.storage.dao.ResourceDao.java

public <T extends ResourceEntity, V> boolean isUniqueAttributeAlreadyTaken(String attributeValue, String id,
        SingularAttribute<? super T, V> attribute, Class<T> clazz) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> cq = cb.createQuery(Long.class);
    Root<T> resource = cq.from(clazz);

    cq.select(cb.countDistinct(resource));

    Predicate predicate = cb.equal(resource.get(attribute), attributeValue);
    if (id != null) {
        Predicate ignoreId = cb.notEqual(resource.get(ResourceEntity_.id), id);
        predicate = cb.and(predicate, ignoreId);
    }//from   w  w  w.  j a  va2s.  com
    cq.where(predicate);

    TypedQuery<Long> countQuery = em.createQuery(cq);

    return countQuery.getSingleResult() > 0;
}

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

/**
 * /*  w  w  w  .  ja v a 2s  .  co  m*/
 * @param productId
 * @return
 */
public boolean productExists(Long productId) {
    logger.info(String.format("checking if product with ID %d exists ...", productId));
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> criteria = cb.createQuery(Long.class);
    Root<Product> from = criteria.from(Product.class);
    criteria.where(cb.equal(from.get(Product_.id), productId));
    criteria.select(cb.countDistinct(from));
    return (em.createQuery(criteria).getSingleResult() == 1);
}

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

/**
 * /*ww w. j  a va2s .  c o m*/
 * @param processId
 * @return
 */
public boolean processExists(Long processId) {
    logger.info(String.format("checking if process with ID %d exists ...", processId));
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> criteria = cb.createQuery(Long.class);
    Root<Process> from = criteria.from(Process.class);
    criteria.where(cb.equal(from.get(Process_.id), processId));
    criteria.select(cb.countDistinct(from));
    return (em.createQuery(criteria).getSingleResult() == 1);
}

From source file:com.carser.viamais.vo.TransactionFilter.java

public Long count(final EntityManager em) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> cq = cb.createQuery(Long.class);
    Root<Transaction> transaction = cq.from(Transaction.class);
    cq.select(cb.countDistinct((transaction)));
    cq.where(getPredicates(cb, transaction));
    return em.createQuery(cq).getSingleResult();
}

From source file:eu.uqasar.service.company.CompanyService.java

public long countAllFiltered(final CompanyFilterStructure filter) {
    logger.infof("counting all Companies matching the filter %s ...", filter);
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> criteria = cb.createQuery(Long.class);
    Root<Company> from = criteria.from(Company.class);
    criteria.where(cb.or(cb.equal(from.get(Company_.name), filter.getName()),
            cb.equal(from.get(Company_.shortName), filter.getShortName()),
            cb.equal(from.get(Company_.country), filter.getCountry())));
    criteria.select(cb.countDistinct(from));
    return em.createQuery(criteria).getSingleResult();
}