Example usage for org.hibernate Query setFirstResult

List of usage examples for org.hibernate Query setFirstResult

Introduction

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

Prototype

@Override
    Query<R> setFirstResult(int startPosition);

Source Link

Usage

From source file:com.javaweb.service.TintucService.java

public ArrayList<Tintuc> GetAllTintuc(int pageSize, int pageNumber) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction tx = null;/*  w  ww.  ja  v  a  2s  .  com*/
    ArrayList<Tintuc> listTintuc = new ArrayList<Tintuc>();
    try {
        tx = session.getTransaction();
        tx.begin();
        Query query = session.createQuery("from Tintuc");
        tintuccount = query.list().size();
        query = query.setFirstResult(pageSize * (pageNumber - 1));
        query.setMaxResults(pageSize);
        listTintuc = (ArrayList) query.list();
        tx.commit();
    } catch (Exception e) {
        if (tx != null) {
            tx.rollback();
        }
    } finally {
        session.close();
    }
    return listTintuc;
}

From source file:com.javaweb.service.TintucService.java

public ArrayList<Tintuc> SearchTintuc(int pageSize, int pageNumber, String TintucKey) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction tx = null;/*ww w .  jav  a 2  s  .  c o m*/
    ArrayList listTintuc = new ArrayList<Tintuc>();
    try {
        tx = session.getTransaction();
        tx.begin();
        Query query = session.createQuery("from Tintuc where NgayDang = '" + TintucKey + "'");
        tintuccount = query.list().size();
        query = query.setFirstResult(pageSize * (pageNumber - 1));
        query.setMaxResults(pageSize);
        listTintuc = (ArrayList) query.list();
        tx.commit();
    } catch (Exception e) {
        if (tx != null) {
            tx.rollback();
        }
    } finally {
        session.close();
    }
    return listTintuc;
}

From source file:com.javaweb.service.VeService.java

public ArrayList<Ve> GetAllVe(int pageSize, int pageNumber) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction tx = null;//  w w  w.  j av a2 s  . c o m
    ArrayList<Ve> listVe = new ArrayList<Ve>();
    try {
        tx = session.getTransaction();
        tx.begin();
        Query query = session.createQuery("from Ve");
        vecount = query.list().size();
        query = query.setFirstResult(pageSize * (pageNumber - 1));
        query.setMaxResults(pageSize);
        listVe = (ArrayList) query.list();
        tx.commit();
    } catch (Exception e) {
        if (tx != null) {
            tx.rollback();
        }
    } finally {
        session.close();
    }
    return listVe;
}

From source file:com.jdon.persistence.hibernate.HibernateTemplate.java

License:Apache License

/**
 * Prepare the given Query object, applying cache settings and/or a
 * transaction timeout.//from   www. j  a  v a 2  s  .c om
 * 
 * @param queryObject
 *            the Query object to prepare
 * @see #setCacheQueries
 * @see #setQueryCacheRegion
 * @see SessionProviderHolder#applyTransactionTimeout
 */
protected void prepareQuery(Query queryObject) {
    if (isCacheQueries()) {
        queryObject.setCacheable(true);
        if (getQueryCacheRegion() != null) {
            queryObject.setCacheRegion(getQueryCacheRegion());
        }
    }
    if (getFetchSize() > 0) {
        queryObject.setFetchSize(getFetchSize());
    }
    if (getMaxResults() > 0) {
        queryObject.setMaxResults(getMaxResults());
    }

    if (getFirstResult() > 0) {
        queryObject.setFirstResult(getFirstResult());
    }

}

From source file:com.jeysan.modules.orm.hibernate.HibernateDao.java

License:Apache License

/**
 * ?Query,./*  ww  w.j av  a  2  s.c om*/
 */
protected Query setPageParameterToQuery(final Query q, final Page<T> page) {
    if (page.getPageSize() != -1) {
        //Assert.isTrue(page.getPageSize() > 0, "Page Size must larger than zero");

        //hibernatefirstResult??0
        q.setFirstResult(page.getFirst() - 1);
        q.setMaxResults(page.getPageSize());
        return q;
    }
    return q;
}

From source file:com.jredrain.dao.HibernateDao.java

License:Apache License

/**
 * //from  w w  w  .java  2 s.c  o m
 *
 * @param query
 * @param pageNo
 *            ?
 * @param pageSize
 *            ???
 * @return
 */
public static List pageQuery(Query query, int pageNo, int pageSize) {
    return query.setFirstResult((pageNo - 1) * pageSize).setMaxResults(pageSize).list();
}

