iHHH-2269 : added tests for bidirectional one-to-many associations with both sides cascading; one test fails with TransientObjectException if the inverse collection is marked CascadeType.DELETE_ORPHAN
git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@14069 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
parent
bbc9029b29
commit
c21fa75f77
|
@ -0,0 +1,145 @@
|
|||
//$Id: $
|
||||
|
||||
package org.hibernate.test.cascade;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import junit.framework.Test;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.Transaction;
|
||||
import org.hibernate.junit.functional.FunctionalTestCase;
|
||||
import org.hibernate.junit.functional.FunctionalTestClassTestSuite;
|
||||
|
||||
/**
|
||||
* @author Gail Badner (based on annotations test case submitted by Edward Costello)
|
||||
*/
|
||||
|
||||
/**
|
||||
* Test case to illustrate that when a delete-orphan cascade is used on a
|
||||
* one-to-many collection and the many-to-one side is also cascaded a
|
||||
* TransientObjectException is thrown.
|
||||
*/
|
||||
public class BidirectionalOneToManyCascadeTest extends FunctionalTestCase {
|
||||
|
||||
public BidirectionalOneToManyCascadeTest(String name) {
|
||||
super( name );
|
||||
}
|
||||
|
||||
public String[] getMappings() {
|
||||
return new String[] {
|
||||
"cascade/Child.hbm.xml",
|
||||
"cascade/DeleteOrphanChild.hbm.xml",
|
||||
"cascade/Parent.hbm.xml"
|
||||
};
|
||||
}
|
||||
|
||||
public static Test suite() {
|
||||
return new FunctionalTestClassTestSuite( BidirectionalOneToManyCascadeTest.class );
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the parent object with a child when both the one-to-many and
|
||||
* many-to-one associations use cascade="all"
|
||||
*/
|
||||
public void testSaveParentWithChild() {
|
||||
Session session = openSession();
|
||||
Transaction txn = session.beginTransaction();
|
||||
Parent parent = new Parent();
|
||||
Child child = new Child();
|
||||
child.setParent( parent );
|
||||
parent.setChildren( Collections.singleton( child ) );
|
||||
session.save( parent );
|
||||
txn.commit();
|
||||
session.close();
|
||||
|
||||
session = openSession();
|
||||
txn = session.beginTransaction();
|
||||
parent = ( Parent ) session.get( Parent.class, parent.getId() );
|
||||
assertEquals( 1, parent.getChildren().size() );
|
||||
assertEquals( 0, parent.getDeleteOrphanChildren().size() );
|
||||
session.delete( parent );
|
||||
txn.commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the child object with the parent when both the one-to-many and
|
||||
* many-to-one associations use cascade="all"
|
||||
*/
|
||||
public void testSaveChildWithParent() {
|
||||
Session session = openSession();
|
||||
Transaction txn = session.beginTransaction();
|
||||
Parent parent = new Parent();
|
||||
Child child = new Child();
|
||||
child.setParent( parent );
|
||||
parent.setChildren( Collections.singleton( child ) );
|
||||
session.save( child );
|
||||
txn.commit();
|
||||
session.close();
|
||||
|
||||
session = openSession();
|
||||
txn = session.beginTransaction();
|
||||
parent = ( Parent ) session.get( Parent.class, parent.getId() );
|
||||
assertEquals( 1, parent.getChildren().size() );
|
||||
assertEquals( 0, parent.getDeleteOrphanChildren().size() );
|
||||
session.delete( parent );
|
||||
txn.commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the parent object with a child when the one-to-many association
|
||||
* uses cascade="all-delete-orphan" and the many-to-one association uses
|
||||
* cascade="all"
|
||||
*/
|
||||
public void testSaveParentWithOrphanDeleteChild() {
|
||||
Session session = openSession();
|
||||
Transaction txn = session.beginTransaction();
|
||||
Parent parent = new Parent();
|
||||
DeleteOrphanChild child = new DeleteOrphanChild();
|
||||
child.setParent( parent );
|
||||
parent.setDeleteOrphanChildren( Collections.singleton( child ) );
|
||||
session.save( parent );
|
||||
txn.commit();
|
||||
session.close();
|
||||
|
||||
session = openSession();
|
||||
txn = session.beginTransaction();
|
||||
parent = ( Parent ) session.get( Parent.class, parent.getId() );
|
||||
assertEquals( 0, parent.getChildren().size() );
|
||||
assertEquals( 1, parent.getDeleteOrphanChildren().size() );
|
||||
session.delete( parent );
|
||||
txn.commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the child object with the parent when the one-to-many association
|
||||
* uses cascade="all-delete-orphan" and the many-to-one association uses
|
||||
* cascade="all"
|
||||
* <p/>
|
||||
* This test is known to fail. See HHH-2269.
|
||||
*/
|
||||
public void testSaveOrphanDeleteChildWithParentFailureExpected() {
|
||||
Session session = openSession();
|
||||
Transaction txn = session.beginTransaction();
|
||||
Parent parent = new Parent();
|
||||
DeleteOrphanChild child = new DeleteOrphanChild();
|
||||
child.setParent( parent );
|
||||
parent.setDeleteOrphanChildren( Collections.singleton( child ) );
|
||||
session.save( child );
|
||||
txn.commit();
|
||||
session.close();
|
||||
|
||||
session = openSession();
|
||||
txn = session.beginTransaction();
|
||||
parent = ( Parent ) session.get( Parent.class, parent.getId() );
|
||||
assertEquals( 0, parent.getChildren().size() );
|
||||
assertEquals( 1, parent.getDeleteOrphanChildren().size() );
|
||||
session.delete( parent );
|
||||
txn.commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package org.hibernate.test.cascade;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
/**
|
||||
* Implementation of CascadeSuite.
|
||||
*/
|
||||
public class CascadeSuite {
|
||||
|
||||
public static Test suite() {
|
||||
TestSuite suite = new TestSuite( "Cascade tests" );
|
||||
suite.addTest( BidirectionalOneToManyCascadeTest.suite() );
|
||||
suite.addTest( RefreshTest.suite() );
|
||||
return suite;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
|
||||
|
||||
<hibernate-mapping package="org.hibernate.test.cascade">
|
||||
<class name="Child" table="Child">
|
||||
<id name="id" column="id" type="java.lang.Long">
|
||||
<generator class="native"/>
|
||||
</id>
|
||||
<many-to-one name="parent" class="Parent" not-null="false" cascade="all"/>
|
||||
</class>
|
||||
</hibernate-mapping>
|
|
@ -0,0 +1,29 @@
|
|||
package org.hibernate.test.cascade;
|
||||
|
||||
/**
|
||||
* Created by IntelliJ IDEA.
|
||||
* User: Gail
|
||||
* Date: Jan 2, 2007
|
||||
* Time: 4:51:29 PM
|
||||
* To change this template use File | Settings | File Templates.
|
||||
*/
|
||||
public class Child {
|
||||
private Long id;
|
||||
private Parent parent;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Parent getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public void setParent(Parent parent) {
|
||||
this.parent = parent;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
|
||||
|
||||
<hibernate-mapping package="org.hibernate.test.cascade">
|
||||
<class name="DeleteOrphanChild" table="DeleteOrphanChild">
|
||||
<id name="id" column="id" type="java.lang.Long">
|
||||
<generator class="native"/>
|
||||
</id>
|
||||
<many-to-one name="parent" class="Parent" not-null="false" cascade="all"/>
|
||||
</class>
|
||||
</hibernate-mapping>
|
|
@ -0,0 +1,29 @@
|
|||
package org.hibernate.test.cascade;
|
||||
|
||||
/**
|
||||
* Created by IntelliJ IDEA.
|
||||
* User: Gail
|
||||
* Date: Jan 2, 2007
|
||||
* Time: 4:52:10 PM
|
||||
* To change this template use File | Settings | File Templates.
|
||||
*/
|
||||
public class DeleteOrphanChild {
|
||||
private Long id;
|
||||
private Parent parent;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Parent getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public void setParent(Parent parent) {
|
||||
this.parent = parent;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
|
||||
|
||||
<hibernate-mapping package="org.hibernate.test.cascade">
|
||||
<class name="Parent" table="Parent">
|
||||
<id name="id" column="id" type="java.lang.Long">
|
||||
<generator class="native"/>
|
||||
</id>
|
||||
<set name="children" cascade="all" inverse="true">
|
||||
<key column="parent"/>
|
||||
<one-to-many class="Child"/>
|
||||
</set>
|
||||
<set name="deleteOrphanChildren" cascade="all-delete-orphan" inverse="true">
|
||||
<key column="parent"/>
|
||||
<one-to-many class="DeleteOrphanChild"/>
|
||||
</set>
|
||||
</class>
|
||||
</hibernate-mapping>
|
|
@ -0,0 +1,40 @@
|
|||
package org.hibernate.test.cascade;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Created by IntelliJ IDEA.
|
||||
* User: Gail
|
||||
* Date: Jan 2, 2007
|
||||
* Time: 4:50:24 PM
|
||||
* To change this template use File | Settings | File Templates.
|
||||
*/
|
||||
public class Parent {
|
||||
private Long id;
|
||||
private Set deleteOrphanChildren;
|
||||
private Set children;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Set getDeleteOrphanChildren() {
|
||||
return deleteOrphanChildren;
|
||||
}
|
||||
|
||||
public void setDeleteOrphanChildren(Set deleteOrphanChildren) {
|
||||
this.deleteOrphanChildren = deleteOrphanChildren;
|
||||
}
|
||||
|
||||
public Set getChildren() {
|
||||
return children;
|
||||
}
|
||||
|
||||
public void setChildren(Set children) {
|
||||
this.children = children;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue