[jboss-user] [EJB 3.0] - Many-to-Many and cascade pronlem ?

ericmacau do-not-reply at jboss.com
Sun Apr 15 10:08:36 EDT 2007


Hello,


In the following code, User and Role. When I tried to remove User, it will remove all the roles in the ROLE that the user has and also all the related records in the USER_ROLE_LINK.

For example,


User u = new User();
Role a = new Role("A");
Role b = new Role("B");


// persist a and b

ArrayList roles = new ArrayList();
roles.add(a);
roles.add(b);

u.setRoles(roles);

//persist u


.....


em.remove(u);




For the above codes, it wiil cause the problem that I described above.
How can I only remove the User and the related records in the USER_ROLE_LINK when tried to remove a single user?

And how can I remove all the users that a role have when remove a ROLE ?




  | public class User {
  | 
  | ...
  | @ManyToMany (cascade=CascadeType.ALL,
  | 	mappedBy = "users", targetEntity = Role.class)
  | public Collection<Role> getRoles() {
  | 	return roles;
  | }
  | 
  | ...
  | 
  | }
  | 
  | 



  | public class Role {
  | ...
  | @ManyToMany (fetch = FetchType.EAGER, targetEntity = User.class, cascade=CascadeType.REMOVE)
  | @JoinTable (name = "USER_ROLE_LINK", joinColumns = {
  |    @JoinColumn (name = "ROLENAME")
  | }, inverseJoinColumns = {@JoinColumn (name = "USERNAME")
  | })
  | public Collection<User> getUsers() {
  |    return users;
  | }
  | 
  | ...
  | }
  | 
  | 




View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4037370#4037370

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4037370



More information about the jboss-user mailing list