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

寵の児 提交于 2019-12-04 03:37:41

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!