-->
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.  [ 2 posts ] 
Author Message
 Post subject: Member collection of an entity has duplicates after query
PostPosted: Thu Jun 22, 2006 5:56 am 
Newbie

Joined: Tue Jan 31, 2006 2:30 am
Posts: 12
Hibernate version:
Hibernate 3.1.1
Hibernate Annotations 3.1 beta8

This is related to a previous problem i posted.
http://forum.hibernate.org/viewtopic.php?t=960677

I have defined 5 classes using annotations as follows. Note that the following members are eager fetched:
1. contactNumbers of the Person class
2. cards of the Membership class


Code:
@Entity
public class ContactNumber
{
    @NotNull
    String number;

    String description;
}


@Entity
public class Person
{
...

   @OneToMany(fetch = FetchType.EAGER)
   @Cascade( { CascadeType.ALL, CascadeType.DELETE_ORPHAN })
   @JoinTable(name = "person_contactnumber", joinColumns = @JoinColumn(name = "person_id"), inverseJoinColumns = @JoinColumn(name = "contactNumber_id"))
   private List<ContactNumber> contactNumbers = new ArrayList<ContactNumber>();

...
}



@Entity
public class Card
{
...

  @ManyToOne
  @Cascade( { CascadeType.MERGE, CascadeType.PERSIST, CascCascadeType.SAVE_UPDATE })
  @JoinColumn(name = "membership_id")
  private Membership      membership;

  private String cardId;

...
}



@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Membership
{
...

  @OneToMany(mappedBy = "membership")
  @Cascade( { CascadeType.ALL, CascadeType.DELETE_ORPHAN })
  private List<Card>   cards   = new ArrayList<Card>();

...
}


@Entity
public class PersonalMembership extends Membership
{
...

  @ManyToOne(fetch = FetchType.EAGER)
  @Cascade( { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.SAVE_UPDATE })
  private Person   person;

...
}






I input the following test data for the Person and ContactNumber classes into the database. Note that i enter 2 contactNumbers for the Person object created.

Code:
Person person = new Person();
List<ContactNumber> list= new ArrayList<ContactNumber>(2);

ContactNumber number = new ContactNumber();
number.setNumber("9111111");
number.setDescription("Work");
list.add(number);

number= new ContactNumber();
number.setNumber("1234567");
number.setDescription("Test");
list.add(number);

person.setContactNumbers(list);

session.save(person);


So the database now has 1 Person entry and 2 ContactNumbers for that Person.

Continuing, i input the test data for the Membership classes

Code:
List<Card> cardlist = new ArrayList<Card>(2);
Card card = new Card();
card.setCardId("A123");
cardlist.add(card);

card = new Card();
card.setCardId("B456");
cardlist.add(card);

PersonalMembership member = new PersonalMembership();
member.setPerson(person);
member.setCards(cardlist);

session.save(member);


The database will now have 1 PersonalMembership entry having 2 cards. And the PersonalMembership is associated with the created Person entry.

Then when I try to get the PersonalMembership using the following queries

Code:
Long id = member.getId();

session.get(PersonalMembership.class, id);

// or

session.createCriteria(PersonalMembership.class).add(Restrictions.idEq(id)).uniqueResult();


The cards collection now has a total of 4 items in the list even though there are only 2 entries in the database. When I checked the list, the extra 2 items are simply duplicates of the original 2 cards.

I observed the following behaviors:
1. When I associate the PersonalMembership with the Person class, done by calling the line "member.setPerson(person);", the list of cards i get after the query is multiplied by the number of contactNumbers in the database. So, from my test data, there are 2 contactNumbers and 2 cards, which is why I get 4 items in the list.

2. When I DO NOT associate the PersonalMembership with the Person class, the list of cards i get after the query is correct, which is 2.


Can someone explain why is this happening? Or am I using hibernate incorrectly? Is there another way to query the database such that it doesnt give me multiple items in the list for the Cards?

Thanks!

_________________
When all else fails - fresh tactics!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 22, 2006 6:44 am 
Regular
Regular

Joined: Tue Nov 16, 2004 6:36 pm
Posts: 62
Location: Zürich
This is in the faq: no there is no other way. It is a drawback of eagerly loading (joining) collections. You must put the result in a Set to remove duplicates.

For the same reason, fetching many collections eagerly is a bad idea: the intermediate ResultSet will explode in size (as is also the case in plain JDBC) making it more expensive in many cases than processing multiple queries.


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