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 delete not propagating to children
PostPosted: Mon Apr 16, 2007 1:17 pm 
Beginner
Beginner

Joined: Mon Aug 22, 2005 1:22 am
Posts: 36
Hibernate version: 3.2

Mapping documents:

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 auto-import="false" >

    <class name="hibtest.A" table="HIBTEST_A" >
       
        <id name="id" column="AID" access="field" type="long" >
            <generator class="identity"/>
        </id>

        <set name="c_children"
         access="field"
         inverse="true"
         cascade="delete,delete-orphan" >
            <key on-delete="cascade" not-null="true" column="A_PARENT_ID" />
            <one-to-many class="hibtest.C" />
        </set>

    </class>

    <joined-subclass name="hibtest.B"
                extends="hibtest.A"
                table="HIBTEST_B" >
        <key column="AID" />

        <set name="d_children"
         access="field"
                        inverse="true"
                        cascade="delete,delete-orphan" >
            <key on-delete="cascade" not-null="true" column="B_PARENT_ID" />
            <one-to-many class="hibtest.D" />
        </set>


    </joined-subclass>

    <class name="hibtest.C" table="HIBTEST_C" >
       
        <id name="id" column="CID" access="field" type="long" >
            <generator class="identity"/>
        </id>

   <many-to-one name="aParent"
       access="field"
            column="A_PARENT_ID"
            class="hibtest.A"
            cascade="none"
            />
    </class>

    <joined-subclass name="hibtest.D"
                extends="hibtest.C"
                table="HIBTEST_D" >
        <key column="CID" />

   <many-to-one name="bParent"
       access="field"
            column="B_PARENT_ID"
            class="hibtest.B"
            cascade="none"
            />

    </joined-subclass>


</hibernate-mapping>



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

Code:

package hibtest;

import java.util.*;
import org.hibernate.Session;
import org.hibernate.criterion.*;
import com.fromtherough.mog.hib.HibernateUtil;

import hibtest.A;
import hibtest.B;
import hibtest.C;
import hibtest.D;


public class CreateTest {
    public static
    void main( String[] args ) throws Exception {
   try {

       int cPerA = 8;
       int bCount = 8;
       int dPerB = 8;

       for (int i = 0; i < args.length; i++ ) {
           if ( args[i].startsWith( "-cPerA=" ) ) {
          cPerA = new Integer( parseArg( args[i] ) ).intValue();
      }
      else
           if ( args[i].startsWith( "-bCount=" ) ) {
          bCount = new Integer( parseArg( args[i] ) ).intValue();
      }
      else
           if ( args[i].startsWith( "-dPerB=" ) ) {
          dPerB = new Integer( parseArg( args[i] ) ).intValue();
      }
      else {
          System.out.println( "CreateTest [-cPerA=xxx] [-bCount=xxx] [-dPerB=xxx]" );
          System.exit(1);
      }
       }

       System.out.println();
       System.out.println( "cPerA: " +  cPerA );
       System.out.println( "bCount: " + bCount );
       System.out.println( "dPerB: " + dPerB );
       System.out.println();

       HibernateUtil.beginTransaction();

       B b = new B();

       Long id = (Long)HibernateUtil.currentSession().save( b );
       System.out.println( "B ID: " + id );

       for (int i = 0; i < cPerA; i++ ) {
           C c = new C();
           c.aParent = b;
      HibernateUtil.currentSession().save( c );
      b.c_children.add( c );
       }

       for (int i = 0; i < dPerB; i++ ) {
           D d = new D();
           d.aParent = b;
           d.bParent = b;
      HibernateUtil.currentSession().save( d );
      b.c_children.add( d );
      b.d_children.add( d );
       }

       HibernateUtil.currentSession().update( b );

       HibernateUtil.commitTransaction();

   }
   catch ( Exception e ) {
       HibernateUtil.rollbackTransaction();
       throw e;
   }
   finally {
       HibernateUtil.closeSession();
   }
    }

    private static
    String parseArg( String arg ) {
        int idx = arg.indexOf( "=" );
   if ( idx == -1 ) {
       return null;
   }

   return arg.substring( idx + 1 );
    }

}

package hibtest;

import java.util.*;
import org.hibernate.Session;
import com.fromtherough.mog.hib.HibernateUtil;


public class DeleteTest {
    public static
    void main( String[] args ) throws Exception {
   try {

       if ( args.length < 1 ) {
           throw new Exception( "DeleteTest tournamentId" );
       }

       HibernateUtil.beginTransaction();

       B b = (B)HibernateUtil.currentSession()
             .get( B.class, new Long( args[0] ) );
       if ( b == null ) {
           throw new Exception( "B not found." );
       }
       HibernateUtil.currentSession().delete( b );
       HibernateUtil.commitTransaction();
   }
   catch ( Exception e ) {
       HibernateUtil.rollbackTransaction();
       throw e;
   }
   finally {
       HibernateUtil.closeSession();
   }
    }

    private static
    String parseArg( String arg ) {
        int idx = arg.indexOf( "=" );
   if ( idx == -1 ) {
       return null;
   }

   return arg.substring( idx + 1 );
    }

}




Name and version of the database you are using:

MySql 5.0

The generated SQL (show_sql=true):

Code:
Hibernate: select c_children0_.A_PARENT_ID as A2_1_, c_children0_.CID as CID1_,
c_children0_.CID as CID213_0_, c_children0_.A_PARENT_ID as A2_213_0_, c_children
0_1_.B_PARENT_ID as B2_214_0_, case when c_children0_1_.CID is not null then 1 w
hen c_children0_.CID is not null then 0 end as clazz_0_ from HIBTEST_C c_childre
n0_ left outer join HIBTEST_D c_children0_1_ on c_children0_.CID=c_children0_1_.
CID where c_children0_.A_PARENT_ID=?
Hibernate: select d_children0_.B_PARENT_ID as B2_1_, d_children0_.CID as CID1_,
d_children0_.CID as CID213_0_, d_children0_1_.A_PARENT_ID as A2_213_0_, d_childr
en0_.B_PARENT_ID as B2_214_0_ from HIBTEST_D d_children0_ inner join HIBTEST_C d
_children0_1_ on d_children0_.CID=d_children0_1_.CID where d_children0_.B_PARENT
_ID=?
Hibernate: delete from HIBTEST_B where AID=?
Hibernate: delete from HIBTEST_A where AID=?


I have 4 objects (2 separate object trees)

B inherits A
D inherits C
A contains C
B contains D

When I delete a B, the children are not deleted (A.cChildren and B.dChildren)

Anyone else experience this problem?
Any suggestions?


Top
 Profile  
 
 Post subject: Answer
PostPosted: Mon Apr 16, 2007 3:15 pm 
Beginner
Beginner

Joined: Mon Aug 22, 2005 1:22 am
Posts: 36
I figured it out.

The issue was with <key on-delete="cascade" ...>

I misunderstood what that meant or did not read it carefully the first time.

It is recommended to use on-delete="cascade" in the hibernate docs. However, you must setup your DB to auto delete the data when the referenced table data is deleted....which I did not do.

I would like to use this but my DB skills are not 'administrative' quality. Not sure how to setup the DB to handle this scenario. If someone does, please pass on the info.

BTW... I tried setting up foreign key but I kept getting foreign key violations or cannot update a foreign key (key not found...when it is).


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.