I found a possible solution, but ... I do not know if this is a good solution.
@Entity public class Role extends Identifiable { @ManyToMany(cascade ={CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}) @JoinTable(name="Role_Permission", joinColumns=@JoinColumn(name="Role_id"), inverseJoinColumns=@JoinColumn(name="Permission_id") ) public List<Permission> getPermissions() { return permissions; } public void setPermissions(List<Permission> permissions) { this.permissions = permissions; } } @Entity public class Permission extends Identifiable { @ManyToMany(cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}) @JoinTable(name="Role_Permission", joinColumns=@JoinColumn(name="Permission_id"), inverseJoinColumns=@JoinColumn(name="Role_id") ) public List<Role> getRoles() { return roles; } public void setRoles(List<Role> roles) { this.roles = roles; }
I tried this and it works. When you delete a Role, relations (but not Permission objects) are also deleted, and when you delete Permission, relations with a role are also deleted (but not an instance of Role). But we map the unidirectional relationship two times, and both objects are the owners of the relationship. Could this cause some problems for hibernation? What type of problems?
Thank!
The code above refers to another post.
jelies Aug 26 '10 at 11:59 on 2010-08-26 11:59
source share