-->
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.  [ 6 posts ] 
Author Message
 Post subject: Simple "SELECT *" by criteria
PostPosted: Tue Jan 20, 2009 11:33 am 
Newbie

Joined: Wed Jul 30, 2008 1:03 am
Posts: 16
Hello,

I have a simple findAll() code that uses criteria to do so:

Code:
Criteria crit = getSession().createCriteria(someEntity.class);
crit.list();


I'm seeing duplicates here which i don't understand since this can be translated to a plain:

Code:
select * from someEntity


So, where do the duplicates come from.

Thanks for the explanation.

Ramin


Top
 Profile  
 
 Post subject: pagination and Criteria.DISTINCT_ROOT_ENTITY
PostPosted: Tue Jan 20, 2009 12:02 pm 
Newbie

Joined: Wed Jul 30, 2008 1:03 am
Posts: 16
I think i have the explanation:

Since someEntity has a property which is a Set, there will be a left join on this other table as well and i'll get duplicates for each item of the Set.

Having said that, i can suppress the duplicates with:

Code:
setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)


but then i have yet another problem if i want to do pagination since the calls to:

Code:
setFirstResult()
setMaxResults()


are being processed on the whole duplicate result set. What is a solution for that?

Ramin


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 20, 2009 12:57 pm 
Newbie

Joined: Wed Jul 30, 2008 1:03 am
Posts: 16
OK, I seem to have solved the pagination problem by switching the fetch mode from join to subselect. Anyone sees any problem with that?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 20, 2009 5:00 pm 
Senior
Senior

Joined: Thu Jan 08, 2009 3:48 pm
Posts: 168
I had the same problem today, just with HQL instead of Criteria

http://java.dzone.com/articles/hibernat ... page=0%2C0
provided the explanation, Paging does not work together with collection fetching (to-one association fetch works fine though)
Saw the debug message shown on page 2 in my app too.

As subselect currently can't take the setMaxResults into account i saw it fall back to single queries so you might want to enable batch fetching.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 20, 2009 6:56 pm 
Newbie

Joined: Wed Jul 30, 2008 1:03 am
Posts: 16
Both setFirstResult() and setMaxResults() are working fine for me. Basically the pagination logic is working fine with subselect.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 20, 2009 8:01 pm 
Senior
Senior

Joined: Thu Jan 08, 2009 3:48 pm
Posts: 168
Please check the SQL generated for the subselect and post it

When i do a
createQuery("from SomeEntity").setMaxResults(10)
The SQL will be like
SELECT ID FROM SOME_ENTITY_TABLE where rownum <=10

Then i access a collection of SomeEntity that is mapped with subselect the SQL will read something like

SELECT FROM ENTITY_COLLECTION_TABLE WHERE ENTITY_ID IN (SELECT ID FROM SOME_ENTITY_TABLE)

Not including the rownum in the subselect.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.