Hi all,
I recently tried to migrate databases and update Hibernate to a newer version, but this is giving me some trouble when it comes to cascaded deletes.
Our database contains some surveys. These surveys each have a number of mailings, and these mailings contain Informations, which are basically String containers for multiple translations of the same piece of text.
The relevant mappings are:
SurveyDefinition.hbm.xml:
Code:
...
<set name="mailings" inverse="true" cascade="evict, delete">
<key column="surveyDefinition" on-delete="cascade"/>
<one-to-many class="Mailing" />
</set>
...
Mailing.hbm.xml:
Code:
...
<many-to-one name="content" class="Information" not-null="true" unique="true" cascade="all" />
<many-to-one name="subject" class="Information" not-null="true" unique="true" cascade="all" />
...
Information.hbm.xml:
Code:
...
<id name="id" column="id">
<generator class="native" />
</id>
...
Now, when deleting a survey, it should also delete all its mailings and all Informations belonging to these mailings. This used to happen just fine when using MySQL 5 in combination with Hibernate 3.3.2.GA. However, when migrating to PostgreSQL and/or upgrading to Hibernate 3.5.1-Final (all three of these other combinations fail!), I get a foreign key constraint violation when deleting the surveyDefinition. When I delete a mailing manually everything works just fine.
The exact error:
Code:
ERROR - JDBCExceptionReporter - ERROR: update or delete on table "information" violates foreign key constraint "fk3471b287f8128fb" on table "mailings"
Detail: Key (id)=(26) is still referenced from table "mailings".
The details of this constraint according to pgAdmin:
Code:
-- Foreign Key: fk3471b287f8128fb
-- ALTER TABLE mailings DROP CONSTRAINT fk3471b287f8128fb;
ALTER TABLE mailings
ADD CONSTRAINT fk3471b287f8128fb FOREIGN KEY ("content")
REFERENCES information (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION;
The same problem occurs with other similar cascaded deletes. To me it seems that somehow Hibernate deletes things from the database in the wrong order, and not as a single transaction but in separate queries.
Can anyone shed any light on what I'm doing wrong? :) Thanks in advance!