简体   繁体   English

HIbernate:找到同一集合的两个表示

[英]HIbernate: Found two representations of the same collection

Working with Hibernate-EntityManager 4.3.5 I'm getting the HibernateException: Found two representations of the same collection. 使用Hibernate-EntityManager 4.3.5,我得到了HibernateException:找到了同一集合的两个表示形式。

The scenario is that I have 2 entities A and B; 场景是我有两个实体A和B; both with relationship to a third entity C. A also have a relationship with B. Let's try to make it clear: 两者都与第三个实体C有关系。A也与B有关系。让我们弄清楚:

A -> OneToOne with B and C A-> OneToOne与B和C

B -> OneToMany with C B-> OneToMany与C

A and B are loaded in different transactions (Interceptors) but next I make A.setXXX(B) and do a entityManager.merge(A); A和B被加载到不同的事务(拦截器)中,但是接下来我创建A.setXXX(B)并执行一个entityManager.merge(A); -- A have an instance of C and B another, but they represent the same instance (same ID or primary key). -A有一个C实例,另一个B实例,但是它们代表相同的实例(相同的ID或主键)。 Because of that, I'm getting this error - in fact, I have 2 different instances of C (one with A and another with B) that represent the same entity (same PK). 因此,我遇到了这个错误-实际上,我有2个不同的C实例(一个带有A,另一个带有B)代表相同的实体(相同的PK)。 How to solve this? 如何解决呢? I thought of doing something like B.setXXX(a.getXXX()) but it sounds very messy. 我想到做类似B.setXXX(a.getXXX())的事情,但这听起来很混乱。 Any suggestions? 有什么建议么?

Thanks to @Tobb and @Mihir I could figure out what was going on - When I call entityManager.merge(A) I have the following situation: 多亏@Tobb和@Mihir,我才能弄清楚发生了什么-当我调用entityManager.merge(A)时,我有以下情况:

A points to B and C; A指向B和C; B points to a List of C; B指向C的列表; C points to another instance of B, which in turn points to a List of C; C指向B的另一个实例,后者又指向C的列表;

However, both instances of B represent the same object (same db PK). 但是,B的两个实例表示相同的对象(相同的db PK)。 The same applies to the lists of C, so it's fair to get the message Two representations of the same collection I'll have to remove the cascade=All in the relationship B -> C so the merge won't transverse this branch of the graph. C的列表也是如此,因此得到消息很公平同一集合的两个表示形式我必须删除关系B-> C中的cascade = All,这样合并不会越过该分支的该分支。图形。

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

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