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:org.domdrides.hibernate.repository.HibernateRepository.java

License:Apache License

@Transactional(readOnly = true)
public int size() {
    return ((Number) createCriteria().setProjection(Projections.count("id")).uniqueResult()).intValue();
}

From source file:org.egov.pgr.service.ComplaintTypeService.java

License:Open Source License

/**
 * List top 5 complaint types filed in last one month
 *
 * @return complaint Type list//from ww w . j a  v  a  2s.  co  m
 */
@ReadOnly
public List<ComplaintType> getFrequentlyFiledComplaints() {

    DateTime previousDate = new DateTime();
    DateTime currentDate = new DateTime();
    previousDate = previousDate.minusMonths(1);

    Criteria criteria = entityManager.unwrap(Session.class).createCriteria(Complaint.class, "complaint");
    criteria.createAlias(COMPLAINT_COMPLAINT_TYPE, "compType");
    criteria.setProjection(Projections.projectionList().add(Projections.property(COMPLAINT_COMPLAINT_TYPE))
            .add(Projections.count(COMPLAINT_COMPLAINT_TYPE).as("count"))
            .add(Projections.groupProperty(COMPLAINT_COMPLAINT_TYPE)));
    criteria.add(Restrictions.between("complaint.createdDate", previousDate.toDate(), currentDate.toDate()));
    criteria.add(Restrictions.eq("compType.isActive", Boolean.TRUE));
    criteria.setMaxResults(5).addOrder(Order.desc("count"));
    List<Object> resultList = criteria.list();
    List<ComplaintType> complaintTypeList = new ArrayList<>();

    for (Object row : resultList) {
        Object[] columns = (Object[]) row;
        complaintTypeList.add((ComplaintType) columns[0]);
    }
    return complaintTypeList;

}

From source file:org.egov.ptis.actions.reports.BoundaryWisePropUsgeDelegate.java

License:Open Source License

public List getZoneList() {
    LOGGER.debug("Entered into getZoneList method");
    List zoneList = null;// w  ww .  j a v  a2 s.  co m
    Criterion criterion = null;
    Projection projection = Projections.projectionList().add(Projections.property("zone.id"))
            .add(Projections.property("propTypeMstrID.id")).add(Projections.sum("aggrArrDmd"))
            .add(Projections.sum("aggrCurrDmd")).add(Projections.count("basicPropertyID"))
            .add(Projections.groupProperty("zone.id")).add(Projections.groupProperty("propTypeMstrID"));
    Order order = Order.asc("zone.id");

    /*
     * Integer vacTypeId = getPropertyIdbyCode("OPEN_PLOT"); criterion =
     * Restrictions.ne("propTypeMstrID", vacTypeId);
     */
    zoneList = propertyDAO.getPropMaterlizeViewList(projection, criterion, order);
    LOGGER.debug("Zone list : " + (zoneList != null ? zoneList : ZERO));
    LOGGER.debug("Exit from getZoneList method");
    return zoneList;
}

From source file:org.egov.ptis.actions.reports.BoundaryWisePropUsgeDelegate.java

License:Open Source License

public List getTotPropUsage(Integer bndryNo) {
    LOGGER.debug("Entered into getTotPropUsage method");
    LOGGER.debug("Boundary Number : " + bndryNo);
    List wardList = null;/* w  w w. j  a  v a  2  s  .c  o  m*/
    Criterion criterion = null;
    Criterion vacantCrit = null;
    Conjunction conjun = Restrictions.conjunction();
    Projection projection = Projections.projectionList().add(Projections.property("propTypeMstrID.id"))
            .add(Projections.count("basicPropertyID")).add(Projections.sum("aggrArrDmd"))
            .add(Projections.sum("aggrCurrDmd")).add(Projections.groupProperty("propTypeMstrID"));
    if (bndryNo != null) {
        criterion = Restrictions.like("zone.id", bndryNo);
        conjun.add(criterion);
    }
    /*
     * Integer vacTypeId = getPropertyIdbyCode("OPEN_PLOT"); vacantCrit =
     * Restrictions.ne("propTypeMstrID", vacTypeId); conjun.add(vacantCrit);
     */
    wardList = propertyDAO.getPropMaterlizeViewList(projection, conjun, null);
    LOGGER.debug("Ward list : " + (wardList != null ? wardList : ZERO));
    LOGGER.debug("Exit from getTotPropUsage method");
    return wardList;
}

