小编典典

java.lang.IllegalStateException:具有@ManyToMany 3个实体的同一实体的多个表示形式

hibernate

我有3个具有ManyToMany关系的实体:

角色实体:

@Entity
public class Role {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer roleID;
    private String roleName;
    private String description;

    @ManyToMany(cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}, fetch = FetchType.EAGER)
    @JoinTable(name = "role_permission", joinColumns = {@JoinColumn(name = "role_id")}, inverseJoinColumns = {@JoinColumn(name = "permission_id")})
    private Set<Permission> permissions = new LinkedHashSet<Permission>();
}

权限实体:

@Entity
public class Permission {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int permissionID;
    private String permissionName;
    private String description;

    @ManyToMany(cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}, fetch = FetchType.EAGER)
    @JoinTable(name = "permission_functionality", joinColumns = {@JoinColumn(name = "permission_id")}, inverseJoinColumns = {@JoinColumn(name = "functionality_id")})
    private Set<Functionality> functionalities = new LinkedHashSet<>();
}

功能实体:

@Entity
public class Functionality {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private String name;
}

我做了以下事情:

  1. 我创建了3个功能:

    Functionality1, Functionality2, Functionality3
    
  2. 然后创建2个权限:

    Permission1 with Functionality1, Functionality2
    

    Permission2 with Functionality2, Functionality3

  3. 然后创建一个角色:

    Role1 with Permission1 and Permission2
    

我收到以下异常:

java.lang.IllegalStateException:同一实体[com.persistence.entity.admin.Functionality#1]的多个表示形式正在被合并。分离:[com.persistence.entity.admin.Functionality@4729256a];
分离:[com.persistence.entity.admin.Functionality@56ed25db]


阅读 319

收藏
2020-06-20

共1个答案

小编典典

通过删除权限实体上的CascadeType.MERGE修复了此问题

2020-06-20