Skip to main content

How to get results from a fixed position in Hibernate Criteria Query

Points To Remember


  • Offset may be required to do pagination or get the results after a particular result from the database.
  • Using offset you can define the first result to be included in the result set from the database. 
  • You can use setFirstResult on the CriteriaQuery to set the first result that you want.

Using offset to get the results

SampleDAO.java
@SuppressWarnings("unchecked")
public List<Person> getCategories(Integer offset){
return getSession()
.createCriteria(Person.class)
.setFirstResult(offset!=null?offset:0)
.setMaxResults(10)
.list();
}
This is how you can set the first result you want to fetch from the database. In this example we have set the first result and the maximum number of records that we need to fetch.

Comments