[hibernate-commits] Hibernate SVN: r19966 - core/branches/Branch_3_5/testsuite/src/test/java/org/hibernate/test/cascade.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Mon Jul 19 17:52:36 EDT 2010


Author: gbadner
Date: 2010-07-19 17:52:35 -0400 (Mon, 19 Jul 2010)
New Revision: 19966

Added:
   core/branches/Branch_3_5/testsuite/src/test/java/org/hibernate/test/cascade/CascadeTestWithAssignedParentIdTest.java
   core/branches/Branch_3_5/testsuite/src/test/java/org/hibernate/test/cascade/ChildForParentWithAssignedId.hbm.xml
   core/branches/Branch_3_5/testsuite/src/test/java/org/hibernate/test/cascade/ParentWithAssignedId.hbm.xml
Log:
HHH-3334 : Added FailureExpected test for Cascade-save breaks if parent ID is assigned (delays insert) and child has identity ID (early insert)

Added: core/branches/Branch_3_5/testsuite/src/test/java/org/hibernate/test/cascade/CascadeTestWithAssignedParentIdTest.java
===================================================================
--- core/branches/Branch_3_5/testsuite/src/test/java/org/hibernate/test/cascade/CascadeTestWithAssignedParentIdTest.java	                        (rev 0)
+++ core/branches/Branch_3_5/testsuite/src/test/java/org/hibernate/test/cascade/CascadeTestWithAssignedParentIdTest.java	2010-07-19 21:52:35 UTC (rev 19966)
@@ -0,0 +1,119 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2010, Red Hat Middleware LLC or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors.  All third-party contributions are
+ * distributed under license by Red Hat Middleware LLC.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA  02110-1301  USA
+ */
+package org.hibernate.test.cascade;
+
+import java.util.Collections;
+
+import junit.framework.Test;
+
+import org.hibernate.Session;
+import org.hibernate.Transaction;
+import org.hibernate.id.IdentityGenerator;
+import org.hibernate.junit.functional.FunctionalTestCase;
+import org.hibernate.junit.functional.FunctionalTestClassTestSuite;
+
+/**
+ * @author Wallace Wadge (based on code by Gail Badner)
+ */
+
+/**
+ * Test case to illustrate that when a child table attempts to cascade to a parent and the parent's Id
+ * is set to assigned, an exception thrown (not-null property references a null or transient value). 
+ * This error only occurs if the parent link in marked as not nullable.
+ */
+public class CascadeTestWithAssignedParentIdTest extends FunctionalTestCase {
+
+	public CascadeTestWithAssignedParentIdTest(String name) {
+		super( name );
+	}
+
+	public String[] getMappings() {
+		return new String[] {
+				"cascade/ChildForParentWithAssignedId.hbm.xml",
+				"cascade/ParentWithAssignedId.hbm.xml"
+		};
+	}
+
+	public static Test suite() {
+		return new FunctionalTestClassTestSuite( CascadeTestWithAssignedParentIdTest.class );
+	}
+
+
+	/**
+	 * Saves the child object with the parent when both the one-to-many and
+	 * many-to-one associations use cascade="all"
+	 */
+	public void testSaveChildWithParentFailureExpected() {
+		if ( ! IdentityGenerator.class.isAssignableFrom( getDialect().getNativeIdentifierGeneratorClass() ) ) {
+			reportSkip( "FailureExpected test passes when native id generator is not IdentityGenerator",
+					"parent insert is assigned (delayed) when child is identity (early insert)" );
+			fail( "test is expected to fail" );
+			return;
+		}
+		Session session = openSession();
+		Transaction txn = session.beginTransaction();
+		Parent parent = new Parent();
+		Child child = new Child();
+		child.setParent( parent );
+		parent.setChildren( Collections.singleton( child ) );
+		parent.setId(new Long(123L));
+		// this should figure out that the parent needs saving first since id is assigned.
+		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() );
+		txn.commit();
+		session.close();
+	}
+
+	public void testSaveChildWithParent() {
+		if ( IdentityGenerator.class.isAssignableFrom( getDialect().getNativeIdentifierGeneratorClass() ) ) {
+			reportSkip( "test is known to fail when native id generator is IdentityGenerator",
+					"parent insert is assigned (delayed) when child has identity (early insert)" );
+			return;
+		}
+		Session session = openSession();
+		Transaction txn = session.beginTransaction();
+		Parent parent = new Parent();
+		Child child = new Child();
+		child.setParent( parent );
+		parent.setChildren( Collections.singleton( child ) );
+		parent.setId(new Long(123L));
+		// this should figure out that the parent needs saving first since id is assigned.
+		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() );
+		txn.commit();
+		session.close();
+	}
+}

Added: core/branches/Branch_3_5/testsuite/src/test/java/org/hibernate/test/cascade/ChildForParentWithAssignedId.hbm.xml
===================================================================
--- core/branches/Branch_3_5/testsuite/src/test/java/org/hibernate/test/cascade/ChildForParentWithAssignedId.hbm.xml	                        (rev 0)
+++ core/branches/Branch_3_5/testsuite/src/test/java/org/hibernate/test/cascade/ChildForParentWithAssignedId.hbm.xml	2010-07-19 21:52:35 UTC (rev 19966)
@@ -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="true" cascade="all"  />
+    </class>
+</hibernate-mapping>

Added: core/branches/Branch_3_5/testsuite/src/test/java/org/hibernate/test/cascade/ParentWithAssignedId.hbm.xml
===================================================================
--- core/branches/Branch_3_5/testsuite/src/test/java/org/hibernate/test/cascade/ParentWithAssignedId.hbm.xml	                        (rev 0)
+++ core/branches/Branch_3_5/testsuite/src/test/java/org/hibernate/test/cascade/ParentWithAssignedId.hbm.xml	2010-07-19 21:52:35 UTC (rev 19966)
@@ -0,0 +1,14 @@
+<?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="assigned"/>
+        </id>
+        <set name="children" cascade="all" inverse="true">
+            <key column="parent"/>
+            <one-to-many class="Child"/>
+        </set>
+    </class>
+</hibernate-mapping>



More information about the hibernate-commits mailing list