Example usage for org.hibernate.criterion Projections rowCount

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

Introduction

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

Prototype

public static Projection rowCount() 

Source Link

Document

The query row count, ie.

Usage

From source file:com.heliosapm.aa4h.parser.XMLQueryParser.java

License:Apache License

/**
 * Initializes a Criteria Query.//from   w  ww . jav  a2s  . co  m
 * Mandatory Attributes:<ul>
 * <li><b>name</b>: The unqualified class name driving the criteria query.</li>
 * </ul>
 * Optional Attributes:<ul>
 * <li><b>prefix</b>: The package name of the class driving the criteria query. If null, no package is assumed.</li>
 * <li><b>maxSize</b>: The maximum number of rows to return from the database.</li>
 * <li><b>fetchSize</b>: The number of rows to fetch when rows are requested. Usually not useful for AA4H.</li>
 * <li><b>cacheEnabled</b>: Enables or disables caching for the queried objects.</li>
 * <li><b>cacheMode</b>: The cache options for the queried objects.</li>
 * <li><b>flushMode</b>: The session flush options.</li>
 * <li><b>fetchMode</b>: The collection fetch options for the query.</li>
 * <li><b>lockMode</b>: The row lock options for the queried rows.</li>
 * <li><b>timeOut</b>: The query timeout option.</li>
 * <li><b>rowCountOnly</b>: Returns a count of the query rows only.</li>
 * </ul>
 * @param attrs The attributes of the processed node.
 * @return An appended or new CriteriaSpecification
 * @throws SAXException
 */
protected CriteriaSpecification processCriteria(Attributes attrs) throws SAXException {
    if (inDetached) {
        return criteriaStack.peek();
    }
    String name = attrs.getValue("name");
    String prefix = attrs.getValue("prefix");
    if (prefix != null) {
        className = prefix + "." + name;
    } else {
        className = name;
    }
    String maxSize = attrs.getValue("maxSize");
    String fetchSize = attrs.getValue("fetchSize");
    String firstResult = attrs.getValue("firstResult");
    String cacheEnabled = attrs.getValue("cacheEnabled");
    String cacheMode = attrs.getValue("cacheMode");
    String flushMode = attrs.getValue("flushMode");
    String fetchMode = attrs.getValue("fetchMode");
    String lockMode = attrs.getValue("lockMode");
    String timeOut = attrs.getValue("timeOut");
    String rowCountOnly = attrs.getValue("rowCountOnly");
    Criteria newCriteria = null;
    try {
        if (criteriaStack.size() == 0) {
            newCriteria = session.createCriteria(className);
        } else {
            newCriteria = ((Criteria) criteriaStack.peek()).createCriteria(className);
        }
        criteriaStack.push(newCriteria);
        if ("true".equalsIgnoreCase(rowCountOnly)) {
            newCriteria.setProjection(Projections.projectionList().add(Projections.rowCount())

            );
            setRowCountOnly(true);
        }
        if (maxSize != null && isRowCountOnly() == false) {
            newCriteria.setMaxResults(Integer.parseInt(maxSize));
        }
        if (fetchSize != null && isRowCountOnly() == false) {
            newCriteria.setFetchSize(Integer.parseInt(fetchSize));
        }
        if (firstResult != null && isRowCountOnly() == false) {
            newCriteria.setFirstResult(Integer.parseInt(firstResult));
        }
        if (timeOut != null) {
            newCriteria.setTimeout(Integer.parseInt(timeOut));
        }

        if ("true".equalsIgnoreCase(cacheEnabled)) {
            newCriteria.setCacheable(true);
        } else if ("false".equalsIgnoreCase(cacheEnabled)) {
            newCriteria.setCacheable(false);
        }
        if (fetchMode != null && fetchMode.length() > 0) {
            if ("JOIN".equalsIgnoreCase(fetchMode)) {
                newCriteria.setFetchMode(name, FetchMode.JOIN);
            } else if ("SELECT".equalsIgnoreCase(fetchMode)) {
                newCriteria.setFetchMode(name, FetchMode.SELECT);
            } else {
                newCriteria.setFetchMode(name, FetchMode.DEFAULT);
            }
        } else {
            newCriteria.setFetchMode(name, FetchMode.DEFAULT);
        }
        if (cacheMode != null && cacheMode.length() > 0) {
            if ("GET".equalsIgnoreCase(cacheMode)) {
                newCriteria.setCacheMode(CacheMode.GET);
            } else if ("IGNORE".equalsIgnoreCase(cacheMode)) {
                newCriteria.setCacheMode(CacheMode.IGNORE);
            } else if ("NORMAL".equalsIgnoreCase(cacheMode)) {
                newCriteria.setCacheMode(CacheMode.NORMAL);
            } else if ("PUT".equalsIgnoreCase(cacheMode)) {
                newCriteria.setCacheMode(CacheMode.PUT);
            } else if ("REFRESH".equalsIgnoreCase(cacheMode)) {
                newCriteria.setCacheMode(CacheMode.REFRESH);
            } else {
                newCriteria.setCacheMode(CacheMode.NORMAL);
            }
        }
        if (lockMode != null && lockMode.length() > 0) {
            if ("NONE".equalsIgnoreCase(lockMode)) {
                newCriteria.setLockMode(LockMode.NONE);
            } else if ("READ".equalsIgnoreCase(lockMode)) {
                newCriteria.setLockMode(LockMode.READ);
            } else if ("UPGRADE".equalsIgnoreCase(lockMode)) {
                newCriteria.setLockMode(LockMode.UPGRADE);
            } else if ("UPGRADE_NOWAIT".equalsIgnoreCase(lockMode)) {
                newCriteria.setLockMode(LockMode.UPGRADE_NOWAIT);
            } else if ("WRITE".equalsIgnoreCase(lockMode)) {
                newCriteria.setLockMode(LockMode.WRITE);
            } else {
                throw new SAXException("lockMode[" + lockMode + "] Not Recognized");
            }
        }
        if (flushMode != null && flushMode.length() > 0) {
            if ("ALWAYS".equalsIgnoreCase(flushMode)) {
                newCriteria.setFlushMode(FlushMode.ALWAYS);
            } else if ("AUTO".equalsIgnoreCase(flushMode)) {
                newCriteria.setFlushMode(FlushMode.AUTO);
            } else if ("COMMIT".equalsIgnoreCase(flushMode)) {
                newCriteria.setFlushMode(FlushMode.COMMIT);
            } else if ("NEVER".equalsIgnoreCase(flushMode)) {
                // NEVER is deprecated, so we won't throw an exception but we'll ignore it.
            } else {
                throw new SAXException("flushMode[" + flushMode + "] Not Recognized");
            }
        }
        return newCriteria;

    } catch (Exception e) {
        throw new SAXException("Unable to configure class " + className, e);
    }
}

