5

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

<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>

这两个实体都是聚合的一部分,采访充当聚合根。我正在尝试通过采访实体保存/更新/删除表单提交,因此我已将关联的采访结束映射为 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" ,但似乎没有任何效果。我错过了什么?

4

1 回答 1

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>

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

于 2011-04-18T17:16:15.577 回答