Example usage for org.hibernate.criterion Projections countDistinct

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

Introduction

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

Prototype

public static CountProjection countDistinct(String propertyName) 

Source Link

Document

A distinct property value count projection

Usage

From source file:edu.utah.further.core.data.hibernate.query.CustomProjections.java

License:Apache License

/**
 * Visits a collection expression.//from w w w  . java2s. c o  m
 */
public static Projection countProjection(final String propertyName, final boolean distinct) {
    return distinct ? Projections.countDistinct(propertyName) : Projections.count(propertyName);
}

From source file:edu.utah.further.ds.impl.executor.db.hibernate.criteria.HibernateCountSearchQueryExecutor.java

License:Apache License

@SuppressWarnings("boxing")
@Override/*from  ww  w  . j  a v  a2 s. c om*/
public boolean process(final ChainRequest request) {
    final HibernateExecReq execReq = new HibernateExecReq(request);
    final GenericCriteria criteria = execReq.getResult();
    notNull(criteria, "Expected Hibernate criteria");

    final Class<? extends PersistentEntity<?>> domainClass = execReq.getRootEntity();

    final SessionFactory sessionFactory = execReq.getSessionFactory();
    notNull(sessionFactory, "Expected SessionFactory");

    // Get information about the root entity class
    final ClassMetadata classMetadata = sessionFactory.getClassMetadata(domainClass);
    final String identifierName = classMetadata.getIdentifierPropertyName();
    final Type identifierType = classMetadata.getIdentifierType();

    if (identifierType.isComponentType()) {
        criteria.setProjection(Projections.countDistinct(identifierName + "." + identifierName));
    } else {
        criteria.setProjection(Projections.countDistinct(identifierName));
    }
    Long result = -1l;
    try {
        result = criteria.uniqueResult();
    } catch (final HibernateException e) {
        if (log.isDebugEnabled()) {
            log.debug("Caught Hibernate exception.");
        }
    }

    execReq.setResult(result);
    execReq.setStatus("Returned search query count result");

    return false;
}

From source file:grails.orm.HibernateCriteriaBuilder.java

License:Apache License

/**
 * Adds a projection that allows the criteria to return the distinct property count
 *
 * @param propertyName The name of the property
 * @param alias The alias to use/*from   www.  j  a  v a 2 s  .co m*/
 */
public org.grails.datastore.mapping.query.api.Projections countDistinct(String propertyName, String alias) {
    final CountProjection proj = Projections.countDistinct(calculatePropertyName(propertyName));
    addProjectionToList(proj, alias);
    return this;
}

From source file:net.longfalcon.newsj.persistence.hibernate.UserDAOImpl.java

License:Open Source License

@Override
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public long countUsers() {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(User.class);
    criteria.setProjection(Projections.countDistinct("id"));

    return (long) criteria.uniqueResult();
}

From source file:org.fornax.cartridges.sculptor.framework.accessimpl.jpahibernate.JpaHibFindByConditionAccessImpl.java

License:Apache License

public void executeCount() {
    final Criteria criteria = createCriteria();
    prepareCache(criteria);/*from  w  ww .  j a  v  a2 s .c o m*/

    // Prepare where clause
    addSubCriterias(criteria);
    addConditionalCriteria(criteria);

    addResultTransformer(criteria);

    criteria.setProjection(Projections.countDistinct(Criteria.ROOT_ALIAS + ".id"));
    rowCount = (Integer) criteria.uniqueResult();
}

From source file:org.generationcp.middleware.dao.GermplasmListDAO.java

License:Open Source License

public long countByGID(final Integer gid) {
    try {//from  w w  w.ja v a 2 s .c o m
        if (gid != null) {
            final Criteria criteria = this.getSession().createCriteria(GermplasmListData.class);
            criteria.createAlias("list", "l");
            criteria.add(Restrictions.eq("gid", gid));
            criteria.add(Restrictions.ne("l.status", GermplasmListDAO.STATUS_DELETED));
            criteria.setProjection(Projections.countDistinct("l.id"));
            return ((Long) criteria.uniqueResult()).longValue();
        }
    } catch (final HibernateException e) {
        final String errorMessage = "Error with countByGID(gid=" + gid + ") query from GermplasmList "
                + e.getMessage();
        GermplasmListDAO.LOG.error(errorMessage);
        throw new MiddlewareQueryException(errorMessage, e);
    }
    return 0;
}

