不要更改对cascade =“all-delete-orphan”集合的引用。 [英] Don't change the reference to a collection with cascade="all-delete-orphan"

查看:1344
本文介绍了不要更改对cascade =“all-delete-orphan”集合的引用。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个错误:


请勿更改cascade =all-delete-orphan集合的引用


在尝试以下操作时:

  beginTx(); 
父母=新父母();
Child child = new Child();
parent.addChild(child);
getSession()。save(parent);
commitTx();
closeSession();

beginTx();
// id是主键
child.setID(null);
getSession()。update(child);
commitTx();
closeSession();

父母与子女通过一对多

  class Parent {
Set child;



< key column =FK>< / key>
<一对多班=小孩/>
< / set>

任何想法为什么抛出这个异常?
为什么在主键上设置null会导致这个异常,即使实体处于分离状态?

解决方案

异常通常发生在如果您加载一个实体具有集合 cascade = all-delete-orphan 的集合中,然后删除集合的引用。



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


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.

这篇关于不要更改对cascade =“all-delete-orphan”集合的引用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