Example usage for org.hibernate ScrollableResults last

List of usage examples for org.hibernate ScrollableResults last

Introduction

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

Prototype

boolean last();

Source Link

Document

Go to the last result.

Usage

From source file:org.openvpms.component.business.dao.hibernate.im.IMObjectDAOHibernate.java

License:Open Source License

/**
 * Counts the total no. of rows that would be returned by a named query.
 *
 * @param name    the query name/* w w  w .j av  a 2  s  .c  om*/
 * @param params  the query parameters
 * @param session the session
 * @return the total no. of rows that would be returned by the query
 * @throws IMObjectDAOException a runtime exception, raised if the request
 *                              cannot complete.
 */
private int countNamedQuery(String name, Params params, Session session) {
    Query query = session.getNamedQuery(name);
    params.setParameters(query);

    int result = 0;
    ScrollableResults results = null;
    try {
        results = query.scroll(ScrollMode.FORWARD_ONLY);
        if (results.last()) {
            result = results.getRowNumber() + 1;
        }
    } finally {
        if (results != null) {
            results.close();
        }
    }
    return result;
}

From source file:org.sacredscripturefoundation.commons.entity.dao.HibernateVendorHelper.java

License:Apache License

@Override
public Count<List<T>> page(Query query, int beginRow, int endRow) {
    // Get total result count
    int total;// w  ww  . j av  a2  s.com
    ScrollableResults scroll = query.unwrap(org.hibernate.Query.class).scroll();
    try {
        scroll.last();
        total = scroll.getRowNumber();
    } finally {
        scroll.close();
    }

    // Get page of data
    query.setFirstResult(beginRow);
    query.setMaxResults(endRow - beginRow + 1);

    @SuppressWarnings("unchecked")
    List<T> results = query.getResultList();
    return new CountImpl<List<T>>(total, results);
}

From source file:org.shredzone.cilla.core.repository.impl.SearchDaoHibImpl.java

License:Open Source License

@Override
public int count(Query query, Criteria crit) {
    assertCriteriaEntity(crit);/* w w w.j  a  va 2 s . c  o  m*/

    int count = 0;

    FullTextSession fullTextSession = getFullTextSession();
    FullTextQuery fq = fullTextSession.createFullTextQuery(query, Page.class);
    fq.setCriteriaQuery(crit != null ? crit : pageDao.criteria());

    ScrollableResults sr = fq.scroll(ScrollMode.FORWARD_ONLY);
    if (sr.last()) {
        count = sr.getRowNumber();
    }
    sr.close();

    return count;
}

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  a2  s  .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  .  j  a va 2 s.  com

    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  a  2  s.  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:vn.vnpttech.ssdc.nms.dao.hibernate.DeviceDaoHibernate.java

License:Apache License

@Override
public Map searchDevice(String deviceMAC, String deviceSerialNumber, String deviceStatus,
        String deviceFirmwareStatus, String deviceModel, String deviceFirmware, String deviceProvince,
        String deviceDistrict, List<Area> listArea, String ipAddress, String username, Long start, Long limit) {
    try {/*www.j a  v a 2 s  .  com*/
        int total = 0;
        Map pagingMap = new HashMap();

        List<Device> listDevice = new ArrayList<Device>();
        Criteria criteria = getSession().createCriteria(Device.class);
        //MAC
        if (StringUtils.isNotBlank(deviceMAC)) {
            criteria.add(Restrictions.like("mac", deviceMAC.trim(), MatchMode.ANYWHERE).ignoreCase());
        }
        //Serial Number
        if (StringUtils.isNotBlank(deviceSerialNumber)) {
            criteria.add(Restrictions.like("serialNumber", "%" + deviceSerialNumber.trim() + "%").ignoreCase());
        }
        //Device status
        if (StringUtils.isNotBlank(deviceStatus)) {
            criteria.add(Restrictions.eq("cpeStatus", Integer.parseInt(deviceStatus.trim())));
        }
        //Firmware Status
        if (StringUtils.isNotBlank(deviceFirmwareStatus)) {
            criteria.add(Restrictions.eq("firmwareStatus", Integer.parseInt(deviceFirmwareStatus.trim())));
        }
        //Model
        if (StringUtils.isNotBlank(deviceModel)) {
            criteria.add(Restrictions.eq("deviceModel.id", Long.parseLong(deviceModel.trim())));
        }
        //Firmware
        if (StringUtils.isNotBlank(deviceFirmware)) {
            criteria.add(Restrictions.eq("firmwareVersion", deviceFirmware.trim()).ignoreCase());
        }

        //IP
        if (StringUtils.isNotBlank(ipAddress)) {
            criteria.add(Restrictions.like("ipAddress", ipAddress.trim(), MatchMode.ANYWHERE).ignoreCase());
        }
        // username 
        if (StringUtils.isNotBlank(username)) {
            criteria.add(Restrictions.like("stbUsername", username.trim(), MatchMode.ANYWHERE).ignoreCase());
        }

        //Province
        if (!listArea.isEmpty()) {
            criteria.add(Restrictions.in("area", listArea));
        }
        //            if (StringUtils.isNotBlank(deviceProvince)) {
        //                //District
        //                if (StringUtils.isBlank(deviceDistrict)) { //province level
        //                    criteria.createCriteria("area").add(Restrictions.eq("area.id", Long.parseLong(deviceProvince.trim())));
        //                } else {//district level
        //                    criteria.add(Restrictions.eq("area.id", Long.parseLong(deviceDistrict.trim())));
        //                }
        //            }
        criteria.addOrder(Order.desc("id"));
        //Paging
        if (limit != null && limit > 0) {
            // get the count
            ScrollableResults results = criteria.scroll();
            results.last();
            total = results.getRowNumber() + 1;
            results.close();
            //End get count
            criteria.setFirstResult(start.intValue());
            criteria.setMaxResults(limit.intValue());
        }

        listDevice = criteria.list();
        pagingMap.put("list", listDevice);
        pagingMap.put("totalCount", Long.parseLong(String.valueOf(total)));
        return pagingMap;
    } catch (Exception ex) {
        log.error("ERROR searchDevice: ", ex);
        return null;
    }
}

