When we want to use the JDBC driver supports scrollable ResultSets, the Query interface can be used to obtain a ScrollableResults object that allows flexible navigation of the query results.
Query q = sess.createQuery("select student.name, student from DomesticStudent student " + "order by student.name");
ScrollableResults students = q.scroll();
if ( students.first() ) {
// Here we can find the first name on each page of an alphabetical list of cats by name
firstNamesOfPages = new ArrayList();
do
{
String name = students.getString(0);
firstNamesOfPages.add(name);
}
while ( cats.scroll(PAGE_SIZE) );
// Now get the first page of students
pageOfCats = new ArrayList();
cats.beforeFirst();
int i=0;
while( ( PAGE_SIZE > i++ ) && cats.next() ) pageOfCats.add( cats.get(1) );
}
cats.close()
We have to remember is that an open database connection and cursor is required for this functionality. Use setMaxResult()/setFirstResult() if you need offline pagination functionality.