-->
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: cascade="all-delete-orphan" not cascading
PostPosted: Wed Aug 06, 2008 8:10 am 
Newbie

Joined: Thu Jul 03, 2008 4:41 am
Posts: 3
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:3.2.6

Hello,

I have 3 Entities, the have the following relations:

A(1)--(n)B(1)--(n)C

All realtions are parent-child, that means that when I delete
A, I want all associated Bs to be deleted as well. Also, when B
is deleted (as a result of deletion of A), I want the associated Cs
to be deleted.

I mapped all of the relations a follows:
A:

<list cascade="all-delete-orphan" fetch="join" lazy="false" name="bList">
<key column="AId"/>
<list-index column="listidx"/>
<one-to-many class="B"/>
</list>

B:

<list cascade="all-delete-orphan" fetch="join" lazy="false" name="cList">
<key column="BId"/>
<list-index column="listidx"/>
<one-to-many class="C"/>
</list>


If I just have an A instance and a associated B instance, the
deletion cascades well.

Problem is: If one or more Cn instances are associated to a given
B1, that in turn is associated with A1, the deletion of A1 does _not_
trigger the deletion of B1 and the Cns.

Instead, the foreign key field of B1 is set to null and the Cs are not
touched at all. Hibernate does not use the delete-cascade constraints
of the database but generates delete/update queries in both cases.

Any idea?

Best regards,

Stefan


Top
 Profile  
 
 Post subject: It worked here... Please compare with your setup
PostPosted: Wed Aug 06, 2008 10:34 am 
Beginner
Beginner

Joined: Wed Jul 09, 2008 5:34 am
Posts: 41
Location: Brno, Czech Republic
Dude, I was able to reproduce the correct behaviour here. I tested using MySQL 5 and Hibernate 3.2.6 (which you said is the version you are using).

In the below code, comment the last 4 lines (or change the last commit to a rollback), and look into the database. There should be 1 record for table A, 2 records for table B and 3 records for table C.

If you don't comment it, there should not be any records on any table, except C, which should have 1 record.

This is the relevant java code for my test:
Code:
         tx = s.beginTransaction();

         C c1 = new C();
         c1.setName("c1");

         C c2 = new C();
         c2.setName("c2");

         C c3 = new C();
         c3.setName("c3");

         B b1 = new B();
         b1.setName("b1");

         B b2 = new B();
         b2.setName("b2");
         List<C> cList = new ArrayList<C>();
         cList.add(c1);
         cList.add(c2);
         b2.setC(cList);

         A a1 = new A();
         a1.setName("a1");
         List<B> bList = new ArrayList<B>();
         bList.add(b1);
         bList.add(b2);
         a1.setB(bList);

         s.save(a1);
         s.save(c3);
         
         tx.commit();
         
         tx = s.beginTransaction();
         a1 = (A) s.createQuery("from A where name = 'a1'").list().iterator().next();
         s.delete(a1);
         tx.commit();


And these are the mappings, each on its own mapping file:

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

   <class name="A">
      <id name="id" column="a_id">
         <generator class="native"/>
      </id>
      <property name="name"/>
      <list cascade="all-delete-orphan" fetch="join" lazy="false" name="b">
         <key column="a_id"/>
         <list-index column="b_idx"/>
         <one-to-many class="B"/>
      </list>
   </class>
   
</hibernate-mapping>


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

   <class name="B">
      <id name="id" column="b_id">
         <generator class="native"/>
      </id>
      <property name="name"/>
      <list cascade="all-delete-orphan" fetch="join" lazy="false" name="c">
         <key column="b_id"/>
         <list-index column="c_idx"/>
         <one-to-many class="C"/>
      </list>
   </class>
   
</hibernate-mapping>


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

   <class name="C">
      <id name="id" column="c_id">
         <generator class="native"/>
      </id>
      <property name="name"/>
   </class>
   
</hibernate-mapping>


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