Found shared references to a collection org.hibernate.HibernateException

nightingale2k1 picture nightingale2k1 · Nov 7, 2009 · Viewed 101.4k times · Source

I got this error message:

error: Found shared references to a collection: Person.relatedPersons

When I tried to execute addToRelatedPersons(anotherPerson):

person.addToRelatedPersons(anotherPerson);
anotherPerson.addToRelatedPersons(person);

anotherPerson.save();
person.save();

My domain:

Person {

 static hasMany = [relatedPersons:Person];

}

any idea why this happens ?

Answer

ChssPly76 picture ChssPly76 · Nov 7, 2009

Hibernate shows this error when you attempt to persist more than one entity instance sharing the same collection reference (i.e. the collection identity in contrast with collection equality).

Note that it means the same collection, not collection element - in other words relatedPersons on both person and anotherPerson must be the same. Perhaps you're resetting that collection after entities are loaded? Or you've initialized both references with the same collection instance?