Hibernate version: 3.3.0 CR1
Mapping documents:
Code:
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="events.Event" table="EVENTS">
<id name="id" column="[event_id]"><generator class="native"/></id>
<property name="name" column="[name]"/>
<set name="people" table="PERSON_EVENTS" inverse="true">
<key column="[event_id]"/>
<many-to-many column="[person_id]" class="events.Person"/>
</set>
</class>
<class name="events.Person" table="PERSON">
<id name="id" column="[person_id]"><generator class="native"/></id>
<property name="firstname" column="[fname]"/>
<property name="lastname" column="[lname]"/>
<set name="events" table="PERSON_EVENTS">
<key column="[person_id]"/>
<many-to-many column="[event_id]" class="events.Event"/>
</set>
</class>
</hibernate-mapping>
Relevant code, called between beginTransaction() and commit():Code:
public void deletePerson (long person_id) {
Session s = getCurrentSession();
Person p = (Person)s.load(Person.class, new Long(person_id));
s.delete(p);
}
public void deleteEvent (long event_id) {
Session s = getCurrentSession();
Event p = (Event)s.load(Event.class, new Long(event_id));
s.delete(p);
}
Name and version of the database you are using:MS SQL Server 2005
The generated SQL (show_sql=true):When deleting people:
Code:
Hibernate: select person0_."person_id" as person1_3_0_, person0_."fname" as fname2_3_0_, person0_."lname" as lname3_3_0_ from PERSON person0_ where person0_."person_id"=?
Hibernate: delete from PERSON_EVENTS where "person_id"=?
Hibernate: delete from PERSON where "person_id"=?
When deleting events:
Code:
Hibernate: select event0_."event_id" as event1_1_0_, event0_."name" as name2_1_0_ from EVENTS event0_ where event0_."event_id"=?
Hibernate: delete from EVENTS where "event_id"=?
Problem
The documentation and Google are failing me here, I've spent a significant amount of time today trying to get this to work. The above example is sort of like the hibernate tutorial. There is a many-to-many relationship between Persons and Events.
I want to be able to delete a Person or delete an Event and have it delete the links in the PERSON_EVENTS table, but not actually delete the linked objects themselves.
Everything works fine when deleting a Person. As shown in the queries above, hibernate explicitly deletes the links from the PERSON_EVENTS table first before deleting the PERSON -- so there are no key violations when deleting people.
Deleting from the inverse side of the relationship (the Events) does not work. Hibernate does not delete the link first. I want it to do that. How do I set that up?
The reason I put "cascade" in quotes in the title is because it doesn't appear to be a database-level cascade. Hibernate is explicitly deleting things first.
I have made many attempts, all failed. Using "cascade=delete" is not appropriate as I don't want the actual linked objects to be deleted. I have also visited many Google results for this, and none of the solutions seem to work (ranging from placing "cascade=save-update" in random locations to explicitly attempting to delete the links first at the Java level).
How can I make hibernate do what I want? It works fine from the Person side, but doesn't work from the Event side at all.
Thanks,
Jason