Hi,
I have been running into a problem with a table structure and trying to get it mapped properly to do cascading deletes. I have been reading over some web sites about how to deal with a many-to-many relationship and working with a cross-reference table. I am fairly comfortable on the concept and have my domain objects setup and working. My problem is that when I delete one side of the many-to-many I want the record in the cross-reference table to be deleted as well. The unique situation that I am dealing with is that the cross-reference table is actually a domain object for the application I am working on. For simplicity sake I am going to use the example data setup as described at
http://docs.jboss.org/hibernate/stable/ ... ollections . Where this application varies is that we have a date on the cross-referenced table. So using the example the PERSON_EVENT table would have a SIGN_UP_DATE attribute. To support this the mapping the Person class would look something like this:
Code:
<class name="Person" table="PERSON">
<id name="id" column="PERSON_ID">
<generator class="native"/>
</id>
<property name="age"/>
<property name="firstname"/>
<property name="lastname"/>
<set name="events" table="PERSON_EVENT" inverse="true" cascade="all">
<key column="PERSON_ID"/>
<one-to-many class="PersonEvent"/>
</set>
</class>
Then there is a new class to allow for the retrieval of the extra attribute on the PERSON_EVENT table.
Code:
<class name="PersonEvent" table="PERSON_EVENT">
<composite-id>
<key-many-to-one name="person" class="Person" column="PERSON_ID" />
<key-many-to-one name="event" class="Event" column="EVENT_ID" />
</composite-id>
<property name="signUpDate" column="SIGN_UP_DATE" />
</class>
The code I am using to add events is as follows:
Code:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Person aPerson = (Person) session.load(Person.class, personId);
Event anEvent = (Event) session.load(Event.class, eventId);
PersonEvent personEvent = new PersonEvent(aPerson, anEvent);
aPerson.getEvents().add(personEvent);
session.getTransaction().commit();
The problem comes when trying to delete:
Code:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Person aPerson = (Person) session.load(Person.class, personId);
session.delete(aPerson);
session.getTransaction().commit();
This deletes the person record from the PERSON table but does not end up deleting the PERSON_EVENT record. I have played around with changing the inverse attribute as well as the cascade attribute on the <set> element without any success.
Anyone able to offer any insight?
Thanks