Reputation: 1876
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...
Upvotes: 5
Views: 1550
Reputation: 4072
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.
Upvotes: 2
Reputation: 12680
The EmployeeDetails mapping doesn't look right. Seems like the many-to-one mapping to the Employee should also be a one-to-one but I don't see that the tblDetails has a column for the Employee.Id value. Without it you can't establish a bi-directional association for the delete logic in NHibernate to know which tblDetails row belongs to which Employee.
UPDATE:
I found a good sample for how to map one-to-one relationships but it is for the strictly one-to-one scenario. It does mention that the child table would need to be checked for the determining whether an association exists so it may be applicable here but might involve some manual checking in your code.
Upvotes: 0