I'm working a little further of the example on the reference Hibernate documentation (Event and Person classes).
Here are the mapping files :
Code:
<?xml version="1.0"?>
<!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">
<cache usage="read-write" />
<id name="id" column="EVENT_ID">
<generator class="increment" />
</id>
<property name="date" type="timestamp" column="EVENT_DATE" />
<property name="title" />
<set name="participants" table="PERSON_EVENT" inverse="true">
<key column="EVENT_ID" />
<many-to-many column="PERSON_ID" class="events.Person" />
</set>
</class>
</hibernate-mapping>
and
Code:
<?xml version="1.0"?>
<!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.Person" table="PERSON" lazy="false">
<cache usage="read-write" />
<id name="id" column="PERSON_ID">
<generator class="native" />
</id>
<property name="age" />
<property name="firstname" length="80" />
<property name="lastname" length="80" />
<set name="events" table="PERSON_EVENT" lazy="true">
<key column="PERSON_ID" />
<many-to-many column="EVENT_ID" class="events.Event" />
</set>
<set name="emailAddresses" table="PERSON_EMAIL_ADDR" lazy="true"
cascade="all">
<key column="PERSON_ID" />
<element type="string" column="EMAIL_ADDR" />
</set>
</class>
</hibernate-mapping>
Now, the issue : when I delete a Person having Events, Hibernate also deletes the rows in the PERSON_EVENT table (that's cool)
But when I delete and Event, I have a
Code:
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
How can I delete the Event ad its relations ?
Here is the code sample I use (the Event to delete comes from a previous transaction)
Code:
Event event = (Event) request.getSession().getAttribute("event");
Transaction tx = beginTransaction();
getSession().delete(event);
commitTransaction(tx);
Thanks