From source file:org.egov.ptis.actions.reports.BoundaryWisePropUsgeDelegate.java

License:Open Source License

public List getWardList(Integer zoneNo) {
    LOGGER.debug("Entered into getWardList method");
    LOGGER.debug("Zone Number : " + zoneNo);
    List wardList = null;/*from ww w . java  2  s .c o  m*/
    Conjunction conjun = Restrictions.conjunction();
    if (zoneNo > 0) {
        Criterion criterion = Restrictions.like("zone.id", zoneNo);
        conjun.add(criterion);
        Integer vacTypeId = getPropertyIdbyCode("OPEN_PLOT");
        // Criterion anothercriterion = Restrictions.ne("propTypeMstrID",
        // vacTypeId);
        // conjun.add(anothercriterion);

        Projection projection = Projections.projectionList().add(Projections.property("ward.id"))
                .add(Projections.property("propTypeMstrID.id")).add(Projections.sum("aggrArrDmd"))
                .add(Projections.sum("aggrCurrDmd")).add(Projections.count("basicPropertyID"))
                .add(Projections.groupProperty("ward.id")).add(Projections.groupProperty("propTypeMstrID"));
        Order order = Order.asc("ward.id");
        wardList = propertyDAO.getPropMaterlizeViewList(projection, conjun, order);
    }
    LOGGER.debug("Ward list : " + (wardList != null ? wardList : ZERO));
    LOGGER.debug("Exit from getWardList method");
    return wardList;
}

From source file:org.eurocarbdb.dataaccess.indexes.IndexByMostEvidence.java

License:Open Source License

public void apply(Criteria query) {
    /*  //from   ww  w . j av  a  2s . c o m
    *   the query for this index requires a horrible hibernate hack.
    *   basically, the incoming Criteria query depends on an SQL 
    *   'group by', which, due to Hibernate limitations, screws over 
    *   the select clause. So, we attach a results transformer to the
    *   query that post-processes the result set from a list of 
    *   GlycanSequence ids into GlycanSequence objects through a
    *   separate query.
    */
    query.createAlias("glycanEvidence", "gs2ev", LEFT_JOIN)
            .setProjection(Projections.projectionList().add(Projections.groupProperty("glycanSequenceId"))
                    .add(Projections.count("gs2ev.evidence").as("ev")))
            .addOrder(Order.desc("ev")).setResultTransformer(new PassThroughResultTransformer() {
                public List transformList(List results) {
                    if (results == null || results.size() == 0)
                        return results;

                    List<Object[]> rows = (List<Object[]>) results;

                    //  the lookup hash
                    Map<Integer, GlycanSequence> hash = new HashMap<Integer, GlycanSequence>(results.size());

                    //  gather the sequence ids 
                    List<Integer> ids = new ArrayList<Integer>(results.size());
                    for (Object[] columns : rows)
                        ids.add((Integer) columns[0]);

                    //  look them up
                    List<GlycanSequence> seqs = (List<GlycanSequence>) getEntityManager()
                            .createQuery(GlycanSequence.class).add(Restrictions.in("glycanSequenceId", ids))
                            .setFetchSize(ids.size())
                            // .setCacheable( true )
                            .list();

                    for (GlycanSequence seq : seqs)
                        hash.put(seq.getGlycanSequenceId(), seq);

                    seqs.clear();

                    for (Integer id : ids)
                        seqs.add(hash.get(id));

                    return seqs;
                }
                /*
                    public Object transformTuple( Object[] tuple, String[] aliases )
                    {
                        int id = (Integer) tuple[0];
                        return getEntityManager().lookup( GlycanSequence.class, id );
                    }
                */
            });

    /* // attempt 2
            query
    .createAlias("glycanEvidence", "gs2ev", LEFT_JOIN ) 
    .addOrder( 
        new Order( "irrelevant", false ) 
        {
            public final String toSqlString( Criteria c, CriteriaQuery q )
            {
                // return "count( gs2ev.evidence_id ) desc";       
                return "count( "
                    + "gs2ev1_"//q.getSQLAlias( query, "glycanEvidence" )
                    + ".evidence_id ) desc";       
            }
        }
    )
            ;
    */
    /* // attempt 3
            query
    .createAlias("glycanEvidence", "gs2ev", LEFT_JOIN )  
    .setProjection( 
        Projections.groupProperty("glycanSequenceId").as("glycanSequenceId")
    )
    .addOrder( 
        new Order( "irrelevant", false ) 
        {
            public final String toSqlString( Criteria c, CriteriaQuery q )
            {
                // return "count( gs2ev.evidence_id ) desc";       
                return "count( "
                    + "gs2ev1_"//q.getSQLAlias( query, "glycanEvidence" )
                    + ".evidence_id ) desc";       
            }
        }
    )
    .setResultTransformer( 
        org.hibernate.transform.Transformers.aliasToBean( 
            GlycanSequence.class ) ) 
            ;
    */
}

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

