-->
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.  [ 4 posts ] 
Author Message
 Post subject: simple many-to-one and cascading delete
PostPosted: Mon Aug 16, 2004 12:58 pm 
Newbie

Joined: Fri Aug 13, 2004 3:49 pm
Posts: 2
Hibernate version:
2.1.6
Mapping documents:

parent.hbm.xml:

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

<hibernate-mapping>

<class name="Parent" table="PARENT">
<id name="parentId" type="string">
<generator class="uuid.hex"/>
</id>
<property name="parentProperty1" type="string" />
</class>

</hibernate-mapping>

child.hbm.xml:

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

<hibernate-mapping>

<class name="Child" table="CHILD">
<id name="itemId" column="itemId" type="string">
<generator class="uuid.hex"/>
</id>

<property name="childProperty1" type="string" />
<many-to-one name="owner" column="ownerId" not-null="true" cascade="delete"/>
</class>

</hibernate-mapping>



Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:
[FTF] - [TESTMACHINE]DELETE statement conflicted with COLUMN REFERENCE constraint 'FKF0C5AC3AC1221B2E'. The conflict occurred

in database 'testdb', table 'CHILD', column 'parentId'.
[FTF] - could not delete: [Parent#15ca8d00fe68722000fe6872249f0003]
[FTF] java.sql.SQLException: [TESTMACHINE]DELETE statement conflicted with COLUMN REFERENCE constraint 'FKF0C5AC3AC1221B2E'.

The conflict occurred in database 'testdb', table 'CHILD', column 'parentId'.
[FTF] at com.inet.tds.e.a(Unknown Source)
[FTF] at com.inet.tds.e.a(Unknown Source)
[FTF] at com.inet.tds.b.int(Unknown Source)
[FTF] at com.inet.tds.b.executeUpdate(Unknown Source)
[FTF] at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:22)
[FTF] at net.sf.hibernate.persister.EntityPersister.delete(EntityPersister.java:599)
[FTF] at net.sf.hibernate.impl.ScheduledDeletion.execute(ScheduledDeletion.java:29)
[FTF] at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2414)
[FTF] at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2372)
[FTF] at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2236)
[FTF] at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
<rest of stack trace is at application layer>

Name and version of the database you are using:
MS SQL Server 2000

Debug level Hibernate log excerpt:

Foreign key constraint referenced by the stack trace:
alter table CHILD add constraint FKF0C5AC3AC1221B2E foreign key (parentId) references PARENT;

I've defined two simple classes, Parent and Child, and want the Child to keep a reference to its parent. Each Parent can have more than one child. What I want to happen is for all the children of a parent to be deleted when the parent is deleted, but I don't want the parent to hold references to its children. I thought I could achieve this effect by defining a <many-to-one> mapping in the child, with cascade="delete". Unfortunately, the children are not being deleted when I perform
session.delete(parent), and the foreign key constraint is being violated. I am beginning to suspect that the parent has to keep a set of all its children in order for the cascading delete to happen, but if that's the case, then why have a cascade attribute on the <many-to-one> tag? I don't get it.

Any help would be much appreciated.

Thanks,

Justin


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 16, 2004 2:42 pm 
Regular
Regular

Joined: Mon Oct 06, 2003 7:17 am
Posts: 58
Location: Switzerland
The many-to-one defines the relationship from the child to the parent and so is the cascade type. When you delete a child then the parent is also deleted. Then you have also defined that relationship can't be null. So when you delete the parent first, the association to the parent is null and the constraint is now violated.

Reto


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 16, 2004 3:39 pm 
Newbie

Joined: Fri Aug 13, 2004 3:49 pm
Posts: 2
That makes sense. So is the only way to get cascading deletion of children when the parent is deleted to have a one-to-many mapping in a <set> property on the parent? That is, there is no way to achieve this effect without the parent object storing pointers to children?

_________________
Justin Haugh - Integration Consultant - Trilogy Software, Inc.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 16, 2004 4:44 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
if your parent doesn't know about his children, how do you want him to cause the deletion of the children?
So you must declare a one(parent) to many(children)

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.