简体   繁体   English

Hibernate单向多对多更新目标组成关系

[英]Hibernate Unidirectional ManyToMany Update Target Constituent Relation

There is a unidirectional ManyToMany mapping between Role and Privilege with Role as the owning entity like so 有一个单向ManyToMany之间的映射RolePrivilegeRole作为像这样的拥有实体

Role

@Entity
public class Role extends BaseEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "role_id")
    private Integer roleId;
    @Size(max = 45)
    @Column(name = "role")
    private String role;

    @JoinTable(name = "role_privilege", joinColumns = {
            @JoinColumn(name = "role_role_id", referencedColumnName = "role_id")}, inverseJoinColumns = {
            @JoinColumn(name = "privilege_privilege_id", referencedColumnName = "privilege_id")})
    @ManyToMany(
            cascade = {
                CascadeType.DETACH,
                CascadeType.MERGE,
                CascadeType.REFRESH,
                CascadeType.PERSIST }, fetch = FetchType.EAGER, targetEntity = Privilege.class)
    private Collection<Privilege> privilegeCollection;

    @Transient
    private Collection<Privilege> parentPrivilegeCollection;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "roleId")
    @JsonIgnore
    private Collection<User> userCollection;

    public Role() {
    }
    //getters,setter,hashcode,equals removed for brevity
}

Privilege

@Entity
public class Privilege extends BaseEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "privilege_id")
    private Integer privilegeId;
    @Size(max = 45)
    @Column(name = "name")
    private String name;
    @Size(max = 150)
    @Column(name = "description")
    private String description;
    @Size(max = 45)
    @Column(name = "friendly_name")
    private String friendlyName;
    @JoinTable(name = "privilege_hierachy", joinColumns = {
            @JoinColumn(name = "parent_privilege", referencedColumnName = "privilege_id")}, inverseJoinColumns = {
            @JoinColumn(name = "child_privilege", referencedColumnName = "privilege_id")})
    @ManyToMany
    private Collection<Privilege> privilegeCollection;

    public Privilege() {
    }
}

The Problem 问题

Whenever i set updated list of privileges in a role and update, the join table is successfully updated without removing either target or owning entity, and that is desired result. 每当我在角色中设置更新的特权列表并进行更新时,联接表都会成功更新,而不会删除目标实体或拥有实体,这是理想的结果。 The problem is on update it also affect another self join table in Privilege called privilege_hierachy which is not what is expect. 问题在于更新时,它还会影响Privilege另一个名为privilege_hierachy自联接表,这不是预期的。

Is it possible for hibernate to only update the Role - Privilege mant-to-many relationship and let other relation unchanged. 休眠状态是否可能仅更新“ Role - Privilege多对多关系并使其他关系不变。

Spring Data Jpa is used for data persistence Spring Data Jpa用于数据持久性

It sounds like you are updating the privileges by (removing old privileges and) adding new ones. 听起来您正在通过删除旧特权并添加新特权来更新特权。 If you do that, clearly, the second join table (the self-referencing table) could be updated with new rows, based on what you are passing. 如果这样做,很明显,第二个联接表(自引用表)可以根据您传递的内容用新行进行更新。

I see that for the self-referencing table, Privilege, you are not setting cascade type. 我看到对于自引用表Privilege,您没有设置级联类型。 It defaults to no operation, and that sounds like what you want. 它默认为无操作,这听起来像您想要的。 But my guess is based on what you said "Whenever i set updated list of privileges in a role" , and that tells me you are creating new privileges for a role, instead of using existing privileges and associate them with the role. 但是我的猜测是基于您所说的“每当我在角色中设置特权的更新列表”时 ,这告诉我您是在为角色创建新特权,而不是使用现有特权并将其与角色关联。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM