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!