From source file:com.heliosapm.aa4h.parser.XMLQueryParser.java

License:Apache License

/**
 * Adds a new projection to the current projectionList stack.
 * Mandatory Attributes:<ul>/*from  w  w w .  ja  v  a 2 s . c  om*/
 * <li><b>name</b>: The field to create a projection on.</li>
 * <li><b>type</b>: The projection type.</li>
 * <li><b>alias</b>: The name applied to the projection returned field.</li>
 * </ul>
 * @param attrs The attributes of the processed node.
 * @throws SAXException
 * TODO: Implement checks for mandatory attributes.
 */
public void addProjection(Attributes attrs) throws SAXException {
    ProjectionList projectionList = projectionStack.peek();
    if (projectionList == null) {
        throw new SAXException(
                "Attempted to add Projection and no ProjectionList Exists On The Stack. (Projection must be embedded inside a Projections)");
    }
    String type = attrs.getValue("type");
    String name = attrs.getValue("name");
    String alias = attrs.getValue("alias");
    if ("avg".equalsIgnoreCase(type)) {
        projectionList.add(Projections.avg(name), alias);
    } else if ("count".equalsIgnoreCase(type)) {
        projectionList.add(Projections.count(name), alias);
    } else if ("countDistinct".equalsIgnoreCase(type)) {
        projectionList.add(Projections.countDistinct(name), alias);
    } else if ("groupProperty".equalsIgnoreCase(type)) {
        projectionList.add(Projections.groupProperty(name), alias);
    } else if ("max".equalsIgnoreCase(type)) {
        projectionList.add(Projections.max(name), alias);
    } else if ("min".equalsIgnoreCase(type)) {
        projectionList.add(Projections.min(name), alias);
    } else if ("sum".equalsIgnoreCase(type)) {
        projectionList.add(Projections.sum(name), alias);
    } else if ("rowCount".equalsIgnoreCase(type)) {
        projectionList.add(Projections.rowCount(), alias);
    }

}

From source file:com.hibernate.dao.AsesinosDAO.java

@Override
public List<Asesinos> getAsesinosListByProjection() {

    List<Asesinos> peliculas1 = session.createCriteria(Asesinos.class)//.list();
            .setProjection(Projections.projectionList().add(Projections.rowCount())
                    .add(Projections.avg("personasasesinadas")).add(Projections.max("personasasesinadas"))
                    .add(Projections.groupProperty("formato").as("ao"))

            ).list();//from  w  ww  .j av  a 2 s  . c  om

    return peliculas1;
}

From source file:com.hmsinc.epicenter.model.AbstractJPARepository.java

License:Open Source License

public <L extends T> long count(final Class<L> type) {
    Validate.notNull(type, "Type must be specified.");
    final Criteria c = ModelUtils.criteriaQuery(entityManager, type);
    c.setProjection(Projections.rowCount());
    return ((Integer) c.uniqueResult()).longValue();
}

