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.gcrm.dao.BaseDao.java

License:Apache License

@SuppressWarnings("unchecked")
public SearchResult<T> getPaginationObjects(final String clazz, final SearchCondition searchCondition) {

    List<T> objects = null;

    final String condition = searchCondition.getCondition();

    objects = getHibernateTemplate().executeFind(new HibernateCallback<List<T>>() {

        public List<T> doInHibernate(Session session) throws HibernateException, SQLException {
            String hql = INIT_HQL + clazz;
            if (condition != null && condition.length() > 0) {
                hql += " where ";
                hql += condition;/*w ww.j a v a  2  s  . c om*/
            }
            hql += " order by " + searchCondition.getSidx() + " "

                    + searchCondition.getSord();
            int pageSize = searchCondition.getPageSize();
            int pageNo = searchCondition.getPageNo();

            Query query = session.createQuery(hql);

            if (pageNo != 0 && pageSize != 0) {
                int rowNumber = (pageNo - 1) * pageSize;
                query.setFirstResult(rowNumber);
                query.setMaxResults(pageSize);
            }
            List<T> list = query.list();

            return list;
        }
    });

    long count = 0;
    String countHql = "select count(*) from " + clazz;
    if (condition != null && condition.length() > 0) {
        countHql += " where ";
        countHql += condition;
    }

    count = (Long) getHibernateTemplate().find(countHql).get(0);
    SearchResult<T> result = new SearchResult<T>(count, objects);

    return result;
}

From source file:com.gcrm.dao.impl.BaseDao.java

License:Apache License

@SuppressWarnings("unchecked")
public SearchResult<T> getPaginationObjects(final String clazz, final String columns,
        final SearchCondition searchCondition) {

    List<T> objects = null;

    final String condition = searchCondition.getCondition();

    objects = getHibernateTemplate().executeFind(new HibernateCallback() {

        public List<T> doInHibernate(Session session) throws HibernateException, SQLException {
            StringBuilder hqlBuilder = new StringBuilder("");
            if (columns != null) {
                hqlBuilder.append(SELECT_HQL).append(columns).append(" ");
            }/*  ww  w.  ja va2s.co  m*/
            hqlBuilder.append(FROM_HQL).append(clazz);
            if (condition != null && condition.length() > 0) {
                hqlBuilder.append(" where ");
                hqlBuilder.append(condition);
            }
            hqlBuilder.append(" order by ").append(searchCondition.getSidx()).append(" ")
                    .append(searchCondition.getSord());
            int pageSize = searchCondition.getPageSize();
            int pageNo = searchCondition.getPageNo();

            Query query = session.createQuery(hqlBuilder.toString());

            if (pageNo != 0 && pageSize != 0) {
                int rowNumber = (pageNo - 1) * pageSize;
                query.setFirstResult(rowNumber);
                query.setMaxResults(pageSize);
            }
            List<T> list = query.list();

            return list;
        }
    });

    long count = 0;
    String countHql = "select count(*) from " + clazz;
    if (condition != null && condition.length() > 0) {
        countHql += " where ";
        countHql += condition;
    }

    count = (Long) getHibernateTemplate().find(countHql).get(0);
    SearchResult<T> result = new SearchResult<T>(count, objects);

    return result;
}

From source file:com.gisgraphy.domain.repository.GenericDao.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<T> getAllPaginate(final int from, final int maxResults) {
    return (List<T>) this.getHibernateTemplate().execute(new HibernateCallback() {

        public Object doInHibernate(Session session) throws PersistenceException {
            String queryString = "from " + persistentClass.getSimpleName() + " order by id";
            //we order by id because we want to keep the same order query after query

            Query qry = session.createQuery(queryString);
            qry.setCacheable(true);/*from  w w w.  j  a  va  2s .  c  om*/
            if (maxResults > 0) {
                qry.setMaxResults(maxResults);
            }
            if (from >= 1) {
                qry.setFirstResult(from - 1);
            }
            List<T> results = (List<T>) qry.list();
            if (results == null) {
                results = new ArrayList<T>();
            }
            return results;
        }
    });

}

From source file:com.glaf.jbpm.dao.JbpmEntityDAO.java

License:Apache License

