Hi,
I have the following associations between my objects:
A<>----B
<>----C
C------>B
So A contains B and C.
C references B.
Mapping for class A is
<class name="A" table="A">
...
<set name="b" inverse="true"
cascade="all-delete-orphan">
<key column="a_id" />
<one-to-many class="B" />
</set>
<set name="c" inverse="true"
cascade="all-delete-orphan">
<key column="a_id" />
<one-to-many class="C" />
</set>
</class>
So the lifetime of B and C depends on the lifetime of A.
The reference between B and C is bidirectional and looks as follows
<class name="B" table="B">
...
<many-to-one name="a" column="a_id"
not-null="true" />
<set name="cs" inverse="true">
<key column="b_Id" />
<one-to-many class="C" />
</set>
</class>
<class name="C" table="C">
...
<many-to-one name="a" column="a_id"
not-null="true" />
<many-to-one name="b"
column="b_Id" not-null="false"/>
</class>
I add some object instances to the model and everything works fine.
A a;
B b;
C c;
a.addToBs(b);
a.addToCs(c);
c.setB(b);
b.addToCs(c);
But when I delete an instance of B and a reference from an instance of C to B still exists, I get a
Integrity constraint violation FKD5AC618F4BC6603A table: C in statement [delete from B where Id=?]
Session s = HibernateUtil.getSessionFactory().getCurrentSession();
s.beginTransaction();
a.removeFromBs(b);
s.getTransaction().commit();
But what I expect is that the reference from C to B is automatically deleted (e.g. the foreign key is set to null), when B is deleted.
I already tried to add the cascade=delete to the <set name="cs" inverse="true">, but this resulted in a
"deleted object would be re-saved by cascade (remove deleted object from associations):" exception.
Is there anybody who can help me? Thanks in advance.
I am using Hibernate Version 3.2.6 and HSQLDB
|