-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Many-to-many delete
PostPosted: Mon Oct 25, 2010 11:31 am 
Newbie

Joined: Mon Oct 25, 2010 10:56 am
Posts: 1
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.