Last night I spent an extended work day trying to track down the source of a Hibernate exception that I have never encountered before. The application in question is a simple data loader application that reads in an XML file populates a Hibernate object graph. The data in the XML file changes from day to day and if element exists in the XML file one day and not the next, the entity is removed from the object graph. All had been working fine until suddenly I started seeing this in my error logs:
Caused by: org.hibernate.HibernateException: Found two representations of same collection: ...
The odd thing was that there can ever be more than one representation of this collection in the application, so I had wonder WTF? I then came across this thread on the Hibernate Forums, but I wasn’t doing anything with Session.clear()
. In fact, we weren’t doing manual session management (i.e. flush, etc.).
To make a long story short, the issue was traced down to mapping error. The object hierarchy is as follows:
parent + + Component + + component attribute
The removals were being performed on the component by removing a component attribute the Component.componentAttributes
collection. The component attribute maintains a bi-directional relationship with its owning component. However, the mapping for the component attributes parent was as follows:
@NaturalId @ManyToOne(fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE }) private Component parentComponent;
Note the CascadeType.REMOVE
. This meant that when this child was removed, it’s parent would also be removed, hence the duplicate collection. The issue was resolved once the mapping was changed to:
@NaturalId @ManyToOne(fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST, CascadeType.MERGE }) private Component parentComponent;
Of course this could have been caught sooner had some unit tests been a little better, but the cause of this issue sure was a bitch to find. In end it was a subtle mapping issue that ended up getting introduced and wreaking havoc on my day. Hopefully this post can spare someone else some lost hours.
KAVIL , do you found some solution?
LikeLike
KAVIL , do you found some solution?
LikeLike
your post is really very helpful.
I am getting the below error:
“Error while saving customer: Found two representations of same collection: com.core.model.impl.ContactInfoImpl.contactTypeDetails”
[classes are: ContactInfo and ContactInfoDetails]
In Parent class:
and the child class:
I do not see any cascade code used in the hbm files. I am new to using hibernate and I have been stuck with this issue for the past 3 days. Your help is greatly appreciated.
LikeLike
Got your mappings handy?
LikeLike
your post is really very helpful.
I am getting the below error:
“Error while saving customer: Found two representations of same collection: com.core.model.impl.ContactInfoImpl.contactTypeDetails”
[classes are: ContactInfo and ContactInfoDetails]
In Parent class:
and the child class:
I do not see any cascade code used in the hbm files. I am new to using hibernate and I have been stuck with this issue for the past 3 days. Your help is greatly appreciated.
LikeLike
Got your mappings handy?
LikeLike