From source file:com.hmsinc.epicenter.model.surveillance.impl.SurveillanceRepositoryImpl.java

License:Open Source License

public Integer getAnomalyCount(final DateTime startDate, final DateTime endDate, final boolean includeAll,
        final Geometry filter, final Geometry excludeFacilityEventsFilter) {

    final Criteria c = criteriaQuery(entityManager, Anomaly.class);
    c.setCacheable(true);//  w w w.j a va2 s  . c o  m

    applyAnomalyCriteria(c, startDate, endDate, includeAll, filter, excludeFacilityEventsFilter);

    return (Integer) c.setProjection(Projections.rowCount()).uniqueResult();
}

From source file:com.hmsinc.epicenter.model.workflow.impl.WorkflowRepositoryImpl.java

License:Open Source License

public Integer getInvestigationCount(DateTime startDate, DateTime endDate, Geometry geometry,
        Collection<Organization> organizations, boolean showAll) {

    final Criteria c = criteriaQuery(entityManager, Investigation.class, "investigation");
    applyInvestigationCriteria(c, startDate, endDate, geometry, organizations, showAll);

    c.setProjection(Projections.rowCount());

    return (Integer) c.uniqueResult();
}

From source file:com.hrms.manager.UserManager.java

public Long[] getEmps() {

    Long ary[] = new Long[3];

    if (session != null) {
        session.close();/*  ww  w  .  j  ava 2s  .co m*/
    }
    session = HibernateUtil.getSessionFactory().getCurrentSession();
    Transaction t = session.beginTransaction();
    try {

        System.out.println("count--------++++--------->");
        Long x = (Long) session.createCriteria(EmployeeProfile.class).setProjection(Projections.rowCount())
                .uniqueResult();
        System.out.println("count--------++++--------->" + x);
        Criteria cr = session.createCriteria(EmployeeProfile.class);
        cr.add(Restrictions.eq("gender", "M"));
        cr.setProjection(Projections.rowCount());
        List rowCount = cr.list();
        System.out.println("Total Coint: " + rowCount.get(0));
        Criteria cr1 = session.createCriteria(EmployeeProfile.class);
        cr1.add(Restrictions.eq("gender", "F"));
        cr1.setProjection(Projections.rowCount());
        List rowCount1 = cr1.list();
        System.out.println("Total Coint: " + rowCount1.get(0));
        ary[0] = x;
        ary[1] = (Long) rowCount.get(0);
        ary[2] = (Long) rowCount1.get(0);
        System.out.println("Total aaaaarrrrrrCoint: " + ary[2]);
        t.commit();
        session.close();

    } catch (Exception e) {
        //session.getTransaction().rollback();
    } finally {
        if (session != null) {
            //session.close();
        }
    }
    return ary;

}

From source file:com.hrms.manager.UserManager.java

public Long getQuarts() {
    session = HibernateUtil.getSessionFactory().getCurrentSession();
    Transaction t = session.beginTransaction();
    Long x = (Long) session.createCriteria(ResidentialQuarters.class).setProjection(Projections.rowCount())
            .uniqueResult();/* w  w  w .  j a v a  2  s  .  com*/
    System.out.println("count--------++++--------->" + x);
    t.commit();
    //session.close();
    return x;
}

From source file:com.hrms.manager.UserManager.java

public Long[] getConn() {
    Long ary[] = new Long[3];
    session = HibernateUtil.getSessionFactory().getCurrentSession();
    Transaction t = session.beginTransaction();
    Long x = (Long) session.createCriteria(GasConnectionMaster.class).setProjection(Projections.rowCount())
            .uniqueResult();//from   w  w  w  .j  a  v  a 2  s .  c  o  m
    System.out.println("count--------++++--------->" + x);
    Long y = (Long) session.createCriteria(GasAllotment.class).setProjection(Projections.rowCount())
            .uniqueResult();
    System.out.println("count--------++++--------->" + y);
    Long z = x - y;
    System.out.println("count--------++++--------->" + z);
    ary[0] = x;
    ary[1] = y;
    ary[2] = z;
    t.commit();
    // session.close();
    return ary;
}

From source file:com.hrms.manager.UserManager.java

public Long getfamilies() {
    session = HibernateUtil.getSessionFactory().getCurrentSession();
    Transaction t = session.beginTransaction();
    Long x = null;/*  w w  w. ja va2  s .c o  m*/
    try {
        x = (Long) session.createCriteria(FamilyIdentity.class).setProjection(Projections.rowCount())
                .uniqueResult();
        System.out.println("count--------++++--------->" + x);
    } catch (Exception e) {
        System.out.println("------->" + e);
    }
    t.commit();
    // session.close();
    return x;
}