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?

Sunday Ironfoot
  • 12,840
  • 15
  • 75
  • 91

1 Answers1

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.

Daniel Schilling
  • 4,829
  • 28
  • 60
Jakub Linhart
  • 4,062
  • 1
  • 26
  • 42
  • 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? – Sunday Ironfoot Apr 19 '11 at 08:01
  • 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 http://opensource.atlassian.com/projects/hibernate/browse/HHH-2608. No idea how Hibernate and NHibernate versions are related... – Jakub Linhart Apr 19 '11 at 19:45
  • 1
    The [NH-1262 issue](https://nhibernate.jira.com/browse/NH-1262) is now flagged as solved in upcoming 4.1 version. – Frédéric May 10 '16 at 08:02
  • The NH-1262 issue is indeed fixed and working in NHibernate 4.1. [This](http://stackoverflow.com/a/44026508/379279) can help you to use it with fluent nhibernate. – xhafan May 17 '17 at 13:59