From source file:com.jshop.dao.impl.JshopbasicInfoTDaoImpl.java

@SuppressWarnings("unchecked")
public List<JshopbasicInfoT> findAllJshopbasicInfo(final int currentPage, final int lineSize,
        final String creatorid) {
    log.debug("find all JshopbasicInfoT");
    try {//  w w w  .ja va  2 s. c om
        List<JshopbasicInfoT> list = this.getHibernateTemplate().executeFind(new HibernateCallback() {

            String queryString = "from JshopbasicInfoT as jbi where jbi.creatorid=:creatorid";

            public Object doInHibernate(Session session) throws HibernateException, SQLException {
                Query query = session.createQuery(queryString);
                query.setFirstResult((currentPage - 1) * lineSize);
                query.setMaxResults(lineSize);
                query.setParameter("creatorid", creatorid);
                List list = query.list();
                return list;
            }
        });
        return list;
    } catch (RuntimeException re) {
        log.error("find all brand error", re);
        throw re;
    }
}

From source file:com.jshop.dao.impl.JshopbasicInfoTDaoImpl.java

@SuppressWarnings("unchecked")
public List<JshopbasicInfoT> findAllJshopbasicInfoNoParam(final int currentPage, final int lineSize,
        final String state) {
    log.debug("find all findAllJshopbasicInfoNoParam");
    try {/*from   w w w.ja v  a  2  s.c om*/
        List<JshopbasicInfoT> list = this.getHibernateTemplate().executeFind(new HibernateCallback() {

            String queryString = "from JshopbasicInfoT as jbi where jbi.state=:state and jbi.openstate=:openstate";

            public Object doInHibernate(Session session) throws HibernateException, SQLException {
                Query query = session.createQuery(queryString);
                query.setFirstResult((currentPage - 1) * lineSize);
                query.setMaxResults(lineSize);
                query.setParameter("state", state);
                query.setParameter("openstate", state);
                List list = query.list();
                return list;
            }
        });
        return list;
    } catch (RuntimeException re) {
        log.error("find all findAllJshopbasicInfoNoParam error", re);
        throw re;
    }
}

From source file:com.krawler.dao.BaseDAO.java

License:Open Source License

/**
 * Executes a limit select query using the provided query, parameters and
 * limits. Limits are provided using the pagingParam parameter.
 * /* w w  w  .  j  av  a  2 s .c o m*/
 * @param hql
 *            The query to execute
 * @param params
 *            Query Paramter
 * @param pagingParam
 *            Limit paramters. The first entry is the lower limit and the
 *            second is the upper limit
 * @return List
 */
public List executeQueryPaging(final String hql, final Object[] params, final Integer[] pagingParam) {
    List results = null;
    results = getHibernateTemplate().executeFind(new HibernateCallback() {
        public Object doInHibernate(Session session) {
            Query query = session.createQuery(hql);

            if (params != null) {
                for (int i = 0; i < params.length; i++) {
                    query.setParameter(i, params[i]);
                }
            }
            query.setFirstResult(pagingParam[0]);
            query.setMaxResults(pagingParam[1]);

            return query.list();
        }
    });
    return results;
}

From source file:com.krawler.dao.BaseDAO.java

License:Open Source License

public List executeQuery(final String hql, final Object[] params, final Map<String, Object> namedParams,
        final int[] pagingParams) {
    return getHibernateTemplate().executeFind(new HibernateCallback() {
        public Object doInHibernate(Session session) {
            Query query = session.createQuery(hql);
            if (params != null) {
                for (int i = 0; i < params.length; i++) {
                    query.setParameter(i, params[i]);
                }//w  w w  . j  a  v  a 2s  .c om
            }
            if (namedParams != null) {
                for (Map.Entry<String, Object> entry : namedParams.entrySet()) {
                    Object value = entry.getValue();
                    if (value != null) {
                        if (value instanceof Collection)
                            query.setParameterList(entry.getKey(), (Collection) value);
                        else if (entry.getValue().getClass().isArray())
                            query.setParameterList(entry.getKey(), (Object[]) value);
                        else
                            query.setParameter(entry.getKey(), entry.getValue());
                    } else
                        query.setParameter(entry.getKey(), entry.getValue());
                }
            }
            if (pagingParams != null) {
                query.setFirstResult(pagingParams[0]);
                query.setMaxResults(pagingParams[1]);
            }
            return query.list();
        }
    });
}