From source file:org.generationcp.middleware.dao.GermplasmListDAO.java

License:Open Source License

public long countByGIDandProgramUUID(final Integer gid, final String programUUID) {
    try {/*w  w w  . j av a2s . c o m*/
        if (gid != null) {
            final Criteria criteria = this.getSession().createCriteria(GermplasmList.class, "germplasmList");
            final DetachedCriteria gidCriteria = DetachedCriteria.forClass(GermplasmListData.class, "listData");
            gidCriteria.add(Restrictions.eq("listData.gid", gid));
            gidCriteria.add(Property.forName("germplasmList.id").eqProperty("listData.list.id"));
            criteria.add(Subqueries.exists(gidCriteria.setProjection(Projections.property("listData.gid"))));
            criteria.add(Restrictions.ne(GermplasmListDAO.STATUS, GermplasmListDAO.STATUS_DELETED));
            this.addCriteriaForProgramUUIDInLists(programUUID, criteria);
            this.hideSnapshotListTypes(criteria);
            criteria.setProjection(Projections.countDistinct("id"));
            return ((Long) criteria.uniqueResult()).longValue(); // count
        }
    } catch (final HibernateException e) {
        final String errorMessage = "Error with countByGIDandProgramUUID(gid=" + gid + ",programUUID="
                + programUUID + ") query from GermplasmList: " + e.getMessage();
        GermplasmListDAO.LOG.error(errorMessage);
        throw new MiddlewareQueryException(errorMessage, e);
    }
    return 0;
}

From source file:org.generationcp.middleware.dao.OindexDAO.java

License:Open Source License

public long countOunitIDsByRepresentationId(Integer representationId) throws MiddlewareQueryException {
    try {//  ww  w  .ja  v a  2s  .c o  m
        Criteria criteria = getSession().createCriteria(Oindex.class);
        criteria.add(Restrictions.eq("representationNumber", representationId));

        criteria.setProjection(Projections.countDistinct("observationUnitId"));

        Long ounitIdCount = (Long) criteria.uniqueResult();

        return ounitIdCount;
    } catch (HibernateException e) {
        throw new MiddlewareQueryException("Error with countOunitIDsByRepresentationId(representationId="
                + representationId + ") query from Oindex: " + e.getMessage(), e);
    }
}

From source file:org.generationcp.middleware.dao.StudyDAO.java

License:Open Source License

public long countAllTopLevelStudies() throws MiddlewareQueryException {
    try {//from  ww  w.  j ava 2s  .c o m
        Criteria criteria = getSession().createCriteria(Study.class);
        // top level studies are studies without parent folders (shierarchy = 0)
        criteria.add(Restrictions.eq("hierarchy", Integer.valueOf(0)));
        criteria.setProjection(Projections.countDistinct("id"));
        return ((Long) criteria.uniqueResult()).longValue();
    } catch (HibernateException e) {
        throw new MiddlewareQueryException(
                "Error with countAllTopLevelStudies() query from Study: " + e.getMessage(), e);
    }
}

From source file:org.generationcp.middleware.dao.StudyDAO.java

License:Open Source License

public long countAllStudyByParentFolderID(Integer parentFolderId) throws MiddlewareQueryException {
    try {//ww w . j  a  va  2 s .c  o m
        Criteria criteria = getSession().createCriteria(Study.class);
        // top level studies are studies without parent folders (shierarchy = 0)
        criteria.add(Restrictions.eq("hierarchy", parentFolderId));
        criteria.setProjection(Projections.countDistinct("id"));
        return ((Long) criteria.uniqueResult()).longValue();
    } catch (HibernateException e) {
        throw new MiddlewareQueryException("Error with countAllStudyByParentFolderID(parentFolderId="
                + parentFolderId + ") query from Study: " + e.getMessage(), e);
    }
}