10

I am getting an error:

Don't change the reference to a collection with cascade="all-delete-orphan"

while trying the following operation:

beginTx();
Parent parent = new Parent();
Child child = new Child();
parent.addChild(child);
getSession().save(parent);
commitTx();
closeSession();

beginTx();
//id is the primary key
child.setID(null);
getSession().update(child);
commitTx();
closeSession();

Parent and child are related by one-to-many with cascade = 'all-delete-orphan'.

class Parent {
Set child;
}


<set name="child" table="Child" cascade="all-delete-orphan" inverse="true">
    <key column="FK"></key>
    <one-to-many class="Child"/>
</set>

Any idea why this exception is being thrown? Why is setting null on the primary key causing this exception even though the entity is in detached state ?

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
user2599052
  • 1,056
  • 3
  • 12
  • 27

4 Answers4

10

This exception normally happens if you load an entity having a collection with cascade=all-delete-orphan, and then you remove the reference to the collection.

Don't replace this collection. Always use collection.clear() to remove all the associated child entries so that the orphan-deletion algorithm can detect the changes. And if you want to remove any particular child, you just need to remove it from the collection. Once it is removed from the collection, it will be considered as an orphan and will be deleted.

Debojit Saikia
  • 10,532
  • 3
  • 35
  • 46
  • 2
    But if you look at the snippet, I have no where replaced the collection. I am just setting null on the primary key. – user2599052 Sep 20 '13 at 08:24
  • 1
    That's why I started my ans with "This exception normally happens".. but why are you setting null on the primary key of the child entity? – Debojit Saikia Sep 20 '13 at 08:57
  • The aim is to make the object transient and not do that by removing from collection. – user2599052 Sep 20 '13 at 09:04
  • 2
    How would that be possible? I am assuming this.correct me if I am wrong.`make the object transient`:Hibernate will treat it as a new child and will try to persist it. It is same as adding a new child to the set. You want to make it transient but you don't want to remove it from the set. Its like you want to change the ID of a persistent object.But the definition of primary key says that it is not only unique but also constant throughout the life of the row. Hibernate does not support changing identifier values. This could be reason for this exception. – Debojit Saikia Sep 20 '13 at 09:33
  • By making it null, it should be transient, then Hibernate should assign a new key on insertion. And the transient object has to be added to collection of parent and the entities are detached. This what the above code is attempting to do. – user2599052 Sep 20 '13 at 09:37
  • 2
    but i am still confused !! Anyways, If you want to add a new child, create and add one. If you want to update an existing one, you just need to update its properties. And if you want to remove one, just remove it from the collection. During `commitTx()`, changes will be synchronized with DB. I don't see any need of making a persistent object transient and then again adding it back to the collection. – Debojit Saikia Sep 20 '13 at 09:55
  • This exact scenario happened when I tried to copy a child object to another parent. I dont find any other solution other than implement `.clone()` on my child object (luckily only a few field) – Joshua H Mar 31 '16 at 08:45
3

for the exception Don't change the reference to a collection with cascade="all-delete-orphan".you use child.setID(null); change the ID cause the exception.

hibernate use PersistentSet to execute the exception check,so you can do this:

child.setID(null);
parent.child=new HashSet(parent.child)

use HashSet to replace PersistentSet

peacetrue
  • 186
  • 1
  • 15
2

That's because closing the transaction doesn't close the current session. That means that child is still part of the current session, and Hibernate will still consider it to be the same child, and will try to null out its id on the DB.

If you want to make the child object transient, you should use evict() on it.

Haroldo_OK
  • 6,612
  • 3
  • 43
  • 80
0

Another reason you can get this is because EntityManager.detach an object in the entity graph followed by EntityManager.flush (which may occur eventually) in which case since it's detached from the entity graph it will try to create a new one again, but it is in a weird state and if you modify it you would get the following error

Don't change the reference to a collection ...

Archimedes Trajano
  • 35,625
  • 19
  • 175
  • 265