gpt4 book ai didi

java - 不要更改对具有级联 ="all-delete-orphan"的集合的引用

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:43:32 28 4
gpt4 key购买 nike

我收到一个错误:

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

尝试以下操作时:

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();

父子关系是一对多,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>

知道为什么会抛出这个异常吗?为什么在主键上设置 null 会导致此异常,即使实体处于分离状态?

最佳答案

如果您使用 cascade=all-delete-orphan 加载具有集合的实体,然后删除对该集合的引用,则通常会发生此异常。

不要替换这个集合。始终使用 collection.clear() 删除所有关联的子条目,以便孤立删除算法可以检测到更改。如果你想删除任何特定的 child ,你只需要从集合中删除它。一旦从集合中移除,它将被视为孤儿并被删除。

关于java - 不要更改对具有级联 ="all-delete-orphan"的集合的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18910641/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com