Nhibernate - 使用Cascade all-delete-orphan进行一对一映射,而不是删除孤儿

Sun*_*oot 5 c# nhibernate domain-driven-design ddd-repositories nhibernate-mapping

我有一个'采访'实体,它与'FormSubmission'实体有一对一的映射,采访实体是主导的一面,可以这么说,映射是:

<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>
Run Code Online (Sandbox Code Playgroud)

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

myInterview.Submission = new FormSubmission(myInterview);
InterviewRepository.Save(myInterview);
Run Code Online (Sandbox Code Playgroud)

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

myInterview.Submission = null;
InterviewRepository.Save(myInterview);
Run Code Online (Sandbox Code Playgroud)

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

myInterview.Submission.Interview = null;
myInterview.Submission = null;
InterviewRepository.Save(myInterview);
Run Code Online (Sandbox Code Playgroud)

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

Jak*_*art 6

可能这不是你想要的答案.根据此问题,主键一对一关联不支持"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>
Run Code Online (Sandbox Code Playgroud)

编辑:jchapman 建议使用拦截器(事件监听器在NH2.x和更高版本中更受欢迎)来模拟这个听起来很有趣的功能,但我还不知道如何实现这样的拦截器/事件监听器.