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