Example usage for org.hibernate Query scroll

List of usage examples for org.hibernate Query scroll

Introduction

In this page you can find the example usage for org.hibernate Query scroll.

Prototype

ScrollableResults scroll();

Source Link

Document

Return the query results as ScrollableResults.

Usage

From source file:resource.dao.base.HQLDAO.java

public Object doInHibernate(Session session) throws HibernateException {
    if (logger.isDebugEnabled()) {
        logger.debug("doInHibernate(Session) - start"); //$NON-NLS-1$
    }//w w  w.  ja v a2s.com
    Query queryObject = session.createQuery(queryCondition.getQueryString());
    String countHQL = HqlUtils.transferCountHQL(queryCondition.getQueryString());
    if (logger.isInfoEnabled()) {
        logger.info("COUNTHQL = " + countHQL);
    }
    Query queryCount = session.createQuery(countHQL);
    Long totleCount = (Long) queryCount.uniqueResult();

    PageQueryResult queryResult = new PageQueryResult();
    queryResult.setQueryResult(new ArrayList());
    queryResult.setTotalCount(totleCount.intValue());
    if (totleCount > 0) {
        int pageSize = queryCondition.getPageSize();
        int pageIndex = queryCondition.getPageIndex() - 1;
        int startRowNum = pageIndex * pageSize;
        queryObject.setFirstResult(startRowNum);
        queryObject.setMaxResults(pageSize);
        ScrollableResults sr = queryObject.scroll();
        while (sr.next()) {
            queryResult.getQueryResult().add(sr.get());
        }

    }
    if (logger.isDebugEnabled()) {
        logger.debug("doInHibernate(Session) - end"); //$NON-NLS-1$
    }
    return queryResult;
}

From source file:resource.dao.base.HQLDAO.java

public Object doInHibernate(Session session) throws HibernateException {
    if (logger.isDebugEnabled()) {
        logger.debug("doInHibernate(Session) - start"); //$NON-NLS-1$
    }//from  www  .j  a v a  2s.  c o m

    Query queryObject = session.createQuery(queryCondition.getQueryString());
    Object[] values = queryCondition.getObjArray();
    Type[] types = queryCondition.getTypeArray();
    if (null != values) {
        for (int i = 0; i < values.length; i++) {
            if (logger.isDebugEnabled()) {
                logger.debug("i=" + i);
                logger.debug("values[i]=" + values[i]);
                logger.debug("types[i]=" + types[i]);
            }
            if (types != null) {
                queryObject.setParameter(i, values[i], types[i]);
            } else {
                queryObject.setParameter(i, values[i]);
            }
        }
    }

    ScrollableResults sr = null;
    try {
        PageQueryResult queryResult = new PageQueryResult();
        queryResult.setQueryResult(new ArrayList());
        queryResult.setTotalCount(0);

        sr = queryObject.scroll();
        if (false == sr.last()) {

            if (logger.isDebugEnabled()) {
                logger.debug("doInHibernate(Session) - end"); //$NON-NLS-1$
            }
            return queryResult;
        }

        int totalCount = sr.getRowNumber();
        queryResult.setTotalCount(totalCount + 1);

        int pageSize = queryCondition.getPageSize();
        int pageIndex = queryCondition.getPageIndex() - 1;

        int startRowNum = pageIndex * pageSize;
        if (false == sr.setRowNumber(startRowNum)) {
            if (logger.isDebugEnabled()) {
                logger.debug("doInHibernate(Session) - end"); //$NON-NLS-1$
            }
            return queryResult;
        }

        List list = queryResult.getQueryResult();
        for (int i = 0; i < pageSize; i++) {
            list.add(sr.get());
            if (false == sr.next()) {
                break;
            }
        }

        if (logger.isDebugEnabled()) {
            logger.debug("doInHibernate(Session) - end"); //$NON-NLS-1$
        }
        return queryResult;
    } finally {
        if (null != sr)
            sr.close();
    }
}

From source file:resource.dao.base.HQLDAO.java

public Object doInHibernate(Session session) throws HibernateException {
    if (logger.isDebugEnabled()) {
        logger.debug("doInHibernate(Session) - start"); //$NON-NLS-1$
    }/*w  w  w . ja  v a 2s .  c o m*/

    Query queryObject = session.createQuery(queryCondition.getQueryString());
    Object[] values = queryCondition.getObjArray();
    Type[] types = queryCondition.getTypeArray();

    if (null != values) {
        for (int i = 0; i < values.length; i++) {
            if (logger.isDebugEnabled()) {
                logger.debug("i=" + i);
                logger.debug("values[i]=" + values[i]);
                logger.debug("types[i]=" + types[i]);
            }
            if (types != null) {
                queryObject.setParameter(i, values[i], types[i]);
            } else {
                queryObject.setParameter(i, values[i]);
            }
        }
    }

    ScrollableResults sr = null;
    try {
        RangeQueryResult queryResult = new RangeQueryResult();
        queryResult.setQueryResult(new ArrayList());
        queryResult.setTotalCount(0);

        sr = queryObject.scroll();
        if (false == sr.last()) {

            if (logger.isDebugEnabled()) {
                logger.debug("doInHibernate(Session) - end"); //$NON-NLS-1$
            }
            return queryResult;
        }

        int totalCount = sr.getRowNumber();
        queryResult.setTotalCount(totalCount + 1);

        int startRowNum = queryCondition.getStart();
        if (false == sr.setRowNumber(startRowNum)) {
            if (logger.isDebugEnabled()) {
                logger.debug("doInHibernate(Session) - end"); //$NON-NLS-1$
            }
            return queryResult;
        }

        List list = queryResult.getQueryResult();
        for (int i = 0; i < queryCondition.getCount(); i++) {
            list.add(sr.get());
            if (false == sr.next()) {
                break;
            }
        }

        if (logger.isDebugEnabled()) {
            logger.debug("doInHibernate(Session) - end"); //$NON-NLS-1$
        }
        return queryResult;
    } finally {
        if (null != sr)
            sr.close();
    }
}

From source file:ubic.gemma.persistence.service.expression.experiment.ExpressionExperimentDaoImpl.java

License:Apache License

@Override
public Map<Taxon, Long> getPerTaxonCount() {

    Map<Taxon, Long> taxonCount = new LinkedHashMap<>();
    String queryString = "select t, count(distinct ee) from ExpressionExperiment "
            + "ee inner join ee.bioAssays as ba inner join ba.sampleUsed su "
            + "inner join su.sourceTaxon t group by t order by t.scientificName ";

    // it is important to cache this, as it gets called on the home page. Though it's actually fast.
    org.hibernate.Query queryObject = this.getSessionFactory().getCurrentSession().createQuery(queryString);
    queryObject.setCacheable(true);/* w  ww. ja  va  2s .co m*/
    ScrollableResults list = queryObject.scroll();
    while (list.next()) {
        Taxon taxon = (Taxon) list.get(0);
        Long count = list.getLong(1);

        taxonCount.put(taxon, count);

    }
    return taxonCount;

}

From source file:uit.qabpss.util.dao.orm.hibernate.QueryUtil.java

License:Open Source License

public static List<?> randomList(Query query, Dialect dialect, int total, int num, boolean unmodifiable) {

    if ((total == 0) || (num == 0)) {
        return new ArrayList<Object>();
    }/*  w w  w  .j a  v  a 2s .c  o  m*/

    if (num >= total) {
        return list(query, ALL_POS, ALL_POS);
    }

    int[] scrollIds = Randomizer.getInstance().nextInt(total, num);

    List<Object> list = new ArrayList<Object>();

    ScrollableResults sr = query.scroll();

    for (int i = 0; i < scrollIds.length; i++) {
        if (sr.scroll(scrollIds[i])) {
            Object obj = sr.get(0);

            list.add(obj);

            sr.first();
        }
    }

    return list;

}