-->
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.  [ 1 post ] 
Author Message
 Post subject: delete the child in a @OneToMany that has two parents
PostPosted: Sat Jan 02, 2010 10:09 pm 
Beginner
Beginner

Joined: Wed Mar 19, 2008 12:10 am
Posts: 36
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;
   }

}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.