Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Found shared references to a collection org.hibernate.HibernateException

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 ?

like image 911
nightingale2k1 Avatar asked Nov 07 '09 12:11

nightingale2k1


3 Answers

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?

like image 85
ChssPly76 Avatar answered Oct 14 '22 20:10

ChssPly76


I had the same problem. In my case, the issue was that someone used BeanUtils to copy the properties of one entity to another, so we ended up having two entities referencing the same collection.

Given that I spent some time investigating this issue, I would recommend the following checklist:

  • Look for scenarios like entity1.setCollection(entity2.getCollection()) and getCollection returns the internal reference to the collection (if getCollection() returns a new instance of the collection, then you don't need to worry).

  • Look if clone() has been implemented correctly.

  • Look for BeanUtils.copyProperties(entity1, entity2).

like image 67
dgt Avatar answered Oct 14 '22 20:10

dgt


Explanation on practice. If you try to save your object, e.g.:

Set<Folder> folders = message.getFolders();
   folders.remove(inputFolder);
   folders.add(trashFolder);
   message.setFiles(folders);
MESSAGESDAO.getMessageDAO().save(message);

you don't need to set updated object to a parent object:

message.setFiles(folders);

Simple save your parent object like:

Set<Folder> folders = message.getFolders();
   folders.remove(inputFolder);
   folders.add(trashFolder);
   // Not set updated object here
MESSAGESDAO.getMessageDAO().save(message);
like image 7
Mirimas Avatar answered Oct 14 '22 20:10

Mirimas