-->
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: Criteria Query and Duplicate Results
PostPosted: Mon Aug 08, 2005 4:46 pm 
Newbie

Joined: Mon Aug 08, 2005 4:16 pm
Posts: 15
Hi,

I am using Hibernate 3.0 with Oracle 9i. I have four entities with the following configuration,

Entities
******

1. SchemeMember
2. SchemeMemberConfigurationGroup
3. ConfigurationGroup
4. ValidResponse

Relationships
***********

1. SchemeMember -> SchemeMemberConfigurationGroup : one-to-many bidirectional.
2. SchemeMemberConfigurationGroup -> ConfigurationGroup : many-to-one bidirectional
3. ConfigurationGroup -> ValidResponse : one-to-many bidirectional

Code
*****

List findBySchemeMember(SchemeMember schemeMember) {

Criteria criteria = getSession().createCriteria(SchemeMemberConfigurationGroup.class);
criteria.add(Restrictions.eq("schemeMember", schemeMember));
criteria.setFetchMode("configurationGroup", FetchMode.JOIN);
criteria.setFetchMode("configurationGroup.validResponses", FetchMode.JOIN);
return criteria.list();

}

Problem
*******

The code above returns duplicate SchemeMemberConfigurationGroup objects with the same primary key. For example if a given SchemeMember has two SchemeMemberConfigurationGroup objects and the ConfigurationGroup object associated with each SchemeMemberConfigurationGroup objects has two ValidResponse objects, the query returns four SchemeMemberConfigurationGroup objects instead of two. However, if I comment out the line that sets the fetch mode for the association path configurationGroup.validResponses, the query returns the correct number of SchemeMemberConfigurationGroup objects which is two. This is not a desirable option as this will result in n + 1 reads.

Any help on this will be highly appreciated.

Ta
Abu Mariam


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 08, 2005 5:01 pm 
Regular
Regular

Joined: Thu May 26, 2005 2:08 pm
Posts: 99
Try this:

Code:
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);


Top
 Profile  
 
 Post subject: Re: Criteria Query and Duplicate Results
PostPosted: Mon Aug 08, 2005 5:04 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
gurahbah wrote:
Hi,

I am using Hibernate 3.0 with Oracle 9i. I have four entities with the following configuration,

Entities
******

1. SchemeMember
2. SchemeMemberConfigurationGroup
3. ConfigurationGroup
4. ValidResponse

Relationships
***********

1. SchemeMember -> SchemeMemberConfigurationGroup : one-to-many bidirectional.
2. SchemeMemberConfigurationGroup -> ConfigurationGroup : many-to-one bidirectional
3. ConfigurationGroup -> ValidResponse : one-to-many bidirectional

Code
*****

List findBySchemeMember(SchemeMember schemeMember) {

Criteria criteria = getSession().createCriteria(SchemeMemberConfigurationGroup.class);
criteria.add(Restrictions.eq("schemeMember", schemeMember));
criteria.setFetchMode("configurationGroup", FetchMode.JOIN);
criteria.setFetchMode("configurationGroup.validResponses", FetchMode.JOIN);
return criteria.list();

}

Problem
*******

The code above returns duplicate SchemeMemberConfigurationGroup objects with the same primary key. For example if a given SchemeMember has two SchemeMemberConfigurationGroup objects and the ConfigurationGroup object associated with each SchemeMemberConfigurationGroup objects has two ValidResponse objects, the query returns four SchemeMemberConfigurationGroup objects instead of two. However, if I comment out the line that sets the fetch mode for the association path configurationGroup.validResponses, the query returns the correct number of SchemeMemberConfigurationGroup objects which is two. This is not a desirable option as this will result in n + 1 reads.

Any help on this will be highly appreciated.

Ta
Abu Mariam


This question has been posted more than once before and I think the answer is usually to add the results to a Set which will remove the duplicates.

I don't remember the rationale for this being what is returned.

http://forum.hibernate.org/viewtopic.ph ... esults+set

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 10, 2005 1:01 am 
Newbie

Joined: Tue Jun 21, 2005 5:44 pm
Posts: 6
Location: Cd. Madero, México
jdl wrote:
Try this:

Code:
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);


jdl Wins!!

Thanks buddy! You just saved me from using manually concatenated HQL out of frustration from the duplicates.

Great Tip!


Top
 Profile  
 
 Post subject: Thanks
PostPosted: Fri Aug 12, 2005 4:54 am 
Newbie

Joined: Mon Aug 08, 2005 4:16 pm
Posts: 15
Thanks, setting the results transformer fixed the problem.


Top
 Profile  
 
 Post subject: How does this apply to EJB3
PostPosted: Thu Sep 15, 2005 6:23 pm 
Newbie

Joined: Thu Apr 29, 2004 1:09 pm
Posts: 4
Is the a similar approach when doing a fetch join with EJB3 EQL that preserves the order?


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.