我有兩個類。一個稱爲Employee
,另一個稱爲EmployeeDetails
,與其父類'Employee'
有零或一個關係。換句話說,有些時候我們需要將額外的數據存儲到這個'EmployeeDetails'
類中,但這不一定是標準。數據庫結構非常簡單,'EmployeeDetails'與其父級共享相同的ID。nHibernate,一對零或一個關係woes
我得到的問題是從'Employee'
類中刪除'EmployeeDetails'
類,我會想象將對象設置爲null會完成技巧和刷新會話,但DB中的記錄不會被刪除。
我的映射是...
<?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>
...併爲EmployeeDetails類...
<?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>
插入和更新工作正常,但我一直在努力尋找開關,使這項工作刪除。
任何幫助或建議感激地收到...
不用擔心,幸好這個東西可以被業務層處理,所以雖然nHibernate的實現並不是很好,至少它的封裝了。乾杯 – SeanCocteau 2011-05-09 09:08:07