public List<Object> getList(JbpmContext jbpmContext, int currPageNo, int maxResults,
        SqlExecutor queryExecutor) {//from   w w w  . j  a  va  2  s . c  o m
    Session session = jbpmContext.getSession();
    Query query = session.createQuery(queryExecutor.getSql());
    Object parameter = queryExecutor.getParameter();
    if (parameter instanceof Map) {
        Map<String, Object> params = (Map<String, Object>) parameter;

        if (params != null && params.size() > 0) {
            Set<Entry<String, Object>> entrySet = params.entrySet();
            for (Entry<String, Object> entry : entrySet) {
                String name = entry.getKey();
                Object value = entry.getValue();
                if (value != null) {
                    if (value instanceof Collection) {
                        query.setParameterList(name, (Collection<?>) value);
                    } else {
                        query.setParameter(name, value);
                    }
                }
            }
        }
    }

    query.setFirstResult((currPageNo - 1) * maxResults);
    query.setMaxResults(maxResults);

    List<Object> rows = query.list();
    return rows;
}

From source file:com.glaf.jbpm.dao.JbpmEntityDAO.java

License:Apache License

public Paging getPage(JbpmContext jbpmContext, int currPageNo, int pageSize, SqlExecutor countExecutor,
        SqlExecutor queryExecutor) {/*from w w  w  .j  a  v  a  2 s. c o m*/
    Session session = jbpmContext.getSession();
    Paging page = new Paging();
    if (pageSize <= 0) {
        pageSize = Paging.DEFAULT_PAGE_SIZE;
    }
    if (currPageNo <= 0) {
        currPageNo = 1;
    }
    int totalCount = 0;
    if (countExecutor != null) {
        Object obj = null;
        String hql = countExecutor.getSql();
        hql = removeOrders(hql);
        Query q = session.createQuery(hql);

        Object parameter = queryExecutor.getParameter();
        if (parameter instanceof Map) {
            Map<String, Object> params = (Map<String, Object>) parameter;

            if (params != null && params.size() > 0) {
                Set<Entry<String, Object>> entrySet = params.entrySet();
                for (Entry<String, Object> entry : entrySet) {
                    String name = entry.getKey();
                    Object value = entry.getValue();
                    if (value != null) {
                        if (value instanceof Collection) {
                            q.setParameterList(name, (Collection<?>) value);
                        } else {
                            q.setParameter(name, value);
                        }
                    }
                }
            }
        }

        obj = q.iterate().next();

        if (obj instanceof Integer) {
            Integer iCount = (Integer) obj;
            totalCount = iCount.intValue();
        } else if (obj instanceof Long) {
            Long iCount = (Long) obj;
            totalCount = iCount.intValue();
        } else if (obj instanceof BigDecimal) {
            BigDecimal bg = (BigDecimal) obj;
            totalCount = bg.intValue();
        } else if (obj instanceof BigInteger) {
            BigInteger bi = (BigInteger) obj;
            totalCount = bi.intValue();
        }

    } else {
        List<Object> list = null;
        Query q = session.createQuery(queryExecutor.getSql());

        Object parameter = queryExecutor.getParameter();
        if (parameter instanceof Map) {
            Map<String, Object> params = (Map<String, Object>) parameter;

            if (params != null && params.size() > 0) {
                Set<Entry<String, Object>> entrySet = params.entrySet();
                for (Entry<String, Object> entry : entrySet) {
                    String name = entry.getKey();
                    Object value = entry.getValue();
                    if (value != null) {
                        if (value instanceof Collection) {
                            q.setParameterList(name, (Collection<?>) value);
                        } else {
                            q.setParameter(name, value);
                        }
                    }
                }
            }
        }

        list = q.list();
        if (list != null) {
            totalCount = list.size();
        }
    }

    if (totalCount == 0) {
        page.setRows(new java.util.ArrayList<Object>());
        page.setCurrentPage(0);
        page.setPageSize(0);
        page.setTotal(0);
        return page;
    }
    page.setTotal(totalCount);

    int maxPageNo = (page.getTotal() + (pageSize - 1)) / pageSize;
    if (currPageNo > maxPageNo) {
        currPageNo = maxPageNo;
    }

    Query query = session.createQuery(queryExecutor.getSql());

    Object parameter = queryExecutor.getParameter();
    if (parameter instanceof Map) {
        Map<String, Object> params = (Map<String, Object>) parameter;
        if (params != null && params.size() > 0) {
            Set<Entry<String, Object>> entrySet = params.entrySet();
            for (Entry<String, Object> entry : entrySet) {
                String name = entry.getKey();
                Object value = entry.getValue();
                if (value != null) {
                    if (value instanceof Collection) {
                        query.setParameterList(name, (Collection<?>) value);
                    } else {
                        query.setParameter(name, value);
                    }
                }
            }
        }
    }

    query.setFirstResult((currPageNo - 1) * pageSize);
    query.setMaxResults(pageSize);

    List<Object> list = query.list();
    page.setRows(list);
    page.setPageSize(pageSize);
    page.setCurrentPage(currPageNo);

    return page;
}

