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.greendot.db.jpa.core.AbstractSearchDao.java

@Transactional(readOnly = true)
public long count() {

    final CriteriaBuilder criteriaBuilder = getEntityManager().getCriteriaBuilder();
    final CriteriaQuery<Long> rootQuery = criteriaBuilder.createQuery(Long.class);
    rootQuery.select(criteriaBuilder.count(rootQuery.from(getEntityType())));

    return getEntityManager().createQuery(rootQuery).getSingleResult();
}

From source file:ispok.dao.TournamentHibernateJpaDao.java

@Override
public Long getCount(Map<String, Object> filters) {

    EntityManager em = getEntityManager();

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> cq = cb.createQuery(Long.class);
    Root<Tournament> t = cq.from(Tournament.class);

    cq.select(cb.count(t));

    TypedQuery<Long> tq = em.createQuery(cq);
    return tq.getSingleResult();
}

From source file:com.sixsq.slipstream.persistence.Run.java

public static int viewListCount(User user, String moduleResourceUri, String cloudServiceName)
        throws ConfigurationException, ValidationException {
    int count = 0;
    EntityManager em = PersistenceUtil.createEntityManager();
    try {//from w  w w.  j a  v  a2  s. c  o  m
        CriteriaBuilder builder = em.getCriteriaBuilder();
        CriteriaQuery<Long> critQuery = builder.createQuery(Long.class);
        Root<Run> rootQuery = critQuery.from(Run.class);
        critQuery.select(builder.count(rootQuery));
        Predicate where = viewListCommonQueryOptions(builder, rootQuery, user, moduleResourceUri,
                cloudServiceName);
        if (where != null) {
            critQuery.where(where);
        }
        TypedQuery<Long> query = em.createQuery(critQuery);
        count = (int) (long) query.getSingleResult();
    } finally {
        em.close();
    }
    return count;
}

From source file:de.hopmann.repositories.cran.service.CRANPackageListingService.java

public int getPackageListingCount() {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<Number> query = cb.createQuery(Number.class);
    query.select(cb.count(query.from(CRANPackageEntity.class)));
    return entityManager.createQuery(query).getSingleResult().intValue();
}

From source file:com.aimdek.ccm.dao.impl.BulkUploadRepositoryImpl.java

/**
 * {@inheritDoc}//  w w  w .j a v  a  2 s .  c o m
 */
public long getBulkUploadsCount(Map<String, Object> filters) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Long> query = builder.createQuery(Long.class);
    Root<BulkUpload> root = query.from(BulkUpload.class);
    query.select(builder.count(root));
    addFilterCriteria(filters, builder, root, query);

    return entityManager.createQuery(query).getSingleResult();
}

From source file:com.aimdek.ccm.dao.impl.UserRepositoryImpl.java

/**
 * {@inheritDoc}/*  ww w.  jav a2 s .  c  om*/
 */
public long getCustomersCount(Map<String, Object> filters) {

    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Long> query = builder.createQuery(Long.class);
    Root<User> root = query.from(User.class);
    query.select(builder.count(root));
    query.where(builder.equal(root.<String>get(FIELDCONSTANT_USER_ROLE), ROLE_CUSTOMER));
    this.addFilterCriteria(filters, builder, root, query);
    return entityManager.createQuery(query).getSingleResult();
}

From source file:com.aimdek.ccm.dao.impl.CreditCardRepositoryImpl.java

/**
 * {@inheritDoc}/* w ww .ja  v  a 2s.co  m*/
 */
public long getCreditCardsCount(Map<String, Object> filters) {

    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Long> query = builder.createQuery(Long.class);
    Root<CreditCard> root = query.from(CreditCard.class);
    query.select(builder.count(root));
    addFilterCriteria(filters, builder, root, query);

    return entityManager.createQuery(query).getSingleResult();
}

From source file:com.aimdek.ccm.dao.impl.CreditCardRepositoryImpl.java

/**
 * {@inheritDoc}//w w  w  .j  av  a2s .  c  om
 */
public long getCustomerCreditCardsCount(long userId) {

    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Long> query = builder.createQuery(Long.class);
    Root<CreditCard> root = query.from(CreditCard.class);
    query.select(builder.count(root));

    if (CommonUtil.isNotNull(userId)) {
        User user = userRepository.findById(userId);
        if (CommonUtil.isNotNull(user) && user.getRole().equals(ROLE_CUSTOMER)) {
            query.where(builder.equal(root.<Long>get(FIELDCONSTANT_CARDHOLDERID), user.getId()));
        }
    }
    return entityManager.createQuery(query).getSingleResult();
}

From source file:com.netflix.genie.server.metrics.impl.JobCountManagerImpl.java

/**
 * {@inheritDoc}//  w  w w .j a  v  a2 s  .  c  om
 */
@Override
@Transactional(readOnly = true)
//TODO: Move to specification
public int getNumInstanceJobs(final String hostName, final Long minStartTime, final Long maxStartTime)
        throws GenieException {
    LOG.debug("called");

    final String finalHostName;
    // initialize host name
    if (StringUtils.isBlank(hostName)) {
        finalHostName = NetUtil.getHostName();
    } else {
        finalHostName = hostName;
    }
    final CriteriaBuilder cb = this.em.getCriteriaBuilder();
    final CriteriaQuery<Long> cq = cb.createQuery(Long.class);
    final Root<Job> j = cq.from(Job.class);
    cq.select(cb.count(j));
    final Predicate runningStatus = cb.equal(j.get(Job_.status), JobStatus.RUNNING);
    final Predicate initStatus = cb.equal(j.get(Job_.status), JobStatus.INIT);
    final List<Predicate> predicates = new ArrayList<>();
    predicates.add(cb.equal(j.get(Job_.hostName), finalHostName));
    predicates.add(cb.or(runningStatus, initStatus));
    if (minStartTime != null) {
        predicates.add(cb.greaterThanOrEqualTo(j.get(Job_.started), new Date(minStartTime)));
    }
    if (maxStartTime != null) {
        predicates.add(cb.lessThan(j.get(Job_.started), new Date(maxStartTime)));
    }
    //documentation says that by default predicate array is conjuncted together
    cq.where(predicates.toArray(new Predicate[predicates.size()]));
    final TypedQuery<Long> query = this.em.createQuery(cq);
    //Downgrading to an int since all the code seems to want ints
    //Don't feel like changing everthing right now can figure out later
    //if need be
    return query.getSingleResult().intValue();
}

From source file:com.june.app.board.repository.jpa.BoardRepositoryImpl.java

@Override
public long boardListCnt(Board vo) {

    CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();

    int bbsId = vo.getBbsId();
    CriteriaQuery<Long> countQuery = criteriaBuilder.createQuery(Long.class);
    Root<Board> from = countQuery.from(Board.class);
    countQuery.select(criteriaBuilder.count(from));
    if (bbsId > 0) {
        countQuery.where(criteriaBuilder.equal(from.get("bbsId"), bbsId));
    }//from  ww w  .j  a va2  s  .  com
    Long count = em.createQuery(countQuery).getSingleResult();

    return count;

}