简体   繁体   中英

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

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?

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.

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

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.

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM