Example usage for org.hibernate ScrollableResults get

List of usage examples for org.hibernate ScrollableResults get

Introduction

In this page you can find the example usage for org.hibernate ScrollableResults get.

Prototype

Object[] get();

Source Link

Document

Get the current row of results.

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$
    }/*ww w  . j a  v a  2s .  c om*/

    SQLQuery queryObject = session.createSQLQuery(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. j  a  v a2s.com

    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();
    }
}