Im having a problem deleting all instances of a class that has multiple one-to-one bidirectional associations to the same class. Im using nhibernate-0.99.2.0.
Class A is mapped as:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"
namespace="Pncs.Lsm.DataModel" assembly="Pncs.Lsm.DataModel">
<class name="A" table="LSM_A">
<id name="Id" column="Id" type="Int32" unsaved-value="0">
<generator class="identity"/>
</id>
<one-to-one name="B_1" class="B" cascade="all" />
<one-to-one name="B_2" class="B" cascade="all" />
</class>
</hibernate-mapping>
Class B is mapped as:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"
namespace="Pncs.Lsm.DataModel" assembly="Pncs.Lsm.DataModel">
<class name="B" table="LSM_B">
<id name="Id" column="Id" type="Int32" unsaved-value="0">
<generator class="identity"/>
</id>
<many-to-one name="A" class="A" column="A" unique="false"/>
</class>
</hibernate-mapping>
The following code inserts correcly into the database:
Code:
A a1 = new A();
a1.B_1 = new B();
a1.B_1.A = a1;
a1.B_2 = new B();
a1.B_2.A = a1;
this.ds.Set(a1);
A a2 = new A();
a2.B_1 = new B();
a2.B_1.A = a2;
a2.B_2 = new B();
a2.B_2.A = a2;
this.ds.Set(a2);
Trying to then delete all these objects fails as follows:
Code:
this.ds.Clear(typeof(A));
{"DELETE statement conflicted with COLUMN REFERENCE constraint 'FKCFAF6AA2B5E4'. The conflict occurred in database 'XXX', table 'LSM_B', column 'A'.\r\nThe statement has been terminated." }
The SQL that hibernate is producing is as follows:
Code:
UPDATE LSM_B SET A = null WHERE Id = 2
DELETE FROM LSM_B WHERE Id = 1
DELETE FROM LSM_A WHERE Id = 1
DELETE FROM LSM_B WHERE Id = 2
DELETE FROM LSM_A WHERE Id = 2
The error makes sence and hibernate should be issuing another UPDATE LSM_B SET A = null WHERE Id = 1.
Should I be mapping these relationships in another way or is this a bug?
Thanks