Java 不要更改对带有 cascade="all-delete-orphan" 的集合的引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18910641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Don't change the reference to a collection with cascade="all-delete-orphan"
提问by user2599052
I am getting an error:
我收到一个错误:
Don't change the reference to a collection with cascade="all-delete-orphan"
不要更改对带有 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
'.
父子关系通过one-to-many
级联 = ' 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 ?
知道为什么抛出这个异常吗?即使实体处于分离状态,为什么在主键上设置 null 会导致此异常?
回答by Debojit Saikia
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.
如果您使用 加载具有集合的实体cascade=all-delete-orphan
,然后删除对该集合的引用,则通常会发生此异常。
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.
不要替换这个集合。始终使用collection.clear()
删除所有关联的子条目,以便孤立删除算法可以检测到更改。如果你想删除任何特定的孩子,你只需要从集合中删除它。一旦从集合中移除,它将被视为孤儿并将被删除。
回答by Haroldo_OK
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.
那是因为关闭事务不会关闭当前会话。这意味着它child
仍然是当前会话的一部分,并且 Hibernate 仍然会认为它是同一个孩子,并会尝试将其id
在 DB 上清空。
If you want to make the child
object transient, you should use evict()
on it.
如果你想让child
对象瞬态,你应该使用evict()
它。
回答by xiayouxue
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.
对于异常 不要更改对带有级联 =“all-delete-orphan”的集合的引用。您使用child.setID(null);
更改 ID 导致异常。
hibernate use PersistentSet
to execute the exception check,so you can do this:
hibernate 用于PersistentSet
执行异常检查,因此您可以执行以下操作:
child.setID(null);
parent.child=new HashSet(parent.child)
use HashSet to replace PersistentSet
使用 HashSet 替换 PersistentSet