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?