From source file:vn.vnpttech.ssdc.nms.dao.hibernate.PolicyDaoHibernate.java

License:Apache License

@Override
public Map searchPolicy(String policyName, String policyStatus, String enable, String startTime, String endTime,
        Long start, Long limit) {
    try {//  w w  w. j  ava  2 s  .  co  m

        int total = 0;
        Map pagingMap = new HashMap();

        List<Policy> listPolicy = new ArrayList<Policy>();
        Criteria criteria = getSession().createCriteria(Policy.class);
        //Name
        if (StringUtils.isNotBlank(policyName)) {
            criteria.add(Restrictions.like("name", "%" + policyName.trim() + "%").ignoreCase());
        }
        //Status
        if (StringUtils.isNotBlank(policyStatus)) {
            criteria.add(Restrictions.eq("policyStatus", Integer.parseInt(policyStatus.trim())));
        }
        //Enable
        if (StringUtils.isNotBlank(enable)) {
            criteria.add(Restrictions.eq("enable", Integer.parseInt(enable.trim())));
        }
        //Start Time & End Time
        if (StringUtils.isNotBlank(startTime)) {
            Date startTimeInDate = sdf.parse(startTime);
            criteria.add(Restrictions.ge("startTime", startTimeInDate));

        }
        if (StringUtils.isNotBlank(endTime)) {
            Date endTimeInDate = sdf.parse(endTime);
            criteria.add(Restrictions.le("endTime", endTimeInDate));
        }
        criteria.addOrder(Order.desc("id"));
        //Paging
        if (limit != null && limit > 0) {
            // get the count
            ScrollableResults results = criteria.scroll();
            results.last();
            total = results.getRowNumber() + 1;
            results.close();
            //End get count
            criteria.setFirstResult(start.intValue());
            criteria.setMaxResults(limit.intValue());
        }

        listPolicy = criteria.list();
        pagingMap.put("list", listPolicy);
        pagingMap.put("totalCount", Long.parseLong(String.valueOf(total)));
        return pagingMap;
    } catch (Exception ex) {
        log.error("ERROR searchPolicy: ", ex);
        return null;
    }
}

From source file:vn.vnpttech.ssdc.nms.dao.hibernate.PolicyHistoryDaoHibernate.java

License:Apache License

@Override
public Map searchPolicyHistory(String policyId, String deviceId, Long start, Long limit) {
    try {/*from  w  w  w  . j a  v  a2s . c om*/
        int total = 0;
        Map pagingMap = new HashMap();

        List<PolicyHistory> listPolicyHistory = new ArrayList<>();
        Criteria criteria = getSession().createCriteria(PolicyHistory.class);
        //Policy
        if (StringUtils.isNotBlank(policyId)) {
            criteria.add(Restrictions.eq("policy.id", Long.parseLong(policyId.trim())));
        }
        //Device
        if (StringUtils.isNotBlank(deviceId)) {
            criteria.add(Restrictions.eq("device.id", Long.parseLong(deviceId.trim())));
        }

        criteria.addOrder(Order.desc("id"));
        //Paging
        if (limit != null && limit > 0) {
            // get the count
            ScrollableResults results = criteria.scroll();
            results.last();
            total = results.getRowNumber() + 1;
            results.close();
            //End get count
            criteria.setFirstResult(start.intValue());
            criteria.setMaxResults(limit.intValue());
        }

        listPolicyHistory = criteria.list();
        pagingMap.put("list", listPolicyHistory);
        pagingMap.put("totalCount", Long.parseLong(String.valueOf(total)));
        return pagingMap;
    } catch (Exception ex) {
        log.error("ERROR searchPolicyHistory: ", ex);
        return null;
    }
}

From source file:vnpt.media.efinder.model.PaginationResult.java

public PaginationResult(Query query, int page, int maxResult, int maxNavigationPage) {
    final int pageIndex = page - 1 < 0 ? 0 : page - 1;
    int fromRecordIndex = pageIndex * maxResult;
    int maxRecordIndex = fromRecordIndex + maxResult;

    ScrollableResults resultScroll = query.scroll(ScrollMode.SCROLL_INSENSITIVE);

    //resultScroll.g
    List results = new ArrayList<>();
    boolean hasResult = resultScroll.first();
    if (hasResult) {
        // Cuon toi vi tri
        hasResult = resultScroll.scroll(fromRecordIndex);
        if (hasResult) {
            do {/*w ww .  java  2s . c  o  m*/
                E record = (E) resultScroll.get(0);
                results.add(record);
            } while (resultScroll.next()//
                    && resultScroll.getRowNumber() >= fromRecordIndex
                    && resultScroll.getRowNumber() < maxRecordIndex);
        }

        // chuyen toi ban ghi cuoi
        resultScroll.last();
    }

    // Tong so ban ghi
    this.totalRecords = resultScroll.getRowNumber() + 1;
    this.currentPage = pageIndex + 1;
    this.list = results;
    this.maxResult = maxResult;
    this.totalPages = (this.totalRecords / this.maxResult);
    if (totalRecords % this.maxResult != 0) {
        this.totalPages = this.totalPages + 1;
    }
    this.maxNavigationPage = totalPages;

    if (maxNavigationPage < totalPages) {
        this.maxNavigationPage = maxNavigationPage;
    }

    this.calcNavigationPages();

}