0

I have some problems with the Hibernate criteria API. I want to get rows of a table as objects, but limit the amount of returned results. Here is the code:

Criteria c = session.createCriteria(User.class);
c.setFirstResult(start);
c.setMaxResults(end-start);
c.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
List<User> test = c.list();

First result is in my case 0 and max results 10. There are 3 users in the db. The problem is that only the first row of the database is in the result list. If I don't use the resultTransformer, the first row is 10 times (maxResults) in the list. If i dont't use max results and resultTransformer, the first row is about 100 times in the result list.

If I add an restriction for a specific user, the result list contains the specific user, so this it is clear that not only the first row could be found because of some strange circumstances.

Please help, I'm clueless.

Mikko Maunu
  • 41,366
  • 10
  • 132
  • 135
smotron
  • 107
  • 1
  • 10
  • Ok, I just came one step closer to the solution. Every user object if loaded about 40 times from the database. Since only 10 results were loaded because of maxResults and the Criteria.DISTINCT_ROOT_ENTITY is executed afterwards only the first user is left. If I set maxResults to 50, I get 2 users, and so on. Why do I get the user objects that often and is there a solution for this problem? – smotron Apr 25 '13 at 12:28
  • Could solve it with the workaround here http://stackoverflow.com/questions/2183617/criteria-api-returns-a-too-small-resultset suggested by Kim L . – smotron Apr 25 '13 at 13:06

0 Answers0