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:com.creditcloud.common.entities.dao.AbstractReadDAO.java

/**
 * count all entity/*from  ww w . j a v a 2s.  co  m*/
 *
 * @return
 */
public int count() {
    EntityManager em = getEntityManager();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery cq = cb.createQuery();
    Root<T> rt = cq.from(entityClass);
    cq.select(cb.count(rt));
    Query q = em.createQuery(cq);
    Object result = q.getSingleResult();
    return result == null ? 0 : ((Long) result).intValue();
}

From source file:org.jnap.core.persistence.jpa.DaoSupport.java

@Override
public Long countAll() {
    CriteriaBuilder builder = getCriteriaBuilder();
    CriteriaQuery<Long> count = builder.createQuery(Long.class);
    count.select(builder.count(count.from(getEntityClass())));
    return entityManager.createQuery(count).getSingleResult();
}

From source file:name.marcelomorales.siqisiqi.openjpa.impl.OrmFinderImpl.java

protected Expression<Long> newCountExpression(CriteriaBuilder cb, Root<T> from) {
    final Path<PK> countPath = settings.countPath(from, persistentClass);
    if (countPath == null) {
        throw new IllegalStateException("Implements countPath en " + persistentClass);
    }//from www  .ja  v a 2 s.  co  m
    return cb.count(countPath);
}

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

/**
 * Get count of requirement rules/*  w w w.java2  s.co m*/
 * 
 * @return count
 */
private Long getRequirementRuleCount() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> c = cb.createQuery(Long.class);
    c.select(cb.count(c.from(RequirementRule.class)));
    return em.createQuery(c).getSingleResult();
}

From source file:ca.uhn.fhir.jpa.dao.BaseHapiFhirSystemDao.java

@Override
public Map<String, Long> getResourceCounts() {
    CriteriaBuilder builder = myEntityManager.getCriteriaBuilder();
    CriteriaQuery<Tuple> cq = builder.createTupleQuery();
    Root<?> from = cq.from(ResourceTable.class);
    cq.multiselect(from.get("myResourceType").as(String.class),
            builder.count(from.get("myResourceType")).as(Long.class));
    cq.groupBy(from.get("myResourceType"));

    TypedQuery<Tuple> q = myEntityManager.createQuery(cq);

    Map<String, Long> retVal = new HashMap<String, Long>();
    for (Tuple next : q.getResultList()) {
        String resourceName = next.get(0, String.class);
        Long count = next.get(1, Long.class);
        retVal.put(resourceName, count);
    }/*from  w w w.j  a  v a  2  s.  c o m*/
    return retVal;
}

From source file:org.businessmanager.dao.GenericDaoImpl.java

@Override
public Long getCount(Map<SingularAttribute<T, ?>, Object> filterAttributes, boolean enableLikeSearch) {
    CriteriaBuilder queryBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Long> criteriaQuery = queryBuilder.createQuery(Long.class);
    Root<T> rootQuery = criteriaQuery.from(getPersistenceClass());
    CriteriaQuery<Long> select = criteriaQuery.select(queryBuilder.count(rootQuery));

    List<Predicate> predicateList = createFilterList(filterAttributes, enableLikeSearch, queryBuilder,
            rootQuery);/* ww  w .j a  va 2 s.c  o m*/
    criteriaQuery.where(predicateList.toArray(new Predicate[0]));

    TypedQuery<Long> typedQuery = entityManager.createQuery(select);
    return typedQuery.getSingleResult();
}

From source file:com.creditcloud.common.entities.dao.AbstractReadDAO.java

/**
 * count entity by ParamInfo//from   w  w  w  . j a v a  2 s  .c o m
 *
 * @param paramInfo
 * @return
 */
public int count(ParamInfo paramInfo) {
    EntityManager em = getEntityManager();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery cq = cb.createQuery(entityClass);
    Root<T> userRoot = cq.from(entityClass);
    cq.select(cb.count(userRoot));

    //build query for paramInfo
    if (paramInfo != null) {
        Set<Predicate> andCriteria = new HashSet();
        Set<Predicate> orCriteria = new HashSet();

        for (ParamItem item : paramInfo.getParamItems()) {
            Predicate predicate;
            if (item.getValue() instanceof String) {
                //fuzy search for string
                String regExp = "%" + item.getValue() + "%";
                predicate = cb.like((Expression) (userRoot.get(item.getFieldName())), regExp);
            } else {
                predicate = cb.equal((userRoot.get(item.getFieldName())), item.getValue());
            }

            switch (item.getOperator()) {
            case AND:
                andCriteria.add(predicate);
                break;
            case OR:
                orCriteria.add(predicate);
                break;
            }
        }
        if (orCriteria.size() > 0) {
            Predicate or = cb.or(orCriteria.toArray(new Predicate[orCriteria.size()]));
            andCriteria.add(or);
        }
        if (andCriteria.size() > 0) {
            Predicate and = cb.and(andCriteria.toArray(new Predicate[andCriteria.size()]));
            cq.where(and);
        }
    }

    TypedQuery<Long> query = em.createQuery(cq);
    Long result = query.getSingleResult();
    return result == null ? 0 : result.intValue();
}

From source file:net.awired.generic.jpa.dao.impl.GenericDaoImpl.java

public Long findFilteredCount(String search, List<String> searchProperties) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Long> query = builder.createQuery(Long.class);
    Root<ENTITY> from = query.from(entityClass);
    query.select(builder.count(from));
    if (!Strings.isNullOrEmpty(search)) {
        Predicate[] buildFilterPredicates = BuildFilterPredicates(from, search, searchProperties);
        if (buildFilterPredicates.length > 0) {
            query.where(builder.or(buildFilterPredicates));
        }/*ww  w .j a v  a 2 s .  c  o m*/
    }
    return count(entityManager.createQuery(query));
}

From source file:org.jnap.core.persistence.jpa.DaoSupport.java

@Override
public boolean exists(Serializable id) {
    CriteriaBuilder builder = getCriteriaBuilder();
    CriteriaQuery<Long> count = builder.createQuery(Long.class);
    Root<?> root = count.from(getEntityClass());
    count.select(builder.count(root));
    count.where(builder.equal(root.get("id"), id));
    return entityManager.createQuery(count).getSingleResult() == 1;
}

From source file:org.synyx.hades.dao.orm.GenericJpaDao.java

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

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

    Root<T> root = applySpecificationToCriteria(spec, query);
    query.select(builder.count(root)).distinct(true);

    return getEntityManager().createQuery(query);
}