-->
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.  [ 2 posts ] 
Author Message
 Post subject: foreign key constraint fails: deleting Child collection
PostPosted: Thu Nov 02, 2006 7:11 pm 
Newbie

Joined: Thu Oct 19, 2006 7:53 pm
Posts: 10
foreign key constraint fails
deleting Child collection from Parent/Child relationship
This uses a bidirectional one to many association with cascades to model a parent / child relationship
http://www.hibernate.org/hib_docs/v3/re ... child.html

Hibernate version: 3.2

Mapping documents:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping
schema="hfydb"
default-cascade="save-update, delete"
auto-import="true"
package="domain"
default-lazy="false">

<class
name="domain.Property"
table="property"
>

<id
name="id"
type="java.lang.Integer"
column="property_id"
>
<generator class="increment" />
</id>

<many-to-one name="office" class="Office" column="office_id" cascade="none" />
<many-to-one name="style" class="Type" column="type_id" cascade="none" />
<many-to-one name="address" class="Address" column="address_id" cascade="all" />

<!-- http://www.hibernate.org/hib_docs/v3/re ... child.html
the Room entity is managing the FK property_id link,
the inverse attribute tells the collection not to update the link.
all-delete-orphan: a Child can't really exist without its parent
-->
<!-- one-to-many association between Property and Room -->
<set name="rooms" table="room" inverse="true" cascade="all-delete-orphan" >
<key column="property_id" />
<one-to-many class="Room" />
</set>

<property
name="appearOnPublic"
type="java.lang.Boolean"
column="public_access"
/>

<property
name="price"
type="java.lang.Integer"
column="price"
/>

</class>
</hibernate-mapping>

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping
schema="hfydb"
default-cascade="save-update, delete"
auto-import="true"
package="domain"
default-lazy="false">

<class
name="domain.Room"
table="room"
>

<id
name="id"
type="java.lang.Integer"
column="room_id"
>
<generator class="increment" />
</id>

<!-- Make FK property_id part of the mapping -->
<many-to-one name="property" class="Property" column="property_id" not-null="true" />
<many-to-one name="type" class="Type" column="type_id" cascade="none" />

<property
name="name"
type="java.lang.String"
column="name"
not-null="true"
length="30"
/>

</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
Session session = _factory.openSession();
Property p = new Property();
session.delete(p);
session.flush();
/*Close session */
session.close();
Full stack trace of any exception that occurs:
Hibernate: delete from hfydb.property where property_id=?
15:03:02,609 DEBUG AbstractBatcher:476 - preparing statement
15:03:02,609 DEBUG AbstractEntityPersister:2391 - Deleting entity: [domain.Address#2]
15:03:02,625 DEBUG AbstractBatcher:44 - Executing batch size: 1
15:03:02,687 DEBUG AbstractBatcher:366 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
15:03:02,687 DEBUG AbstractBatcher:525 - closing statement
15:03:02,687 DEBUG ConnectionManager:266 - skipping aggressive-release due to flush cycle
15:03:02,687 DEBUG JDBCExceptionReporter:63 - Could not execute JDBC batch update [delete from hfydb.property where property_id=?]
java.sql.BatchUpdateException: Cannot delete or update a parent row: a foreign key constraint fails (`hfydb/room`, CONSTRAINT `FK3580DB4834C3A4` FOREIGN KEY (`PROPERTY_ID`) REFERENCES `property` (`PROPERTY_ID`))

Name and version of the database you are using:
mysql
The generated SQL (show_sql=true):
15:03:02,609 DEBUG SQL:393 - delete from hfydb.property where property_id=?
Debug level Hibernate log excerpt:


Problems with Session and transaction handling?

Read this: http://hibernate.org/42.html


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 02, 2006 7:42 pm 
Regular
Regular

Joined: Tue Jul 13, 2004 2:27 am
Posts: 73
Location: Singapore
i remember reading something somewhere talking abt this. if I remember correctly, you have to do this in DB.

some like this:
CREATE TABLE child (id INT, parent_id INT,
INDEX par_ind (parent_id),
FOREIGN KEY (parent_id) REFERENCES parent(id)
ON DELETE CASCADE


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

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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.