I have two related tables in a many to many relation: Role
and Permission
. The join table is RolePermission
.
The Role
object has a list of permission. it looks like this:
private Set<Permission> Permissions;
@ManyToMany(fetch = FetchType.EAGER, cascade = { CascadeType.ALL })
@JoinTable(name = "permission_role", joinColumns = @JoinColumn(name = "role_id"),
inverseJoinColumns = @JoinColumn(name = "permission_id"))
public Set<ObjectPermission> getObjectPermissions() {
return objectPermissions;
}
In a certain time I want to add permissions to a role:
role.getObjectPermissions().add(permission);
roleDAO.saveOrUpdate(role);
Both the role and the permission exist already in the db, each in its table. I want to add a record to the relation table, that will point both on the role and the permission:
role.addObjectEntryPermissionRole(permission);
roleDAO.saveOrUpdate(role);
The problem is that it looks like it's trying to save the permission itself! Then it throws this error:
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.XXX.model.Permission#11]
Could somebody tell me what's the problen? what did I do wrong?