Hi everyone,
I've been dealing with this issue for sometime now, and I can't get to solve the issue. Let me explain it briefly. I have three entity objects, called ClassA, ClassB, ClassC (I am using these names as examples only). All of them extends Parent. I decided to implement them following the table per subclass strategy, so ClassA, ClassB, and ClassC are described on the hbm as joined-subclasses. But also a ClassA has a relationship with ClassB, and ClassB with ClassC. A ClassA could contain a set of ClassB, ClassB must have a ClassA associated with it and could contain a set of ClassC elements, and so on with ClassC.
This is the corresponding mapping file (properties are ommitted)
Code:
<class name="Parent" table="Parent">
<id column="Parent_id" name="Parent">
<generator class="identity"/>
</id>
<property column..../>
<!--Class A subclass-->
<joined-subclass name="ClassA"
extends="Parent"
table="ClassA">
<key column="Parent_id" on-delete="cascade" not-null="true"/>
<property column..../>
<set cascade="delete" inverse="true" name="ClassBChilds">
<key column="classA_id" on-delete="cascade"/>
<one-to-many class="ClassB"/>
</set>
</joined-subclass>
<!--Class B subclass-->
<joined-subclass name="ClassB"
extends="Parent
table="ClassB">
<key column="Parent_id" not-null="true"/>
<property column..../>
<many-to-one class="ClassA"
column="classA_id" name="ClassAParent" not-null="true"/>
<set cascade="all" inverse="true" name="ClassCChilds">
<key column="classB_id" on-delete="cascade"/>
<one-to-many class="ClassC"/>
</set>
</joined-subclass>
<!--ClassC subclass-->
<joined-subclass name="ClassC"
extends="Parent"
table="ClassC">
<key column="Parent_id" not-null="true"/>
<property column..../>
<many-to-one class="ClassB"
column="classB_id" name="ClassBParent" not-null="true"/>
</joined-subclass>
</class>
So the issue I am facing is related with the inner relation between ClassA,B, and C. If I try to delete all instances of ClassA, all associated ClassB elements must be deleted, and also ClassC elements associated with those ClassB deleted objects. And if I delete all ClassB elements, all ClassC elements must be deleted. But the problem is that each time I delete all elements of ClassA, the parent table still have references to a ClassB and ClassC instance which does not exist on its table corresponding tables, and the same happen on the case of ClassB deletion process. I am doing something wrong here with the mapping but I am stuck right now with it for some time and could not see it. Help will be appreciated