Nhibernate - One-to-one mapping with Cascade all-delete-orphan, not deleting the orphan
Asked Answered
S

1

5

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>

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

...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);

...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);

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

Solipsism answered 18/4, 2011 at 15:11 Comment(0)
K
7

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>

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

Kief answered 18/4, 2011 at 17:16 Comment(4)
Aww man, that sucks! I wonder what their justification for not supporting it is? Judging by the tone in Fabio Maulo's response: 'one-to-one with "all-delete-orphan"?' I guess he thinks this sort of mapping is a bad idea, but I wonder why?Solipsism
Difficult to say, it is the question directly for Fabio:). Anyway it seems that it is already implemented in Hibernate 3.5 (one year old) according to this issue opensource.atlassian.com/projects/hibernate/browse/HHH-2608. No idea how Hibernate and NHibernate versions are related...Kief
The NH-1262 issue is now flagged as solved in upcoming 4.1 version.Mordancy
The NH-1262 issue is indeed fixed and working in NHibernate 4.1. This can help you to use it with fluent nhibernate.Simitar

© 2022 - 2024 — McMap. All rights reserved.