gpt4 book ai didi

c# - Nhibernate - 与 Cascade all-delete-orphan 一对一映射,不删除孤儿

转载 作者:太空狗 更新时间:2023-10-29 23:10:43 24 4
gpt4 key购买 nike

我有一个“面试”实体,它与“表单提交”实体具有一对一的映射关系,可以说面试实体是主导方,映射是:

<class name="Interview">
<id name="Id" column="Id" type="Int64">
<generator class="identity" />
</id>

// other props (snip)....

<one-to-one name="Submission" class="FormSubmission"
cascade="all-delete-orphan" />
</class>

<class name="FormSubmission">
<id name="Id" column="Id" type="Int64">
<generator class="foreign">
<param name="property">Interview</param>
</generator>
</id>

// other props (snip)....

<one-to-one name="Interview" class="Interview"
constrained="true" cascade="none" />
</class>

两个实体都是聚合的一部分,采访充当聚合根。我正在尝试通过 Interview 实体保存/更新/删除 FormSubmission,因此我将关联的 Interview 端映射为 cascade="all-delete-orphan"。例如,我可以像这样创建一个新的 FormSubmission:

myInterview.Submission = new FormSubmission(myInterview);
InterviewRepository.Save(myInterview);

...这很好用,FormSubmission 已保存。但是,我似乎无法删除我尝试这样做的 FormSubmission:

myInterview.Submission = null;
InterviewRepository.Save(myInterview);

...但这似乎并没有删除 FormSubmission。我已经尝试将 null 分配给关联的双方:

myInterview.Submission.Interview = null;
myInterview.Submission = null;
InterviewRepository.Save(myInterview);

我什至尝试在 FormSubmission 端设置 cascade="all-delete-orphan",但似乎没有任何效果。我错过了什么?

最佳答案

可能这不是您想要的答案。根据此问题,主键一对一关联不支持“全部删除孤立”级联:https://nhibernate.jira.com/browse/NH-1262 .即使外键一对一关联也很可能忽略“all-delete-orphan”级联:

<class name="Interview">
<id name="Id" column="Id" type="Int64">
<generator class="identity" />
</id>

<property name="Name" />

<many-to-one name="Submission" unique="true" cascade="all-delete-orphan" />
</class>

<class name="FormSubmission">
<id name="Id" column="Id" type="Int64">
<generator class="identity" />
</id>

<property name="Name" />

<one-to-one name="Interview" cascade="all-delete-orphan" property-ref="Submission" />
</class>

编辑:查普曼 suggests使用拦截器(事件监听器在 NH2.x 及更高版本中更受欢迎)来模拟这个听起来很有趣的功能,但我还不清楚如何实现这样的拦截器/事件监听器。

关于c# - Nhibernate - 与 Cascade all-delete-orphan 一对一映射,不删除孤儿,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5704984/

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