views:

1750

answers:

2

Hi,

I got this error message : error: Found shared references to a collection: Person.relatedPersons

when I tried to save addToRelatedPersons(anotherPerson) :

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

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

my domain :

Person {

 static hasMany = [relatedPersons:Person];

}

any idea why this happens ?

A: 

Can you also post your entity classes. Are you by any change mapping a property multiple times from different entity class?

surajz
+3  A: 

Hibernate shows this error when you attempt to persist more than one entity instance sharing the same collection reference.

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?

ChssPly76
found the problem. I did a mistake by typing person.relatedPerson = anotherPerson ; somewhere in the code .... doh.
nightingale2k1