From source file:com.globalsight.persistence.hibernate.HibernateUtil.java

License:Apache License

/**
 * Execute the hql, return the result.//from  w  ww  . j  a va  2 s .  c o  m
 * 
 * @param hql
 *            the hql to execute.
 * @param values
 * @return
 * @throws Exception
 */
public static List<?> search(String hql, Map<String, ?> map, int first, int max) {
    List<?> result = new ArrayList<Object>();

    if (hql != null) {
        Session session = getSession();
        Query query = session.createQuery(hql);

        if (map != null) {
            Iterator<String> iterator = map.keySet().iterator();
            while (iterator.hasNext()) {
                String key = iterator.next();
                query.setParameter(key, map.get(key));
            }
        }

        if (first > 0) {
            query.setFirstResult(first);
        }

        if (max > 0) {
            query.setMaxResults(max);
        }

        result = query.list();
    }

    return result;
}

From source file:com.googlecode.fahweb.dao.TeamDao.java

License:Open Source License

/**
 * <p>listTeam.</p>//  www.  ja va  2s. c  o  m
 *
 * @param firstResult a int.
 * @param maxResults a int.
 * @return a {@link java.util.List} object.
 */
@Transactional
public List<Team> listTeam(int firstResult, int maxResults) {
    Query query = sessionFactory.getCurrentSession().createQuery("FROM Team ORDER BY score DESC");
    query.setFirstResult(firstResult);
    query.setMaxResults(maxResults);
    return query.list();
}

From source file:com.googlecode.fahweb.dao.UserDao.java

License:Open Source License

/**
 * <p>listUser.</p>// w ww .j a v a2 s.co  m
 *
 * @param firstResult a int.
 * @param maxResults a int.
 * @return a {@link java.util.List} object.
 */
@Transactional
public List<User> listUser(int firstResult, int maxResults) {
    Query query = sessionFactory.getCurrentSession().createQuery("FROM User ORDER BY newCredit DESC");
    query.setFirstResult(firstResult);
    query.setMaxResults(maxResults);
    return query.list();
}

From source file:com.googlecode.fahweb.dao.UserDao.java

License:Open Source License

/**
 * <p>listUserForTeam.</p>//from w  w  w . j av a  2  s  .c  o m
 *
 * @param team a int.
 * @param firstResult a int.
 * @param maxResults a int.
 * @return a {@link java.util.List} object.
 */
@Transactional
public List<User> listUserForTeam(int team, int firstResult, int maxResults) {
    Query query = sessionFactory.getCurrentSession()
            .createQuery("FROM User WHERE team=:team ORDER BY newCredit DESC");
    query.setFirstResult(firstResult);
    query.setMaxResults(maxResults);
    query.setInteger("team", team);
    return query.list();
}

From source file:com.googlecode.osde.internal.db.PersonTest.java

License:Apache License

@Test
public void testCreatePerson() throws Exception {
    Transaction tx = session.beginTransaction();
    Person person = new PersonImpl();
    person.setId("john.doe");
    person.setAboutMe("aboutMe1");
    person.setAge(33);//from w  w w. ja v  a  2  s .  com
    session.save(person);
    tx.commit();
    session.clear();
    //
    tx = session.beginTransaction();
    Query query = session.getNamedQuery(PersonImpl.FINDBY_PERSONID);
    query.setParameter(PersonImpl.PARAM_PERSONID, "john.doe");
    query.setFirstResult(0);
    query.setMaxResults(1);
    List<?> plist = query.list();
    person = null;
    if (plist != null && plist.size() > 0) {
        person = (Person) plist.get(0);
    }

    Assert.assertEquals("id", "john.doe", person.getId());
    Assert.assertEquals("aboutMe", "aboutMe1", person.getAboutMe());
    Assert.assertEquals("Age", new Integer(33), person.getAge());
    tx.commit();
}