如果多个实体引用同一源,则无法在 Nhibernate 中级联删除或更新
我有这样的 ChargeOperations(左表)和 Distributions(右表)映射:
在 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”等等。
我使用级联来创建实体,它效果很好,但要删除我必须
清理右表中的所有引用,然后分别保存所有类型的实体。如果没有,我会收到错误。
但我想使用级联保存。我怎样才能意识到它?
保存后可能的变体:
- 仅从右表中删除一条记录。左边的所有记录 表存在,
- 仅从右表中删除了两条记录。全部 左表中的记录也被删除
I have such mapping of ChargeOperations(left table) and Distributions(right table):
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:
- deleted just one record from right table. all records in left
table are exist - deleted just two records from right table. all
records in left table are also deleted
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您不想删除任何引用的实体,请将 Cascade 更改为
Cascade.SaveUpdate()
。,每当您删除一个子项 (
Distributions
) 时,您不应该尝试删除父项 (ChargeOperations
)在我看来
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