ManyToMany bidirectional deletion [message #640753] |
Mon, 22 November 2010 20:56  |
Eclipse User |
|
|
|
Hi guys,
I have a relatively straight-forward problem. I have two classes: user and security role. Users have security roles and security roles have users.
In the User class I have the following:
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.MERGE})
@JoinTable(name = "person_security_role",
joinColumns = {@JoinColumn(name = "person_key")},
inverseJoinColumns = {@JoinColumn(name = "security_role_key")})
private List<SecurityRole> securityRoles;
In the SecurityRole class I have the following:
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.MERGE}, mappedBy = "securityRoles")
private List<User> users;
Now when I run the following test code I get two lists with different sizes:
User user = new User();
SecurityRole securityRole = new SecurityRole();
user.addSecurityRole(securityRole);
user = sb.updateFlush(user);
securityRole = user.getSecurityRoles().get(0);
user.getSecurityRoles().remove(0);
user = sb.updateFlush(user);
int userSecurityRolesSize = user.getSecurityRoles().size();
int securityRoleUsersSize = em.find(SecurityRole.class, securityRole.getSecurityRoleKey()).getUsers().size();
The list of users on the SecurityRole object has a size of 1, and the list of securityRoles on the user has the size of 0.
How do I make it so Eclipselink goes out and deletes the orphan from the list on the other side of the relationship without having to go over there and delete it myself?
Thanks!
|
|
|
|
|
|
Re: ManyToMany bidirectional deletion [message #641294 is a reply to message #641290] |
Wed, 24 November 2010 13:54  |
Eclipse User |
|
|
|
I know this all too well as I have done this in the past. I had to add an annotation for relationships that should not be loaded when the object graph was being traversed. It may be more of a PITA to do this again than to just add the remove methods. Thanks for the help, though, everything works great with the cache off. I am going to stick with it off until I have time to revisit this defect and do it the proper way. Appreciate the help!
|
|
|
Powered by
FUDForum. Page generated in 0.07229 seconds