Example usage for org.hibernate Query setMaxResults

List of usage examples for org.hibernate Query setMaxResults

Introduction

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

Prototype

@Override
    Query<R> setMaxResults(int maxResult);

Source Link

Usage

From source file:com.liferay.jbpm.util.QueryUtil.java

License:Open Source License

public static List<?> list(Query query, Dialect dialect, int begin, int end) {

    if ((begin == ALL_POS) && (end == ALL_POS)) {
        return query.list();
    } else {//from ww w .j av a 2  s .com
        if (dialect.supportsLimit()) {
            query.setMaxResults(end - begin);
            query.setFirstResult(begin);

            return query.list();
        } else {
            List<Object> list = new ArrayList<Object>();

            ScrollableResults sr = query.scroll();

            if (sr.first() && sr.scroll(begin)) {
                for (int i = begin; i < end; i++) {
                    Object obj = sr.get(0);

                    list.add(obj);

                    if (!sr.next()) {
                        break;
                    }
                }
            }

            return list;
        }
    }
}

From source file:com.lighting.platform.base.dao.HibernateDao.java

License:Apache License

/**
 * ?Query,./*from   w  w w . j av  a  2s . c om*/
 */
protected Query setPageParameterToQuery(final Query q, final Page<?> page) {
    AssertUtils.isTrue(page.getPageSize() > 0, "Page Size must larger than zero");

    q.setFirstResult(page.getOffset());
    q.setMaxResults(page.getPageSize());

    return q;
}

From source file:com.liusoft.dlog4j.dao.BBSTopicDAO.java

License:Open Source License

/**
 * //from w ww .  j  a va  2 s. co  m
 * @param site
 * @param fbean
 * @param fromIdx
 * @param count
 * @return
 */
public static List listEliteTopics(SiteBean site, ForumBean fbean, int fromIdx, int count) {
    StringBuffer hql = new StringBuffer("FROM TopicOutlineBean AS t WHERE t.status=:status");
    if (site != null)
        hql.append(" AND t.site.id=:site");
    if (fbean != null)
        hql.append(" AND t.forum.id=:forum");
    hql.append(" AND (t.type=:elite OR t.type=:top_elite) ORDER BY ROUND(t.type / 16, 0) DESC, t.id DESC");
    Session ssn = getSession();
    Query q = ssn.createQuery(hql.toString());
    q.setInteger("status", TopicBean.STATUS_NORMAL);
    q.setInteger("elite", TopicBean.INFO_TYPE_ELITE);
    q.setInteger("top_elite", TopicBean.INFO_TYPE_TOP_ELITE);
    if (site != null)
        q.setInteger("site", site.getId());
    if (fbean != null)
        q.setInteger("forum", fbean.getId());
    if (fromIdx > 0)
        q.setFirstResult(fromIdx);
    if (count > 0)
        q.setMaxResults(count);
    return q.list();
}

From source file:com.liusoft.dlog4j.dao.BBSTopicDAO.java

License:Open Source License

/**
 * /*from w w w.j a  v a 2 s .  co m*/
 * 
 * @param forum_id
 * @param fromIdx
 * @param count
 * @return
 */
public static List listHotTopics(SiteBean site, ForumBean forum, int fromIdx, int count, int days) {
    StringBuffer hql = new StringBuffer(
            "FROM TopicOutlineBean AS t WHERE t.site.id=? AND t.status=? AND t.createTime >= ? AND t.replyCount > 0");
    if (forum != null)
        hql.append(" AND t.forum.id=?");
    hql.append(" ORDER BY ROUND(t.type / 16, 0) DESC, t.replyCount DESC, t.id DESC");
    Session ssn = getSession();
    try {
        Query q = ssn.createQuery(hql.toString());
        q.setInteger(0, site.getId());
        q.setInteger(1, TopicOutlineBean.STATUS_NORMAL);
        Calendar cur_time = Calendar.getInstance();
        cur_time.add(Calendar.DATE, -days);
        q.setTimestamp(2, new Timestamp(cur_time.getTime().getTime()));
        if (forum != null)
            q.setInteger(3, forum.getId());
        if (fromIdx > 0)
            q.setFirstResult(fromIdx);
        q.setMaxResults(count);
        return q.list();
    } finally {
        hql = null;
    }
}

