Cascade 中的错误:级联将重新保存已删除的对象

发布于 2024-12-12 13:21:00 字数 875 浏览 3 评论 0原文

我有一个由 NHibernate 实现并使用延迟加载的项目。 我在这个项目中有两个课程:个人和家庭。 这两者之间的关系是聚合,意味着一个人有一个人列表。 映射是:

  <class name="Person" table="Person_Person" >

    <id name="Id" type="Int64" unsaved-value="0">
      <generator class="native" />
    </id>

    <bag name="Families" inverse="true" table="Person_Family" cascade="all-delete-orphan" >
      <key column="Person_id_fk"/>
      <one-to-many class="Domain.Entities.Family,Domain.Entities"/>
    </bag>

  </class>

在这个项目中,我通过 ID 获取一个人,然后删除一个家庭成员。

Person person = SessionInstance.Get<Person>(id);
foreach (Family fam in person.Families)
    if (fam.Name == "Jaun")
        SessionInstance.Delete(fam);

该系列未删除,因为此消息引发异常: 已删除的对象将通过级联重新保存(从关联中删除已删除的对象)[Domain.Entities.Family#167]

我如何删除一个人的家庭?

I have a project by NHibernate implementation and using Lazy Loading.
I have two class in this project : Person and Family.
Relation between Those two is aggregation, is mean a Person has a list of Person.
Maping is :

  <class name="Person" table="Person_Person" >

    <id name="Id" type="Int64" unsaved-value="0">
      <generator class="native" />
    </id>

    <bag name="Families" inverse="true" table="Person_Family" cascade="all-delete-orphan" >
      <key column="Person_id_fk"/>
      <one-to-many class="Domain.Entities.Family,Domain.Entities"/>
    </bag>

  </class>

In this project, I Get a person by ID then remove a family of families person.

Person person = SessionInstance.Get<Person>(id);
foreach (Family fam in person.Families)
    if (fam.Name == "Jaun")
        SessionInstance.Delete(fam);

The family not deleted, Because throw a exception by this message :
deleted object would be re-saved by cascade (remove deleted object from associations)[Domain.Entities.Family#167]

How can i delete a family of person?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

默嘫て 2024-12-19 13:21:00

基本上 NHibernate 抱怨的是,你明确地告诉它删除 Family 的记录,然后当你重新保存 Person 时,Family 会被放回原处,因为 Person 仍然在其家庭名单。

相反,NHibernate 告诉您以 NHibernate 允许您使用的面向对象的方式处理这种关系。只需从 Person.Families 列表中删除对“Jaun”家族的引用,然后保留该 Person 即可。当您执行此操作时,NHibernate 将删除该 Family 和该 Person 之间的关系。如果该 Family 现在不再被其他任何内容引用,由于您已将 Cascade 属性设置为“all-delete-orphan”,“Jaun”Family 记录将从数据库中完全删除。

Basically what NHibernate is complaining about is that you are explicitly telling it to delete a record for the Family, then when you re-save the Person the Family will be put right back in place, because the Person still has a reference to it in its list of Families.

Instead, NHibernate is telling you to deal with this relationship in the object-oriented manner that NHibernate allows you to use. Simply remove the reference to the "Jaun" family from the Person.Families list, then persist the Person. When you do this, NHibernate will remove the relationship between that Family and that Person. If the Family is now no longer referenced by anything else, since you have set the Cascade property to "all-delete-orphan", the "Jaun" Family record will be deleted completely from the DB.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文