Scrollable iteration in Hibernate by R4R Team

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.
Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!