Reputation: 1043
I have following hibernate mapping and java classes structure:
<class name="A" abstract="true" table="tableA" lazy="false">
<id name="id" type="long">
<generator class="native" />
</id>
<discriminator column="type" type="string" />
<!--common properties-->
<subclass name="B" abstract="true" >
<join table="tableB">
<key column="aId"/>
<!--subclass common properties-->
</join>
<subclass name="B1" discriminator-value="B1TYPE" >
<!--subclass properties-->
</subclass>
<subclass name="B2" discriminator-value="B2TYPE" >
<!--subclass properties-->
</subclass>
</subclass>
</class>
<class name="C" table="tableC">
<id name="id" type="long">
<generator class="native" />
</id>
<set name="items" cascade="all,delete-orphan">
<key column="id" not-null="true" />
<one-to-many class="A" />
</set>
</class>
And java part:
abstract class A
{
}
abstract class B extends A
{
}
class B1 extends B
{
}
class B2 extends B
{
}
class C
{
Set<A> items;
}
The problem is that there are cases when row deleted from tableB
, but is not deleted from tableA
. What possible reasons could be for that ?
edit:
When item is removed from class C
, and C
is stored from its repository class:
C.items.remove(B1);
CRepository.store(C);
Upvotes: 0
Views: 909
Reputation: 516
The reason is that you have to explicitly define the action on-delete
of the tableB
foreign key declaration pointing to aId
.
By default, this is noaction
: this means that when you delete any B entity, the corresponding A related entity is not deleted.
Find more informations on Hibernate documentation, 5.1.11.3. Key
Give a try to this mapping:
<subclass name="B" abstract="true" >
<join table="tableB">
<key column="aId" on-delete="cascade"/>
<!--subclass common properties-->
</join>
<subclass name="B1" discriminator-value="B1TYPE" >
<!--subclass properties-->
</subclass>
<subclass name="B2" discriminator-value="B2TYPE" >
<!--subclass properties-->
</subclass>
</subclass>
Upvotes: 2