我有3個實體與多對多的關係如下圖所示:java.lang.IllegalStateException:與@ManyToMany 3個實體相同的實體的多種表示
角色實體:
@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<>();
Functionalit Ÿ實體:
@Entity
public class Functionality {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
我做了以下內容:
我已經創建了3種功能。
functionality1, functionality2, functionality3
然後創建2個權限:
a. Permission 1 with functionality1, functionality2. b. Permission 2 with functionality2, functionality3.
然後在創建角色:
Role 1 with Permission1 and Permission2 iam getting following exception
java.lang.IllegalStateException:同一實體的多種表示[com.persistence.entity.admin.Functionality#1]正在合併。獨立:[[email protected]];獨立:[email protected]]
嘗試使用Hibernate級聯註釋而不是JPA註釋。例如,@ Cascade(CascadeType.SAVE_UPDATE )會取代{CascadeType.PERSIST,CascadeType.MERGE}。查閱本文:[JPA&Hibernate註解常見錯誤](https://www.mkyong。com/hibernate/cascade -jpa-hibernate-annotation-common-errors /) – 2017-05-21 20:26:00