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
|