Java Mailing List Archive

http://www.gg3721.com/

Home » Hibernate Commits List »

[hibernate-commits] Hibernate SVN: r20223 - in
 core/branches/Branch_3_2_4_SP1_CP:
 src/org/hibernate/event/def and 1 other directories.

hibernate-commits

2010-08-23


Author LoginPost Reply
Author: stliu
Date: 2010-08-23 07:13:03 -0400 (Mon, 23 Aug 2010)
New Revision: 20223

Added:
 core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/cascade/CascadeTestWithAssignedParentIdTest.java
 core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/cascade/Child.java
 core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/cascade/ChildForParentWithAssignedId.hbm.xml
 core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/cascade/Parent.java
 core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/cascade/ParentWithAssignedId.hbm.xml
Modified:
 core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/cache/OptimisticCache.java
 core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/event/def/AbstractSaveEventListener.java
Log:
JBPAPP-4927 HHH-3334 Cascade-save breaks if parent ID is assigned (delays insert) and child has identity ID (early insert)

Modified: core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/cache/OptimisticCache.java
===================================================================
--- core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/cache/OptimisticCache.java  2010-08-23 10:53:37 UTC (rev 20222)
+++ core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/cache/OptimisticCache.java  2010-08-23 11:13:03 UTC (rev 20223)
@@(protected) @@
* A contract for transactional cache implementations which support
* optimistic locking of items within the cache.
* <p/>
- * The optimisitic locking capabilities are only utilized for
+ * The optimistic locking capabilities are only utilized for
* the entity cache regions.
* <p/>
* Unlike the methods on the {@(protected)

Modified: core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/event/def/AbstractSaveEventListener.java
===================================================================
--- core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/event/def/AbstractSaveEventListener.java  2010-08-23 10:53:37 UTC (rev 20222)
+++ core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/event/def/AbstractSaveEventListener.java  2010-08-23 11:13:03 UTC (rev 20223)
@@(protected) @@
   boolean inTxn = source.getJDBCContext().isTransactionInProgress();
   boolean shouldDelayIdentityInserts = !inTxn && !requiresImmediateIdAccess;

-    if ( useIdentityColumn && !shouldDelayIdentityInserts ) {
-      log.trace( "executing insertions" );
-      source.getActionQueue().executeInserts();
-    }
-
   // Put a placeholder in entries, so we don't recurse back and try to save() the
   // same object again. QUESTION: should this be done before onSave() is called?
   // likewise, should it be done before onUpdate()?
@@(protected) @@
   );

   cascadeBeforeSave( source, persister, entity, anything );
+    if ( useIdentityColumn && !shouldDelayIdentityInserts ) {
+      log.trace( "executing insertions" );
+      source.getActionQueue().executeInserts();
+    }

   Object[] values = persister.getPropertyValuesToInsert( entity, getMergeMap( anything ), source );
   Type[] types = persister.getPropertyTypes();

Added: core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/cascade/CascadeTestWithAssignedParentIdTest.java
===================================================================
--- core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/cascade/CascadeTestWithAssignedParentIdTest.java                  (rev 0)
+++ core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/cascade/CascadeTestWithAssignedParentIdTest.java  2010-08-23 11:13:03 UTC (rev 20223)
@@(protected) @@
+/*
+ * 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 );
+  }
+
+  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 ) );
+    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_2_4_SP1_CP/test/org/hibernate/test/cascade/Child.java
===================================================================
--- core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/cascade/Child.java                  (rev 0)
+++ core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/cascade/Child.java  2010-08-23 11:13:03 UTC (rev 20223)
@@(protected) @@
+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;
+  }
+}

Added: core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/cascade/ChildForParentWithAssignedId.hbm.xml
===================================================================
--- core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/cascade/ChildForParentWithAssignedId.hbm.xml                  (rev 0)
+++ core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/cascade/ChildForParentWithAssignedId.hbm.xml  2010-08-23 11:13:03 UTC (rev 20223)
@@(protected) @@
+<?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_2_4_SP1_CP/test/org/hibernate/test/cascade/Parent.java
===================================================================
--- core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/cascade/Parent.java                  (rev 0)
+++ core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/cascade/Parent.java  2010-08-23 11:13:03 UTC (rev 20223)
@@(protected) @@
+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;
+  }
+}

Added: core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/cascade/ParentWithAssignedId.hbm.xml
===================================================================
--- core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/cascade/ParentWithAssignedId.hbm.xml                  (rev 0)
+++ core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/cascade/ParentWithAssignedId.hbm.xml  2010-08-23 11:13:03 UTC (rev 20223)
@@(protected) @@
+<?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>

_______________________________________________
hibernate-commits mailing list
hibernate-commits@(protected)
https://lists.jboss.org/mailman/listinfo/hibernate-commits
©2008 gg3721.com - Jax Systems, LLC, U.S.A.