@ManyToMany:如何在表A,连接表中插入时避免表B中的额外更新

时间:2011-10-25 13:42:39

标签: hibernate hibernate-mapping

我有两张桌子:

权限权限他们有多对多关系

class Authority{

@ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "authority_permission", joinColumns = { @JoinColumn(name = "authority_id", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "permission_id", nullable = false, updatable = false) })
    private List<Permission> permissions = new ArrayList<Permission>(0);

}

有些时候我需要创建一个新权限并为其分配一些现有权限插入 权限中的数据 authority_permission 仅限):

List<Permission> permissions=new ArrayList<Permission>;
Permission permission=permissionDao.getPermissionById(1); 
permissions.add(permission);   
authority.setPermissions(permissions)
sessionFactory.getCurrentSession().save(authority).

上面的代码,工作正常,但我注意到hibernate在权限表中进行了额外更新,这是不需要的,因为权限表中没有更改:

Hibernate: insert into authority (description, display_name, is_default, name, authority_id) values (?, ?, ?, ?, ?)
Hibernate: update permission set description=?, display_name=?, name=? where permission_id=?
Hibernate: insert into role_permission (role_id, permission_id) values (?, ?)

如何避免这种额外更新的任何想法?

1 个答案:

答案 0 :(得分:0)

更改级联类型注释