Hey folks - I don't know if I the following is a feature of Hibernate (2.1.8) or a problem with my code/configuration.
The core of the issue are three objects - Camp, CampWeek, ChildrenInCampWeek (yes, a little Camp Reservation system for the kids). A Camp has many CampWeeks and each CampWeek can have many children in it.
When I delete the Camp, I am expecting that all the CampWeeks for that Camp should be deleted and that any ChildInCampWeeks for those CampWeeks should also be deleted.
When I have one child in a number of CampWeeks for Camp1, everything works as outlined above. However, when I have a second child in those same CampWeeks, then I get errors such as:
DELETE statement conflicted with COLUMN REFERENCE constraint 'FK_ChildInCampWeek_CampWeek'. The conflict occurred in database 'Reservations', table 'ChildInCampWeek', column 'CampWeekId'.
Without getting too lengthy here, the Camp.hbm contains:
Code:
<set cascade="all-delete-orphan" inverse="true" name="CampWeekSet" sort="camp.CampWeek$CampWeekComparator">
<key column="CampId" />
<one-to-many class="CampWeek" />
</set>
CampWeek.hbm has:
Code:
<set
cascade="all-delete-orphan"
name="ChildInCampWeekSet"
table="ChildInCampWeek"
sort="camp.ChildInCampWeek$ChildInCampWeekComparator"
inverse="true"
>
<key column="CampWeekId" />
<one-to-many class="ChildInCampWeek" />
</set>
ChildInCampWeek.hbm has:
Code:
<many-to-one
class="Child"
name="Child"
not-null="true"
>
<column name="ChildId" />
</many-to-one>
<many-to-one
class="CampWeek"
name="CampWeek"
not-null="true"
>
<column name="CampWeekId" />
</many-to-one>
my code is simply:
Code:
public static void deleteCamp(Camp camp) throws HibernateException {
Session session = getSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.delete(camp);
tx.commit();
} catch (HibernateException e) {
System.out.println("Rolling back");
if (tx != null) {
tx.rollback();
}
e.printStackTrace();
} finally {
session.close();
}
}
Am I doing something wrong here? Thanks so much!!