Hello
I have three tables family_members, addresses et family_member_address, that I want to map with three classes FamilyMember, FamilyMemberAddress and Address.
When a family member is deleted, the related associations *and* addresses must be deleted too. I have no problem to automatically suppress associations from database with Hibernate, but I have failed to do the same thing for addresses.
For now, I have written this in FamilyMember.hbm.xml :
Code:
[...]
<set name="addresses" inverse="true" cascade="all-delete-orphan" lazy="true">
<key column="id_member"/>
<one-to-many class="FamilyMemberAddress"/>
</set>
[...]
and in FamilyMemberAddress.hbm.xml :
Code:
<hibernate-mapping package="data">
<class
name="FamilyMemberAddress"
table="family_member_address"
>
<composite-id name="id" class="FamilyMemberAddressPK">
<key-many-to-one
name="address"
class="data.Address"
column="id_address"
/>
<key-many-to-one
name="familyMember"
class="FamilyMember"
column="id_member"
/>
</composite-id>
<many-to-one
name="addressType"
column="id_address_type"
class="data.AddressType"
not-null="true"
>
</many-to-one>
</class>
</hibernate-mapping>
It seems we can't write something like cascade="all-delete-orphan" for the elements of the composite key of an association.
What is the best solution to automatically delete theses data ?
Maybe with <many-to-many> tags, but the documentation isn't clear about that when you have an association class. In the examples, the association table is not mapped with a class (or I don't understand how).
Do you have a working example ?
Thank you for your help
Syll