Hibernate version: 3.3.1
Database version: MySQL 5.1
I having been working on this bug for over two weeks and I can't seem to figure it out. Hopefully my little simplified example will be enough to explain my problem. I have a many-to-many relationship between my containers table and my widgets table (table names have been changed for this example). In the code I want to delete a widget and have it removed from all of the containers it is associated with. When I write the code to delete the widget I get an exception saying I can't due to a constraint violation. Here are my hibernate XML files and the Java code I use to delete:
Container Table
Code:
<hibernate-mapping>
<class name="com.example.app.Container" table="containers" lazy="false">
<id name="containerId" column="container_id">
<generator class="native"/>
</id>
<property name="name" column="name"/>
<property name="displayName" column="display_name"/>
<list name="widgets" table="container_to_widgets" lazy="false" cascade="save-update">
<key column="container_id" />
<list-index column="list_index"/>
<many-to-many class="com.example.app.Widget" column="widget_id"/>
</list>
</class>
</hibernate-mapping>
Widget Table
Code:
<hibernate-mapping>
<class name="com.exmaple.app.Widget" table="widgets" lazy="false">
<id name="widgetId" column="widget_id">
<generator class="native"/>
</id>
<property name="name" column="name"/>
<property name="type" column="type"/>
</class>
</hibernate-mapping>
Delete Java Code:
Code:
mHibernateTemplate.delete(pWidget);
Here is the exception I get:
Code:
java.sql.BatchUpdateException: Cannot delete or update a parent row: a foreign key constraint fails (`exampledb`.`container_to_widgets`, CONSTRAINT `container_to_widgets_ibfk_2` FOREIGN KEY (`widget_id`) REFERENCES `widgets` (`widget_id`))
Am I doing something wrong? Thanks in advance.