My problem is a rather simple one:
I have two classes (A, C) that are linked by an association class B:
Code:
A <---- B ----> C
A 1:n B n:1 C
I do:
A --- B --- C
A --- B
and expected that B is deleted as well.
I want the following:
If A or C ist deleted, then B should be deleted as well. No matter if B is still linked to another A or C. This is just a normal cascading delete.
I mapped the sets in A and C with cascase="all-delete-orphan". However it seems that B is not deleted when it's still linked to another object.
So I tried cascade="all", but that didn't change a thing.
Hibernate version: 2.1.4
Mapping documents: Object A:
Code:
<hibernate-mapping>
<class
name="com.bawag.fi.model.hibernate.CompanyRepresentativeHPB"
table="company_representative"
dynamic-update="false"
dynamic-insert="false"
discriminator-value="COMPREPR"
>
...
<set
name="invitations"
lazy="true"
inverse="true"
cascade="all-delete-orphan"
sort="unsorted"
>
<key
column="fk_company_representative"
>
</key>
<one-to-many
class="com.bawag.fi.model.hibernate.EventInvitationHPB"
/>
</set>
</hibernate-mapping>
Object C:
Code:
<hibernate-mapping>
<class
name="com.bawag.fi.model.hibernate.EventHPB"
table="event"
dynamic-update="false"
dynamic-insert="false"
discriminator-value="EVENT"
>
...
<set
name="invitations"
lazy="true"
inverse="true"
cascade="all-delete-orphan"
sort="unsorted"
>
<key
column="fk_event"
>
</key>
<one-to-many
class="com.bawag.fi.model.hibernate.EventInvitationHPB"
/>
</set>
Object B (association object):
Code:
<hibernate-mapping>
<class
name="com.bawag.fi.model.hibernate.EventInvitationHPB"
table="event_invitation"
dynamic-update="false"
dynamic-insert="false"
discriminator-value="EVENTINV"
>
...
<many-to-one
name="companyRepresentative"
class="com.bawag.fi.model.hibernate.CompanyRepresentativeHPB"
cascade="all"
outer-join="auto"
update="true"
insert="true"
access="property"
foreign-key="fk_CR_EI_company_representati"
column="fk_company_representative"
/>
<many-to-one
name="event"
class="com.bawag.fi.model.hibernate.EventHPB"
cascade="all"
outer-join="auto"
update="true"
insert="true"
access="property"
foreign-key="fk_E_EI_event"
column="fk_event"
/>
Code between sessionFactory.openSession() and session.close():Code:
tx = session.beginTransaction();
// delete the object
session.delete(aCompanyRepresentative);
tx.commit();
Full stack trace of any exception that occurs:[code]
Hibernate: select fromrelati0_.id as id__, fromrelati0_.fk_from_company as fk_from_3___, fromrelati0_.id as id0_, fromrelati0_.SUBTYPE as SUBTYPE0_, fromrelati0_.fk_from_company as fk_from_3_0_, fromrelati0_.fk_to_company as fk_to_co4_0_, fromrelati0_.ownershipRatio as ownershi5_0_ from fi.company_relationship fromrelati0_ where fromrelati0_.fk_from_company=?
11:18:38,935 DEBUG LongType:46 - binding '53' to parameter: 1
Hibernate: select torelation0_.id as id__, torelation0_.fk_to_company as fk_to_co4___, torelation0_.id as id0_, torelation0_.SUBTYPE as SUBTYPE0_, torelation0_.fk_from_company as fk_from_3_0_, torelation0_.fk_to_company as fk_to_co4_0_, torelation0_.ownershipRatio as ownershi5_0_ from fi.company_relationship torelation0_ where torelation0_.fk_to_company=?
11:18:38,951 DEBUG LongType:46 - binding '53' to parameter: 1
Hibernate: select visits0_.id as id__, visits0_.fk_company as fk_company__, visits0_.id as id0_, visits0_.location as location0_, visits0_.timestamp as timestamp0_, visits0_.lunch as lunch0_, visits0_.employeeName1 as employee5_0_, visits0_.employeeName2 as employee6_0_, visits0_.employeeName3 as employee7_0_, visits0_.employeeName4 as employee8_0_, visits0_.employeeName5 as employee9_0_, visits0_.employeeName6 as employe10_0_, visits0_.employeeName7 as employe11_0_, visits0_.employeeName8 as employe12_0_, visits0_.employeeName9 as employe13_0_, visits0_.employeeName10 as employe14_0_, visits0_.companyRepresentativeName1 as company15_0_, visits0_.companyRepresentativeName2 as company16_0_, visits0_.companyRepresentativeName3 as company17_0_, visits0_.companyRepresentativeName4 as company18_0_, visits0_.companyRepresentativeName5 as company19_0_, visits0_.companyRepresentativeName6 as company20_0_, visits0_.companyRepresentativeName7 as company21_0_, visits0_.companyRepresentativeName8 as company22_0_, visits0_.companyRepresentativeName9 as company23_0_, visits0_.companyRepresentativeName10 as company24_0_, visits0_.fk_company as fk_company0_ from fi.visit visits0_ where visits0_.fk_company=?
11:18:38,967 DEBUG LongType:46 - binding '53' to parameter: 1
Hibernate: select invitation0_.id as id__, invitation0_.fk_company_representative as fk_compa2___, invitation0_.id as id0_, invitation0_.fk_company_representative as fk_compa2_0_, invitation0_.fk_event as fk_event0_ from fi.event_invitation invitation0_ where invitation0_.fk_company_representative=?
11:18:38,982 DEBUG LongType:46 - binding '103' to parameter: 1
11:18:38,998 DEBUG LongType:68 - returning '133' as column: id0_
11:18:38,998 DEBUG LongType:68 - returning '103' as column: fk_compa2_0_
11:18:39,014 DEBUG LongType:68 - returning '125' as column: fk_event0_
11:18:39,014 DEBUG LongType:68 - returning '103' as column: fk_compa2___
11:18:39,014 DEBUG LongType:68 - returning '133' as column: id__
Hibernate: select eventhpb0_.id as id0_, eventhpb0_.name as name0_, eventhpb0_.beginDate as beginDate0_, eventhpb0_.endDate as endDate0_, eventhpb0_.remarks as remarks0_ from fi.event eventhpb0_ where eventhpb0_.id=?
11:18:39,029 DEBUG LongType:46 - binding '125' to parameter: 1
11:18:39,045 DEBUG StringType:68 - returning 'BAWAG Management Cercle' as column: name0_
11:18:39,060 DEBUG DateType:68 - returning '17 August 2004' as column: beginDate0_
11:18:39,076 DEBUG DateType:68 - returning '17 August 2004' as column: endDate0_
11:18:39,092 DEBUG StringType:68 - returning 'J