Well, either we've got here the parent-child hierarchy, (x)or many-to-many relation.
I. In case of many-to-many, we are missing
- the pairing table
- the second, reverted view on this relationship.
The model we have is providing the up view: Managers
, and should also provide the down view: Subordinates
.
<bag name="Managers" table="ManagerSubOrdinates" >
<key column="ManagerId" />
<many-to-many class="Employee" column="SubordinateId"/>
</bag>
<bag name="Subordinates" table="ManagerSubOrdinates" inverse="true" >
<key column="SubordinateId" />
<many-to-many class="Employee" column="ManagerId"/>
</bag>
Now, we mapped both ends of the pairing table/relation ManagerSubOrdinates
. If we will delete the Employee, the related records will be removed as well - but just from the pairing table. Not the related Managers or Subordinates
Finally the Deletion.
Now, when we do have the mapping in place, the correct way how to delete is like this
// load the Employee with ID == 1 into the ISession
var employee = session.Get<Employee>(1); // id 1
// pass this instance into the Delete
session.Delete(employee);
This will instruct NHibernate to properly remove all records from pairing table (both mappings) and then delete the Employee record itself
(not sure here) II. If this is in fact, the parent-child relation, we have to correct the mapping this way:
<bag name="Subordinates">
<key column="ManagerId" />
<many-to-many class="Employee"/>
</bag>
<many-to-one class="Employee" Name="Manager" column="ManagerId" >
Here I am just guessing (more Subordinates, one Manager), but The essence is in the column name ManagerId
. In most scenarios, the key
column does not have value "Id" which usually referes to the table Primary
key, not the foreign key...