Nhibernate-与Cascade All-delete-orphan进行一对一映射,不删除该孤儿 [英] Nhibernate - One-to-one mapping with Cascade all-delete-orphan, not deleting the orphan

查看:258
本文介绍了Nhibernate-与Cascade All-delete-orphan进行一对一映射,不删除该孤儿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个'Interview'实体,该实体与'FormSubmission'实体具有一对一映射,可以说Interview实体是主要方面,

I have an 'Interview' entity that has a one-to-one mapping with a 'FormSubmission' entity, the Interview entity is the dominant side so to speak, the mapping is:

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

Both entities are part of an Aggregate with the Interview acting as the Aggregate Root. I'm trying to Save/Update/Delete the FormSubmission via the Interview entity, hence I have mapped the Interview end of the association as cascade="all-delete-orphan". For instance, I can create a new FormSubmission just fine like this:

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

...这很好用,保存了FormSubmission.但是,我似乎无法删除要这样做的FormSubmission:

...and this works just fine, the FormSubmission is saved. However, I can't seem to delete the FormSubmission which I'm trying to do like this:

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

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

...but this doesn't seem to delete the FormSubmission. I've tried assigning null to both sides of the association:

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

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

I've even tried setting cascade="all-delete-orphan" on the FormSubmission side, but nothing seems to work. What am I missing?

推荐答案

可能这不是您想要的答案.根据此问题,主键一对一关联不支持全部删除孤立"级联:

Probably this is not answer what you want. "All-delete-orphan" cascade is not supported for primary key one-to-one association according to this issue: https://nhibernate.jira.com/browse/NH-1262. Even foreign key one-to-one association most likely ignores "all-delete-orphan" cascade:

<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及更高版本中)来模仿此功能,这听起来很有趣,但我还不清楚如何实现此类拦截器/事件监听器.

jchapman suggests to use interceptor (event listener is more preferred in NH2.x and higher) to emulate this feature which sounds interesting but I have no clear idea how to implement such interceptor/event listener yet.

这篇关于Nhibernate-与Cascade All-delete-orphan进行一对一映射,不删除该孤儿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