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.levinas.ecole.dao.ResponsableDaoImpl.java

public HashMap listAll(int page, int rpp, String search, boolean all) {
    Integer iRpp = rpp;/* w  w  w. java2 s .co  m*/
    HashMap result = new HashMap();

    Session session = sessionFactory.getCurrentSession();
    Query query = session.createQuery(
            "SELECT r FROM Responsable r WHERE r.nom like :search OR r.prenom like :search OR r.adresse like :search OR r.ville like :search OR r.codePostale like :search");
    query.setParameter("search", search);
    double nbResult = query.list().size();

    if (!all) {
        query.setFirstResult((page - 1) * rpp);
        query.setMaxResults(rpp);
    }
    List listItems = query.list();

    result.put("items", listItems);
    result.put("page_count", Math.ceil(nbResult / iRpp.doubleValue()));
    result.put("total_items", nbResult);

    return result;
}

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 {// w  w w .java 2 s .  c  o  m
        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,.// w  w  w  .j  a  v  a 2 s  .com
 */
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 w  w.j  a  v a2 s.c o  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 www .  j a va 2 s  . c  o 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  w  ww .  j a v  a  2  s  .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  a2 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

/**
 * /*from w w w . ja va  2  s  .  c  om*/
 * @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

/**
 * ()/*  w  w w  . j av a  2  s . com*/
 * @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.DiaryDAO.java

License:Open Source License

/**
 * //from  w w  w  .j  a  v  a  2  s.co  m
 * 
 * @param site
 * @param user
 * @param catalog_id
 * @param year
 * @param month
 * @param date
 * @param fromIdx
 * @param count
 * @return
 */
public static List listDiary(int year, int month, int date, int fromIdx, int count, boolean withContent) {
    StringBuffer hql = new StringBuffer("FROM ");
    hql.append(withContent ? "DiaryBean" : "DiaryOutlineBean");
    hql.append(" AS a WHERE a.status=:status");
    // 
    hql.append(" AND (a.catalog.type<>:cat_type)");
    if (year > 0 || month > 0 || date > 0) {
        hql.append(" AND a.writeTime >= :beginTime AND a.writeTime < :endTime");
    }
    hql.append(" ORDER BY a.id DESC");
    try {
        Session ssn = getSession();
        Query q = ssn.createQuery(hql.toString());
        q.setInteger("status", DiaryBean.STATUS_NORMAL);
        q.setInteger("cat_type", CatalogBean.TYPE_OWNER);
        if (year > 0 || month > 0 || date > 0) {
            Calendar[] cals = genTimeParams(year, month, date);
            q.setTimestamp("beginTime", cals[0].getTime());
            q.setTimestamp("endTime", cals[1].getTime());
        }
        if (fromIdx > 0)
            q.setFirstResult(fromIdx);
        if (count > 0)
            q.setMaxResults(count);
        return q.list();
    } finally {
        hql = null;
    }
}