My entity classes are provided below.
My 3 questions deal with deleting either the parent or child in a OneToMany relationship when the child also happens to have an additional 2nd parent that also holds a reference to it (See below for my entities with annotations and cascade rules)
1) If I run the following cascade rules would try to delete the associated clubMembers, but would that action throw a foreign key constraint exception because Club will have a reference to it as well through the ClubMember's club property?
Code:
User user = // Load a User
session.delete(user);
entityManager.remove(user);
2) Lets say I retrieve a User's clubMembers set and then i iterate through it and when i find the one I want to delete I call session.delete(clubMember)
Will this cause an exception (with my current cascade and association annotations) because it will have expected me to also to have done iterator.remove() for the clubMember in both the User and Club objects?
3) Are there other potential problems that I need to look out for when trying to delete an item with multiple owners?
Code:
@Entity
public class User {
@OneToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE}, mappedBy="user")
public Set<ClubMember> getJoinedClubs() {
return joinedClubs;
}
}
@Entity
public class Club {
..
@OneToMany(mappedBy="club")
public set<ClubMember> getClubMembers() { .. }
@ManyToOne
@JoinColumn(name="userId")
public Club getClub{
return club
};
}
@Entity
public class ClubMember {
...
@ManyToOne
@JoinColumn(name="userId")
public User getUser() {
return user;
}
@ManyToOne
@JoinColumn(name="clubId")
public Club getClub() {
return club;
}
}