-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Criteria setFirstResult, setMaxResults error
PostPosted: Sun Jun 18, 2006 11:16 am 
Senior
Senior

Joined: Tue Sep 13, 2005 2:01 am
Posts: 137
Session session = ((HibernateSession)em).getHibernateSession();
DetachedCriteria criteria = DetachedCriteria.forClass(Student.class);
Criteria c = dc.getExecutableCriteria(session);
c.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
List result = c.list();
List result1 = c.setFirstResult(0).setMaxResults(10).list();

result has 20 students: correct
But result1 has 4 students, not 10.

I am using JBoss 4.0.4RC1. Thanks for help.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 18, 2006 8:37 pm 
Newbie

Joined: Thu Mar 30, 2006 9:21 pm
Posts: 11
A little hard to see whats going on. I think you need to turn on hibernate sql dumping and see whats being generated. That should show you whats happening.

_________________
Derek Clarkson
Analyst Programmer
Aegeon Pty Ltd
Melbourne Australia


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 20, 2006 10:01 pm 
Senior
Senior

Joined: Tue Sep 13, 2005 2:01 am
Posts: 137
After turning on Show_SQL, and then execute the generated SQL from mysql command line, each record is duplicated in the returned result set.

For example,

(SQL statement) Limit 0, 6

the result set:
record id(s): 1, 1, 2, 2, 3, 3

So Hibernate returns 3 records because of DISTINCT_ROOT_ENTITY.

(note that student record id starts from 1, such as 1, 2, 3, 4, 5, ....)

------------
(SQL statement) Limit 2, 5

the result set:
record id(s): 2, 2, 3, 3, 4
(duplicate records(id=1) skipped)

So Hibernate returns 3 records because of DISTINCT_ROOT_ENTITY.

-------------
The generated SQL statement ends with limit ?, I could not figure out a way to find the value passed to the question mark.

For the following query:

session.createCriteria(Student.class)
.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY)
.setFirstResult(0).setMaxResults(10).list();

I expect it to return the first 10 distinct records, but only 5 (from record 1 to 5)


For
session.createCriteria(Student.class)
.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY)
.setFirstResult(10).setMaxResults(10).list();

I expect it to return the next 10 distinct records, starting from record id 11, but returned records from 6 to 10.

It seems that Hibernate applies DISTINCT_ROOT_ENTITY semantics after retriving the results from DB, but it should be the part of SQL statement in order to get correct results.

Is there a solution? Thanks a lot for help.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.