Example usage for javax.persistence.criteria CriteriaBuilder count

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

Introduction

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

Prototype

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

Source Link

Document

Create an aggregate expression applying the count operation.

Usage

From source file:bq.jpa.demo.query.criteria.service.CriteriaService.java

/**
 * group by and having ://from  w w  w .j  a  v  a2 s.  co m
 * SELECT e, COUNT(p) FROM jpa_query_employee e JOIN e.projects p GROUP BY e HAVING COUNT(p) >= 2
 */
@Transactional
public void doGroupby() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Object[]> c = cb.createQuery(Object[].class);
    Root<Employee> e = c.from(Employee.class);
    Join<Employee, Project> p = e.join("projects");
    c.multiselect(e, cb.count(p)).groupBy(e).having(cb.ge(cb.count(p), 2));

    showResult(c);
}

From source file:edu.umm.radonc.ca_dash.model.ActivityFacade.java

public int itemsDateRangeCount(Date start, Date end) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery cq = cb.createQuery(ActivityAIPC.class);
    Root<ActivityAIPC> rt = cq.from(ActivityAIPC.class);
    cq.select(cb.count(rt.get(ActivityAIPC_.actinstproccodeser)));
    cq.where(cb.and(rt.get(ActivityAIPC_.fromdateofservice).isNotNull(),
            cb.and(cb.notEqual(rt.get(ActivityAIPC_.procedurecodeser), 528),
                    cb.notEqual(rt.get(ActivityAIPC_.procedurecodeser), 529),
                    cb.notEqual(rt.get(ActivityAIPC_.procedurecodeser), 530),
                    cb.between(rt.get(ActivityAIPC_.fromdateofservice), start, end))));

    Query q = em.createQuery(cq);
    return ((Long) (q.getSingleResult())).intValue();

}

From source file:com.sfs.ucm.controller.SpecificationAction.java

/**
 * Get count of specification rules/* w  w  w .  j  a  va  2 s  .c  o m*/
 * 
 * @return count
 */
private Long getSpecificationRuleCount() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> c = cb.createQuery(Long.class);
    c.select(cb.count(c.from(SpecificationRule.class)));
    return em.createQuery(c).getSingleResult();
}

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

/**
 * Create a TypedQuery from a request page
 * @param page request page//ww  w.  ja  v  a 2  s  .c om
 * @return new TypedQuery
 */
@SuppressWarnings("unchecked")
private <K> TypedQuery<K> getCriteriaQuery(Page<K> page) {
    CriteriaQuery<K> criteria = getCriteria(page);

    CriteriaQuery<Long> countCriteria = (CriteriaQuery<Long>) getCriteria(page);
    CriteriaBuilder cb = em.getCriteriaBuilder();

    Root<?> root = countCriteria.getRoots().iterator().next();
    countCriteria.select(cb.count(root));

    page.setCount((em.createQuery(countCriteria).getSingleResult()).intValue());

    criteria.orderBy(getOrder(page, criteria));

    // Add default select to entity class if none was set.
    if (criteria.getSelection() == null) {
        criteria.select((Selection<? extends K>) root);
    }

    return em.createQuery(criteria);
}

From source file:com.sfs.captor.controller.AlternativeFlowAction.java

/**
 * Get count of flowstep rules//from  w w  w  .ja v a2 s. c o  m
 * 
 * @return count
 */
private Long getFlowStepRuleCount() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> c = cb.createQuery(Long.class);
    c.select(cb.count(c.from(FlowStepRule.class)));
    return em.createQuery(c).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}.//from   w ww . j av a 2 s  .com
 * @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:edu.umm.radonc.ca_dash.model.TxInstanceFacade.java

public int itemsDateRangeCount(Date startDate, Date endDate) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery cq = cb.createQuery(TxInstance.class);
    Root<TxInstance> rt = cq.from(TxInstance.class);
    cq.select(cb.count(rt.get(TxInstance_.activityinstanceser)));
    cq.where(cb.and(rt.get(TxInstance_.completed).isNotNull(),
            cb.between(rt.get(TxInstance_.completed), startDate, endDate)));

    Query q = em.createQuery(cq);
    return ((Long) (q.getSingleResult())).intValue();
}

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 w  w  . j av  a 2  s  .com
 * @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:com.hiperium.dao.common.generic.GenericDAO.java

/**
 * //from  w w  w. ja  v a  2 s .  c om
 * @param entity
 * @param bypassCache
 * @return
 */
protected Long count(T entity, boolean bypassCache) {
    this.logger.debug("count - START");
    CriteriaBuilder criteriaBuilder = this.entityManager.getCriteriaBuilder();
    CriteriaQuery<Long> criteriaQuery = criteriaBuilder.createQuery(Long.class);
    Root<T> root = criteriaQuery.from(this.entityClass);
    criteriaQuery.select(criteriaBuilder.count(root));
    // Create JPA Query
    Query query = this.entityManager.createQuery(criteriaQuery);
    if (bypassCache) {
        this.setBypassCahe(query);
    }
    // get the query results
    Long result = (Long) query.getSingleResult();
    this.logger.debug("count - END: " + result);
    return result;
}

From source file:com.adeptj.modules.data.jpa.core.AbstractJpaRepository.java

@Override
public <T extends BaseEntity> Long count(Class<T> entity) {
    EntityManager em = JpaUtil.createEntityManager(this.getEntityManagerFactory());
    try {/*  w  w w.  j  ava 2 s .c  o  m*/
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Long> cq = cb.createQuery(Long.class);
        return em.createQuery(cq.select(cb.count(cq.from(entity)))).getSingleResult();
    } catch (Exception ex) { // NOSONAR
        throw new JpaException(ex);
    } finally {
        JpaUtil.closeEntityManager(em);
    }
}