Hibernate : Pagination

If you need to specify bounds upon your result set (the maximum number of rows you want to retrieve and/or the first row you want to retrieve), use the following methods:

Query q = session.createQuery(select * from ... order by ...");
q.setFirstResult(startPosition);
q.setMaxResults(pageSize);
List l = q.list();

Example

Query q = em.createQuery("select cat from DomesticCat cat"); 
q.setFirstResult(20); 
q.setMaxResults(10); 
List cats = q.getResultList(); //return cats from the 20th position to 29th

Hibernate knows how to translate this limit query into the native SQL of your DBMS.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s