简体   繁体   中英

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.

The scenario is that I have 2 entities A and B; both with relationship to a third entity C. A also have a relationship with B. Let's try to make it clear:

A -> OneToOne with B and C

B -> OneToMany with C

A and B are loaded in different transactions (Interceptors) but next I make A.setXXX(B) and do a entityManager.merge(A); -- A have an instance of C and B another, but they represent the same instance (same ID or primary key). 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). How to solve this? I thought of doing something like B.setXXX(a.getXXX()) but it sounds very messy. 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:

A points to B and C; B points to a List of C; C points to another instance of B, which in turn points to a List of C;

However, both instances of B represent the same object (same 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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