java.lang.IllegalStateException: Multiple represen

2020-01-24 13:19发布

I have 3 entities with ManyToMany relationships:

Role Entity:

@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>();
}

Permission Entity:

@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<>();
}

Functionality Entity:

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

I did the following:

  1. I have created 3 functionalities:

    Functionality1, Functionality2, Functionality3
    
  2. Then created 2 permissions:

    Permission1 with Functionality1, Functionality2
    
    Permission2 with Functionality2, Functionality3
    
  3. Then created a role:

    Role1 with Permission1 and Permission2 
    

I am getting the following exception:

java.lang.IllegalStateException: Multiple representations of the same entity [com.persistence.entity.admin.Functionality#1] are being merged. Detached: [com.persistence.entity.admin.Functionality@4729256a]; Detached: [com.persistence.entity.admin.Functionality@56ed25db]

11条回答
干净又极端
2楼-- · 2020-01-24 13:22

The correct solution would have been to upgrade to hibernate 4.2.15 / 4.3.6 or above and add the following lines to your persistence.xml:

<property name="hibernate.event.merge.entity_copy_observer" value="allow"/>

查看更多
干净又极端
3楼-- · 2020-01-24 13:22

Just a note to say I am using Hibernate Core 4.3.8 in a Spring MVC application, based on Spring Core 4.1.6. The workaround:

<property name="hibernate.event.merge.entity_copy_observer" value="allow"/>

Did not work for me. I needed to remove the CascadeType.MERGE in order to correctly populate an @ManyToMany. Not sure if newer versions of Hibernate have fixed this.

查看更多
兄弟一词,经得起流年.
4楼-- · 2020-01-24 13:23

I could fix it by replacing

cascade = CascadeType.All

with

casecade={CascadeType.PERSIST,CascadeType.REMOVE}
查看更多
Lonely孤独者°
5楼-- · 2020-01-24 13:26

Fixed it by removing CascadeType.MERGE on Permission entity

查看更多
\"骚年 ilove
6楼-- · 2020-01-24 13:26

For Hibernate see the workaround here HHH-9106.

查看更多
淡お忘
7楼-- · 2020-01-24 13:31

@LazyCollection(LazyCollectionOption.FALSE

@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)

@JoinColumn(name = "translate_id")

查看更多
登录 后发表回答