如果多个实体引用同一源,则无法在 Nhibernate 中级联删除或更新

发布于 2025-01-08 05:26:35 字数 944 浏览 4 评论 0原文

我有这样的 ChargeOperations(左表)和 Distributions(右表)映射:

Mapping image 在 ChargeOperations 的代码映射中,它看起来像:

 HasMany(x => x.Distributions).Table("ShadowDistributions").KeyColumn("SourceId").Cascade.All().Inverse();

ShadowDistributions - 是一个正确的表。 x.Distributions 只是一个发行版列表(右表)。 X - 是 ChargeOperation(左表)

分布映射(右表)

References(x => x.Source).Nullable().Column("SourceId").Not.LazyLoad();
References(x => x.Dest).Nullable().Column("Dest").LazyLoad().Fetch.Join().Cascade.All();

因此,我想从分布(右表)中仅删除一行。 应用程序会抛出不同的映射异常,例如“由于资源失败,事务无法提交:已删除的对象将通过级联重新保存(从关联中删除已删除的对象)[ChargeOperation#58]”或“意外的行计数:0;预期:1”等等。


我使用级联来创建实体,它效果很好,但要删除我必须
清理右表中的所有引用,然后分别保存所有类型的实体。如果没有,我会收到错误。

但我想使用级联保存。我怎样才能意识到它?

保存后可能的变体:

  1. 仅从右表中删除一条记录。左边的所有记录 表存在,
  2. 仅从右表中删除了两条记录。全部 左表中的记录也被删除

I have such mapping of ChargeOperations(left table) and Distributions(right table):

Mapping image
In code mapping of ChargeOperations looks like:

 HasMany(x => x.Distributions).Table("ShadowDistributions").KeyColumn("SourceId").Cascade.All().Inverse();

ShadowDistributions - is a right table. x.Distributions is a just a list of Distributions(right table). X - is ChargeOperation(left table)

Mapping of Distributions (right table)

References(x => x.Source).Nullable().Column("SourceId").Not.LazyLoad();
References(x => x.Dest).Nullable().Column("Dest").LazyLoad().Fetch.Join().Cascade.All();

So, I want to delete just one row from Distributions (right table).
And the applications throws different mapping exceptions like "Transaction could not commit because of a failed resource : deleted object would be re-saved by cascade (remove deleted object from associations)[ChargeOperation#58]"" or "Unexpected row count: 0; expected: 1" and so on.


I use cascade for creating entities and it works great, but for delete I had to
clean all references in the right table, and after that save all types of entities separately. If not, I'll get errors.

But I'd like to use cascade saving. How can I realize it?

Possible variants after save:

  1. deleted just one record from right table. all records in left
    table are exist
  2. deleted just two records from right table. all
    records in left table are also deleted

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

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

发布评论

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

评论(1

心不设防 2025-01-15 05:26:35

如果您不想删除任何引用的实体,请将 Cascade 更改为 Cascade.SaveUpdate()

,每当您删除一个子项 (Distributions) 时,您不应该尝试删除父项 (ChargeOperations)

在我看来

public SomeMethod()
{    
    using(ISession session = ... //Get my session from somewhere)
    {
        Distribution childToDelete = ... //Get the distribution to delete
        ChargeOperation parent = ... //Get the parent of the distribution we are deleting

        parent.Distributions.Remove(childToDelete);

        //Since the parent is in session just flush the session to apply changes
        session.Flush();
    }
}

Change your Cascade to Cascade.SaveUpdate() if you don't wish to delete any of the referenced entities.

In my opinion you shouldn't be trying to delete a parent (ChargeOperations) whenever you delete one of the children (Distributions)

Example

public SomeMethod()
{    
    using(ISession session = ... //Get my session from somewhere)
    {
        Distribution childToDelete = ... //Get the distribution to delete
        ChargeOperation parent = ... //Get the parent of the distribution we are deleting

        parent.Distributions.Remove(childToDelete);

        //Since the parent is in session just flush the session to apply changes
        session.Flush();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文