views:

1036

answers:

1

I'm trying to do pagination with Hibernate using setFirstResult() and setMaxResults() but I'm not getting the expected results when setting the first result to 0.

When doing the following:

Query query = session.createQuery(queryString);  
query.setFirstResult(0);  
query.setMaxResults(30);  
List list = query.list();  //list.size() returns 10

but if I set the first result to 1 (or anything different than 0 for that matter):

query.setFirstResult(1);  
query.setMaxResults(30);  
List list = query.list();  //list.size() returns 30

I read this is a known bug in the jdbc driver, but I searched for a solution and I can't seem to find it. Has anyone run across anything similar and found a fix for it?

+1  A: 

Apparently adding setFetchSize() does the trick. So something like this now works perfectly:

query.setFirstResult(0);  
query.setMaxResults(30);  
query.setFetchSize(30);  
List list = query.list(); //list.size() now returns... wait for it... 30
Bernardo