I referred Hibernate API documentation to understand the difference between list() and scroll().
ScrollableResults is like a cursor . An important feature of ScrollableResults is that it allows accessing ith object in the current row of results, without initializing any other results in the row through get(int i) function.
It also allows moving back and forth the result set using next() and previous() function.
Example :
ScrollableResults sc = session.createQuery("select e.employeeName,e.employeeDept FROM Employee e").scroll(ScrollMode.SCROLL_INSENSITIVE);
while(sc.next()) {
String empName = (String)sc.get(0);
System.out.println(empName);
String empdept = (String)sc.get(1);
System.out.println(empdept);
}
The output of above programm will values of employeeName and employeeDept .
Now suppose you want to get the last record of the resultSet. With list() you would need to iterate the whole result. With ScrollableResults, you can use last()
function.
Note that for ScrollableResults sc = session.createCriteria(Employee.class).scroll();
should be iterated as
while(sc.next()) {
Employee emp = (Employee)sc.get(0);
System.out.println(emp.getname);
}