java.lang.IllegalStateException:具有@ManyToMany 3 个实体的同一实体的多个表示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26591521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.IllegalStateException: Multiple representations of the same entity with @ManyToMany 3 entities
提问by user1188867
I have 3 entities with ManyToMany relationships:
我有 3 个具有多对多关系的实体:
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:
我做了以下事情:
I have created 3 functionalities:
Functionality1, Functionality2, Functionality3
Then created 2 permissions:
Permission1 with Functionality1, Functionality2 Permission2 with Functionality2, Functionality3
Then created a role:
Role1 with Permission1 and Permission2
我创建了 3 个功能:
Functionality1, Functionality2, Functionality3
然后创建了2个权限:
Permission1 with Functionality1, Functionality2 Permission2 with Functionality2, Functionality3
然后创建了一个角色:
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]
java.lang.IllegalStateException:正在合并同一实体 [com.persistence.entity.admin.Functionality#1] 的多个表示。分离:[com.persistence.entity.admin.Functionality@4729256a];分离:[com.persistence.entity.admin.Functionality@56ed25db]
采纳答案by user1188867
Fixed it by removing CascadeType.MERGE on Permission entity
通过删除权限实体上的 CascadeType.MERGE 修复它
回答by Suraj Menon
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:
正确的解决方案是升级到 hibernate 4.2.15 / 4.3.6 或更高版本,并将以下几行添加到您的 persistence.xml 中:
<property name="hibernate.event.merge.entity_copy_observer" value="allow"/>
<property name="hibernate.event.merge.entity_copy_observer" value="allow"/>
回答by Beagle
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:
只是说明我在 Spring MVC 应用程序中使用 Hibernate Core 4.3.8,基于 Spring Core 4.1.6。解决方法:
<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.
没有为我工作。我需要删除 CascadeType.MERGE 以正确填充 @ManyToMany。不确定较新版本的 Hibernate 是否已修复此问题。
回答by user1523177
Check your equals and hashCode method, ensure that it is consistent and correctly defined. For example I had copied and mistakenly pasted another-class when computing hashCode, this caused the object never to be equal with itself :(.
检查您的 equals 和 hashCode 方法,确保它是一致且正确定义的。例如,我在计算 hashCode 时复制并错误地粘贴了另一个类,这导致对象永远不会与自身相等:(。
回答by Asif Aminur Rashid
In my case, moving the fetch operation and save operation in same @Transactional block solved the problem.
就我而言,在同一个 @Transactional 块中移动 fetch 操作和 save 操作解决了这个问题。
回答by razi
error occurs when we have multiple object of same time in HashSet.Possible due to incorrect hash function.Hashset check equality of object on the basis of hash function between two objects.
当我们在 HashSet 中同时有多个对象时会发生错误。可能由于不正确的哈希函数。Hashset 根据两个对象之间的哈希函数检查对象的相等性。
Way to debug
调试方式
Just try to print hashset you will see multiple object of same type.
只需尝试打印哈希集,您就会看到多个相同类型的对象。
Solution::#
解决方案::#
- Use HashSet while defining one to many relationships.
- Avoid using Lists.
- Make sure your hash function should be correct.
- 在定义一对多关系时使用 HashSet。
- 避免使用列表。
- 确保您的哈希函数应该是正确的。
回答by W.Man
I ran into the same problem too and solved it by add a few configs in application.yaml
files.
我也遇到了同样的问题,并通过在application.yaml
文件中添加一些配置来解决它。
jpa:
properties:
hibernate:
enable_lazy_load_no_trans: true
event:
merge:
entity_copy_observer: allow
回答by Paul Linux
@LazyCollection(LazyCollectionOption.FALSE
@LazyCollection(LazyCollectionOption.FALSE
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "translate_id")
@JoinColumn(name = "translate_id")
回答by Arul Rozario
I could fix it by replacing
我可以通过更换来修复它
cascade = CascadeType.All
with
和
casecade={CascadeType.PERSIST,CascadeType.REMOVE}