问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/5704984/nhibernate-one-to-one-mapping-with-cascade-all-delete-orphan-not-deleting-the