开发者

NonUniqueObjectException on saving joined tables

开发者 https://www.devze.com 2023-01-03 03:58 出处:网络
I have two related tables in a many to many relation: Role and Permission. The join table is RolePermission.

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! The开发者_如何学Gon 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?


I had the same problem. Removing cascade = { CascadeType.ALL } resolved the issue.

0

精彩评论

暂无评论...
验证码 换一张
取 消