-->
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.  [ 4 posts ] 
Author Message
 Post subject: when lazy=true,"left join fetch" gets duplicates
PostPosted: Tue May 15, 2007 9:34 am 
Newbie

Joined: Mon May 14, 2007 9:54 pm
Posts: 3
Hi,

I have been confused with this problem for two days:(

Hibernate version:3
When lazy=false in xml,excute following code:
Code:
result = s.createCriteria(JtsUser.class)         
              .add(Restrictions.idEq(2))   
              .list();
result.size() is 1, this is correct.
But when lazy=true in xml,excute following in java:
Code:
result = s.createCriteria(JtsUser.class)         
              .setFetchMode("jtsHistories", FetchMode.JOIN)   
              .add(Restrictions.idEq(2))   
              .list();

result.size() is 4, 4 same objects! This is unreasonable because id is PK.

"jtsHistories" is a set of "JtsUser". The JtsUser that id=2 contains 4 records of jtsHistory(if 5, then 5 same objects).


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 15, 2007 11:45 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
It's the join that screwing your results up. Add "setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)" to eliminate the one-root-entity-per-returned-row phenomenon that you're seeing.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 16, 2007 2:44 am 
Newbie

Joined: Mon May 14, 2007 9:54 pm
Posts: 3
tenwit wrote:
It's the join that screwing your results up. Add "setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)" to eliminate the one-root-entity-per-returned-row phenomenon that you're seeing.


Thanks a lot! But the "phenomenon" is... Is it a bug, or of some usage?


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 16, 2007 3:20 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
It's the correct result for the default result transformer. I don't know why DISTINCT_ROOT_ENTITY isn't the default one, as it's the one most people want most of the time, but there you go.

_________________
Code tags are your friend. Know them and use them.


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