I have two classes. One is called Employee
and the other EmployeeDetails
that has a zero or one relationship with its parent 'Employee'
class. In other words, there are occasions where we need to store additional data into this 'EmployeeDetails'
class but this isn't necessarily the norm. The db structure is pretty simple with the 'EmployeeDetails' sharing the same ID as its parent.
The problem I've got is deleting the 'EmployeeDetails'
class from the 'Employee'
class, I would have imagined that setting the object to null would have done the trick and flushing sessions but the record in the DB is not removed.
My mappings are...
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="StudioBusinessLayer.Data.Structure.Employee, StudioBusinessLayer" table="tblEmployee" lazy="false">
<id name="ID" column="ID" type="int">
<generator class="native" />
</id>
<property name="Name" column="Name" not-null="true" type="string" length="100" />
<!-- etc -->
<one-to-one constrained="false" name="EmployeeDetails" class="StudioBusinessLayer.Data.Structure.EmployeeDetails, StudioBusinessLayer" cascade="all-delete-orphan" />
</class>
</hibernate-mapping>
...and for the EmployeeDetails class...
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="StudioBusinessLayer.Data.Structure.EmployeeDetails, StudioBusinessLayer" table="tblDetails" lazy="false">
<id name="ID" column="DetailsID" type="int">
<generator class="foreign">
<param name="property">Employee</param>
</generator>
</id>
<property name="Address" column="Address" not-null="false" type="string" length="1000" />
<property name="ContactEmail" column="ContactEmail" not-null="false" type="string" length="255" />
<property name="ContactName" column="ContactName" not-null="false" type="string" length="255" />
<property name="ContactTelephone" column="ContactTelephone" not-null="false" type="string" length="255" />
<property name="ZipCode" column="ZipCode" not-null="true" type="string" length="100" />
<many-to-one name="Employee" class="StudioBusinessLayer.Data.Structure.Employee, StudioBusinessLayer" column="DetailsID" insert="false" update="false"></many-to-one>
</class>
</hibernate-mapping>
Insertions and updates work fine, but I've been struggling to find the switch that makes this work for deletions.
Any help or suggestions are gratefully received...
Unfortunately all-delete-orphan is not supported for one-to-one relation in NHibernate yet. See this issue or this SO question for more details. It seems that there is no other way than deleting EmployeeDetails by your own or use an event listener that will emulate all-delete-orphan for one-to-one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With