Example usage for org.hibernate.criterion Projections count

List of usage examples for org.hibernate.criterion Projections count

Introduction

In this page you can find the example usage for org.hibernate.criterion Projections count.

Prototype

public static CountProjection count(String propertyName) 

Source Link

Document

A property value count projection

Usage

From source file:com.hypersocket.repository.AbstractRepositoryImpl.java

License:Open Source License

@Override
public List<?> getCounts(Class<?> clz, String groupBy, CriteriaConfiguration... configs) {

    Criteria criteria = createCriteria(clz);

    for (CriteriaConfiguration c : configs) {
        c.configure(criteria);// ww w.j  a  v  a  2s  .  c om
    }

    criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    criteria.setProjection(Projections.projectionList().add(Projections.groupProperty(groupBy))
            .add(Projections.count(groupBy)));

    return criteria.list();
}

From source file:com.jaspersoft.jasperserver.api.metadata.tenant.service.impl.TenantServiceImpl.java

License:Open Source License

private int createNumberOfUsersOrRolesCriteria(String tenantId, Class aClass) {
    Integer rowCount = 0;//from w  ww.  ja v a2s.co m

    RepoTenant tenant = getRepoTenant(tenantId, false);
    if (tenant != null) {
        DetachedCriteria criteria = DetachedCriteria.forClass(aClass);
        criteria.createAlias("tenant", "t");

        criteria.add(Restrictions.or(Restrictions.eq("t.tenantUri", tenant.getTenantUri()), Restrictions
                .like("t.tenantUri", tenant.getTenantUri().equals("/") ? "/%" : tenant.getTenantUri() + "/%")));

        criteria.setProjection(Projections.count("id"));
        criteria.getExecutableCriteria(getSession()).setCacheable(true);

        List results = getHibernateTemplate().findByCriteria(criteria);
        if (results != null && !results.isEmpty()) {

            rowCount = (Integer) results.get(0);
        }
    }

    return rowCount;
}

From source file:com.jaspersoft.jasperserver.api.metadata.user.service.impl.UserAuthorityServiceImpl.java

License:Open Source License

@Transactional(propagation = Propagation.REQUIRED)
public int getUsersCountExceptExcluded(ExecutionContext executionContext, final Set<String> excludedUserNames,
        final boolean excludeDisabledUsers) {
    return (Integer) getHibernateTemplate().execute(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            Criteria criteria = session.createCriteria(getPersistentUserClass());
            if (excludedUserNames != null && excludedUserNames.size() > 0) {
                criteria.add(Restrictions.not(Restrictions.in("username", excludedUserNames)));
            }//from   ww  w .j  a  va  2 s. co  m
            if (excludeDisabledUsers) {
                criteria.add(Restrictions.eq("enabled", true));
            }
            criteria.setProjection(Projections.count("id"));
            return criteria.uniqueResult();
        }
    });
}

From source file:com.jaspersoft.jasperserver.search.sorter.ByPopularitySorter.java

License:Open Source License

@Override
protected void addProjection(String type, ExecutionContext context, SearchCriteria criteria) {
    criteria.addProjection(Projections.projectionList().add(Projections.count("accessEvents"), "aec")
            .add(Projections.groupProperty("accessEvents")));

    criteria.addOrder(Order.desc("aec")).addOrder(Order.asc("id"));
}

From source file:com.liferay.portal.dao.orm.hibernate.ProjectionFactoryImpl.java

License:Open Source License

public Projection count(String propertyName) {
    return new ProjectionImpl(Projections.count(propertyName));
}

From source file:com.mac.green_leaves.v1.master.MasterRepository.java

public int totalItems(String keyword, Integer branch, Class modelClass) {
    Session session = getSession();//ww  w. ja v a2s .  co  m

    Criteria criteria = session.createCriteria(modelClass);
    if (keyword != null) {
        Field[] fields = modelClass.getDeclaredFields();
        ArrayList<Criterion> criterions = new ArrayList<>();
        for (Field field : fields) {
            if (field.getType().isAssignableFrom(String.class)) {
                criterions.add(Restrictions.ilike(field.getName(), keyword, MatchMode.ANYWHERE));
            }
        }
        criteria.add(Restrictions.or(criterions.toArray(new Criterion[] {})));
    }

    criteria.add(Restrictions.eq("branch", branch));

    criteria.setProjection(Projections.count("indexNo"));
    Integer totalItems = ((Long) criteria.uniqueResult()).intValue();
    return totalItems;
}

From source file:com.painiu.core.dao.hibernate.PhotoDAOHibernate.java

License:Open Source License

static Criteria buildCountPhotoCriteria(final Session session, User user, String[] tags, boolean taggedAll,
        String text, Relation relation) {
    //Criteria criteria = buildPhotoCriteria(session, user, group, tags, taggedAll, text, relation, true);
    Criteria criteria = buildPhotoCriteria(session, user, tags, taggedAll, text, relation, true);
    if ((tags != null && tags.length > 1) || text != null) {
        return criteria.setProjection(Projections.countDistinct("id"));
    }/*ww  w .ja  v a2  s. com*/
    return criteria.setProjection(Projections.count("id"));
}

From source file:com.qcadoo.model.api.search.SearchProjections.java

License:Open Source License

/**
 * Creates projection which add given field to the "GROUP BY" clause and its "count" to the "SELECT" clause.
 * //from  ww w .  j  ava 2 s.  c  o  m
 * @param field
 *            field
 * @return projection
 */
public static SearchProjection count(final String field) {
    return new SearchProjectionImpl(Projections.count(field));
}

From source file:com.reignite.query.StructuredQuery.java

License:Open Source License

public void addAggregate(QueryType type, String field) throws ParserException {
    switch (type) {
    case AVG://from ww  w .  j av a  2  s .c  o m
        aggregates.put("avg(" + field + ")", Projections.avg(field));
        break;
    case COUNT:
        aggregates.put("count(" + field + ")", Projections.count(field));
        break;
    case SUM:
        aggregates.put("sum(" + field + ")", Projections.sum(field));
        break;
    default:
        throw new ParserException("Invalid aggregate type: " + type.name());
    }
}

From source file:com.romeikat.datamessie.core.base.dao.impl.AbstractEntityDao.java

License:Open Source License

/**
 * Counts all entities./*from www.j  a  v a  2  s  .c o m*/
 *
 * @param ssc
 * @return
 */
@Override
public long countAll(final SharedSessionContract ssc) {
    // Query
    final Criteria criteria = ssc.createCriteria(entityClass);
    // Projection
    criteria.setProjection(Projections.count("id"));
    // Done
    Long count = (Long) criteria.uniqueResult();
    if (count == null) {
        count = 0l;
    }
    return count;
}