From source file:com.liusoft.dlog4j.dao.DAO.java

License:Open Source License

/**
 * /*from   ww w . j  a  v a 2s. c o  m*/
 * @param hql
 * @param args
 * @return
 */
protected static List executeQuery(String hql, int fromIdx, int fetchCount, Object... args) {
    Session ssn = getSession();
    Query q = ssn.createQuery(hql).setReadOnly(true);
    for (int i = 0; i < args.length; i++) {
        q.setParameter(i, args[i]);
    }
    if (fromIdx > 0)
        q.setFirstResult(fromIdx);
    if (fetchCount > 0)
        q.setMaxResults(fetchCount);
    return q.list();
}

From source file:com.liusoft.dlog4j.dao.DAO.java

License:Open Source License

/**
 * ()/* w  w  w .  j av  a 2  s  . c  om*/
 * @param hql
 * @param args
 * @return
 */
protected static List executeQueryCacheable(String cache_region, String hql, int fromIdx, int fetchCount,
        Object... args) {
    Session ssn = getSession();
    Query q = ssn.createQuery(hql).setReadOnly(true).setCacheable(true);
    if (cache_region != null)
        q.setCacheRegion(cache_region);
    for (int i = 0; i < args.length; i++) {
        q.setParameter(i, args[i]);
    }
    if (fromIdx > 0)
        q.setFirstResult(fromIdx);
    if (fetchCount > 0)
        q.setMaxResults(fetchCount);
    return q.list();
}

From source file:com.liusoft.dlog4j.dao.DAO.java

License:Open Source License

/**
 * /*  w ww  .  java  2 s.  c  om*/
 * @param hql
 * @param args
 * @return
 */
protected static Object uniqueResult(String hql, Object... args) {
    Session ssn = getSession();
    Query q = ssn.createQuery(hql).setReadOnly(true);
    for (int i = 0; i < args.length; i++) {
        q.setParameter(i, args[i]);
    }
    q.setMaxResults(1);
    return q.uniqueResult();
}

From source file:com.liusoft.dlog4j.dao.DAO.java

License:Open Source License

/**
 * /*  w w  w .j  a  v a2  s  .  co  m*/
 * @param hql
 * @param args
 * @return
 */
protected static List executeNamedQuery(String hql, int fromIdx, int fetchCount, Object... args) {
    Session ssn = getSession();
    Query q = ssn.getNamedQuery(hql).setReadOnly(true);
    for (int i = 0; i < args.length; i++) {
        q.setParameter(i, args[i]);
    }
    if (fromIdx > 0)
        q.setFirstResult(fromIdx);
    if (fetchCount > 0)
        q.setMaxResults(fetchCount);
    return q.list();
}

From source file:com.liusoft.dlog4j.dao.DAO.java

License:Open Source License

/**
 * ()//from  ww w. ja va2s.c o m
 * @param hql
 * @param args
 * @return
 */
protected static List executeNamedQueryCacheable(String cache_region, String hql, int fromIdx, int fetchCount,
        Object... args) {
    Session ssn = getSession();
    Query q = ssn.getNamedQuery(hql).setReadOnly(true).setCacheable(true);
    if (cache_region != null)
        q.setCacheRegion(cache_region);
    for (int i = 0; i < args.length; i++) {
        q.setParameter(i, args[i]);
    }
    if (fromIdx > 0)
        q.setFirstResult(fromIdx);
    if (fetchCount > 0)
        q.setMaxResults(fetchCount);
    return q.list();
}

From source file:com.liusoft.dlog4j.dao.DAO.java

License:Open Source License

/**
 * //w  ww .  j a  va 2 s.co m
 * @param hql
 * @param args
 * @return
 */
protected static Object namedUniqueResult(String hql, Object... args) {
    Session ssn = getSession();
    Query q = ssn.getNamedQuery(hql).setReadOnly(true);
    for (int i = 0; i < args.length; i++) {
        q.setParameter(i, args[i]);
    }
    q.setMaxResults(1);
    return q.uniqueResult();
}