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#...
Reply to the post :
http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&a...