License:Apache License

private void addStatProjection(Criteria criteria) throws PersistenceException {
    ProjectionList projList = Projections.projectionList();
    projList.add(Projections.rowCount());
    for (ColumnStat<T> column : statResult) {
        if (column.isCountNotNullFlag()) {
            projList.add(Projections.count(column.getColumn().getName()));
        }/*w  w w.ja  va  2 s. co m*/
        if (column.isMinFlag()) {
            projList.add(Projections.min(column.getColumn().getName()));
        }
        if (column.isMaxFlag()) {
            projList.add(Projections.max(column.getColumn().getName()));
        }
        if (column.isAverageFlag()) {
            projList.add(Projections.avg(column.getColumn().getName()));
        }
        if (column.isSumFlag()) {
            projList.add(Projections.sum(column.getColumn().getName()));
        }
    }

    criteria.setProjection(projList);
}

From source file:org.gaixie.micrite.car.dao.hibernate.CarfileDAOImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
public List findCSGroupByTelVague(SearchBean[] queryBean) {
    DetachedCriteria criteria = SearchFactory.generateCriteria(Carfile.class, queryBean);
    criteria.add(Expression.eq("status", ICarfileService.STATUS_NORMAL));
    criteria.addOrder(Order.desc("editDate"));
    criteria.createAlias("carType", "cs");
    criteria.setProjection(Projections.projectionList().add(Projections.count("cs.name"))
            .add(Projections.groupProperty("cs.name")));
    List<Carfile> list = getHibernateTemplate().findByCriteria(criteria);
    //        desidedExpiredFlag(list);
    return list;//from  ww w  .j  a  va 2 s  . c  o m
}

From source file:org.gaixie.micrite.crm.dao.hibernate.CustomerDAOImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
public List findCSGroupByTelVague(SearchBean[] queryBean) {
    DetachedCriteria criteria = SearchFactory.generateCriteria(Carowner.class, queryBean);
    criteria.createAlias("customerSource", "cs");
    criteria.setProjection(Projections.projectionList().add(Projections.count("cs.name"))
            .add(Projections.groupProperty("cs.name")));
    return getHibernateTemplate().findByCriteria(criteria);
}

From source file:org.gaixie.micrite.enterprise.dao.hibernate.EnterpriseDAOImpl.java

@SuppressWarnings("unchecked")
public List findCSGroupByTelVague(SearchBean[] queryBean) {
    DetachedCriteria criteria = SearchFactory.generateCriteria(Enterprise.class, queryBean);
    criteria.add(Restrictions.ne("id", SYS_RECORD_ID));
    criteria.createAlias("qualification", "cs");
    criteria.setProjection(Projections.projectionList().add(Projections.count("cs.name"))
            .add(Projections.groupProperty("cs.name")));
    return getHibernateTemplate().findByCriteria(criteria);
}