Joined: Tue Apr 25, 2006 1:17 pm Posts: 19
|
Hibernate version: 3.2
Problem/Question: How can I issue bulk deletes so that all entries in the associate link table as well as the row in the target table is deleted?
I want to delete all entries in the EventPersonLink table that correspond to a particular Event as well as that specific Event.
My database mapping:
Person --- EventPersonLink --- Event
where EventPersonLink table contains an ID to Person as well as an ID into the Event table.
Mapping documents:
<hibernate-mapping>
<class name="Person" table="Person" dynamic-update="true">
<id name="id" type="int">
<column name="ID"/>
<generator class="identity"/>
</id>
<property name="name" type="string">
<column name="Name" length="128" not-null="true"/>
</property>
<set name="events" cascade="save-update" table="EventPersonLink">
<!--<cache usage="read-write"/>-->
<key>
<column name="PersonID" not-null="true">
<comment></comment>
</column>
</key>
<many-to-many entity-name="Event">
<column name="EventID" not-null="true">
<comment></comment>
</column>
</many-to-many>
</set>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="Event" table="Event" dynamic-update="true">
<id name="id" type="int">
<column name="ID"/>
<generator class="identity"/>
</id>
<property name="name" type="string">
<column name="Name" length="128" not-null="true"/>
</property>
<property name="active" type="byte">
<column name="ActiveEvent" not-null="true"/>
</property>
<set name="persons" inverse="true" cascade="all" table="EventPersonLink">
<key>
<column name="EventID" not-null="true"/>
</key>
<many-to-many entity-name="Person">
<column name="PersonID" not-null="true"/>
</many-to-many>
</set>
</class>
<hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
Event event = session.get(eventId, Event.class);
event.removeAllPersons();
session.delete(event);
code inside removeAllPersons:
for (Object pObj : this.persons)
{
Person person = (Person) pObj;
person.getEvents().remove(this);
}
this. persons.clear();
What I see:
The above style of delete invokes individual SQL delete statements for each person associated with the event and I would like some help on how to issue a batch delete so that only one SQL delete statement is issued for all rows in the EventPersonLink table.
|
|