[hibernate-commits] Hibernate SVN: r20883 - in core/branches/Branch_3_3_2_GA_CP: testsuite/src/test/java/org/hibernate/test/propertyref and 1 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Tue Jan 25 00:44:12 EST 2011


Author: stliu
Date: 2011-01-25 00:44:12 -0500 (Tue, 25 Jan 2011)
New Revision: 20883

Added:
   core/branches/Branch_3_3_2_GA_CP/testsuite/src/test/java/org/hibernate/test/propertyref/cachedcollections/
   core/branches/Branch_3_3_2_GA_CP/testsuite/src/test/java/org/hibernate/test/propertyref/cachedcollections/CachedPropertyRefCollectionTest.java
   core/branches/Branch_3_3_2_GA_CP/testsuite/src/test/java/org/hibernate/test/propertyref/cachedcollections/ManagedObject.java
   core/branches/Branch_3_3_2_GA_CP/testsuite/src/test/java/org/hibernate/test/propertyref/cachedcollections/Mappings.hbm.xml
Modified:
   core/branches/Branch_3_3_2_GA_CP/core/src/main/java/org/hibernate/engine/loading/CollectionLoadContext.java
Log:
JBPAPP-5765 HHH-5853 Problem loading cachable collections defined with a property-ref key with a versioned owner

Modified: core/branches/Branch_3_3_2_GA_CP/core/src/main/java/org/hibernate/engine/loading/CollectionLoadContext.java
===================================================================
--- core/branches/Branch_3_3_2_GA_CP/core/src/main/java/org/hibernate/engine/loading/CollectionLoadContext.java	2011-01-25 05:28:27 UTC (rev 20882)
+++ core/branches/Branch_3_3_2_GA_CP/core/src/main/java/org/hibernate/engine/loading/CollectionLoadContext.java	2011-01-25 05:44:12 UTC (rev 20883)
@@ -34,6 +34,7 @@
 
 import org.hibernate.CacheMode;
 import org.hibernate.EntityMode;
+import org.hibernate.HibernateException;
 import org.hibernate.cache.CacheKey;
 import org.hibernate.cache.entry.CollectionCacheEntry;
 import org.hibernate.collection.PersistentCollection;
@@ -314,7 +315,27 @@
 
 		final Object version;
 		if ( persister.isVersioned() ) {
-			final Object collectionOwner = getLoadContext().getPersistenceContext().getCollectionOwner( lce.getKey(), persister );
+			Object collectionOwner = getLoadContext().getPersistenceContext().getCollectionOwner( lce.getKey(), persister );
+			if ( collectionOwner == null ) {
+				// generally speaking this would be caused by the collection key being defined by a property-ref, thus
+				// the collection key and the owner key would not match up.  In this case, try to use the key of the
+				// owner instance associated with the collection itself, if one.  If the collection does already know
+				// about its owner, that owner should be the same instance as associated with the PC, but we do the
+				// resolution against the PC anyway just to be safe since the lookup should not be costly.
+				if ( lce.getCollection() != null ) {
+					Object linkedOwner = lce.getCollection().getOwner();
+					if ( linkedOwner != null ) {
+						final Serializable ownerKey = persister.getOwnerEntityPersister().getIdentifier( linkedOwner, session.getEntityMode() );
+						collectionOwner = getLoadContext().getPersistenceContext().getCollectionOwner( ownerKey, persister );
+					}
+					if ( collectionOwner == null ) {
+						throw new HibernateException(
+								"Unable to resolve owner of loading collection [" +
+								MessageHelper.collectionInfoString( persister, lce.getKey(), factory ) +
+								"] for second level caching");
+					}
+				}
+			}
 			version = getLoadContext().getPersistenceContext().getEntry( collectionOwner ).getVersion();
 		}
 		else {

Added: core/branches/Branch_3_3_2_GA_CP/testsuite/src/test/java/org/hibernate/test/propertyref/cachedcollections/CachedPropertyRefCollectionTest.java
===================================================================
--- core/branches/Branch_3_3_2_GA_CP/testsuite/src/test/java/org/hibernate/test/propertyref/cachedcollections/CachedPropertyRefCollectionTest.java	                        (rev 0)
+++ core/branches/Branch_3_3_2_GA_CP/testsuite/src/test/java/org/hibernate/test/propertyref/cachedcollections/CachedPropertyRefCollectionTest.java	2011-01-25 05:44:12 UTC (rev 20883)
@@ -0,0 +1,90 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2011, Red Hat Inc. 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 Inc.
+ *
+ * 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.propertyref.cachedcollections;
+
+import org.hibernate.Criteria;
+import org.hibernate.FetchMode;
+import org.hibernate.Hibernate;
+import org.hibernate.Session;
+import org.hibernate.criterion.Restrictions;
+import org.hibernate.junit.functional.FunctionalTestCase;
+
+/**
+ * Set of tests originally developed to verify and fix HHH-5853
+ *
+ * @author Steve Ebersole
+ */
+public class CachedPropertyRefCollectionTest extends FunctionalTestCase {
+	public CachedPropertyRefCollectionTest(String string) {
+		super( string );
+	}
+
+	public String[] getMappings() {
+		return new String[]{"propertyref/cachedcollections/Mappings.hbm.xml"};
+	}
+
+	public void testRetrievalOfCachedCollectionWithPropertyRefKey() {
+		// create the test data...
+		Session session = openSession();
+		session.beginTransaction();
+		ManagedObject mo = new ManagedObject( "test", "test" );
+		mo.getMembers().add( "members" );
+		session.save( mo );
+		session.getTransaction().commit();
+		session.close();
+
+		// First attempt to load it via PK lookup
+		session = openSession();
+		session.beginTransaction();
+		ManagedObject obj = (ManagedObject) session.get( ManagedObject.class, 1L );
+		assertNotNull( obj );
+		assertTrue( Hibernate.isInitialized( obj ) );
+		obj.getMembers().size();
+		assertTrue( Hibernate.isInitialized( obj.getMembers() ) );
+		session.getTransaction().commit();
+		session.close();
+
+		// Now try to access it via natural key
+		session = openSession();
+		session.beginTransaction();
+		Criteria criteria = session.createCriteria( ManagedObject.class )
+				.add( Restrictions.naturalId().set( "name", "test" ) )
+				.setCacheable( true )
+				.setFetchMode( "members", FetchMode.JOIN );
+		obj = (ManagedObject) criteria.uniqueResult();
+		assertNotNull( obj );
+		assertTrue( Hibernate.isInitialized( obj ) );
+		obj.getMembers().size();
+		assertTrue( Hibernate.isInitialized( obj.getMembers() ) );
+		session.getTransaction().commit();
+		session.close();
+
+		// Clean up
+		session = openSession();
+		session.beginTransaction();
+		session.delete( obj );
+		session.getTransaction().commit();
+		session.close();
+	}
+}

Added: core/branches/Branch_3_3_2_GA_CP/testsuite/src/test/java/org/hibernate/test/propertyref/cachedcollections/ManagedObject.java
===================================================================
--- core/branches/Branch_3_3_2_GA_CP/testsuite/src/test/java/org/hibernate/test/propertyref/cachedcollections/ManagedObject.java	                        (rev 0)
+++ core/branches/Branch_3_3_2_GA_CP/testsuite/src/test/java/org/hibernate/test/propertyref/cachedcollections/ManagedObject.java	2011-01-25 05:44:12 UTC (rev 20883)
@@ -0,0 +1,89 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2011, Red Hat Inc. 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 Inc.
+ *
+ * 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.propertyref.cachedcollections;
+
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * @author Steve Ebersole
+ */
+ at SuppressWarnings({"UnusedDeclaration"})
+public class ManagedObject {
+	private Long moid;
+	private int version;
+	private String name = "parent";
+	private String displayName = "";
+	private Set<String> members = new HashSet<String>();
+
+	public ManagedObject() {
+	}
+
+	public ManagedObject(String name, String displayName) {
+		this.name = name;
+		this.displayName = displayName;
+	}
+
+	public Long getMoid() {
+		return this.moid;
+	}
+
+	private void setMoid(Long moid) {
+		this.moid = moid;
+	}
+
+	public int getVersion() {
+		return this.version;
+	}
+
+	private void setVersion(int version) {
+		this.version = version;
+	}
+
+	public String getName() {
+		return name;
+	}
+
+	public void setName(String name) {
+		this.name = name;
+	}
+
+	public String getDisplayName() {
+		return displayName;
+	}
+
+	public void setDisplayName(String dName) {
+		this.displayName = dName;
+	}
+
+	public Set<String> getMembers() {
+		return this.members;
+	}
+
+	public void setMembers(Set<String> members1) {
+		this.members = members1;
+	}
+
+}
+

Added: core/branches/Branch_3_3_2_GA_CP/testsuite/src/test/java/org/hibernate/test/propertyref/cachedcollections/Mappings.hbm.xml
===================================================================
--- core/branches/Branch_3_3_2_GA_CP/testsuite/src/test/java/org/hibernate/test/propertyref/cachedcollections/Mappings.hbm.xml	                        (rev 0)
+++ core/branches/Branch_3_3_2_GA_CP/testsuite/src/test/java/org/hibernate/test/propertyref/cachedcollections/Mappings.hbm.xml	2011-01-25 05:44:12 UTC (rev 20883)
@@ -0,0 +1,49 @@
+<!--
+  ~ Hibernate, Relational Persistence for Idiomatic Java
+  ~
+  ~ Copyright (c) 2011, Red Hat Inc. 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 Inc.
+  ~
+  ~ 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
+  -->
+
+<!DOCTYPE hibernate-mapping PUBLIC
+    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
+    "http://hibernate.org/dtd/hibernate-mapping-3.0.dtd">
+
+<hibernate-mapping package="org.hibernate.test.propertyref.cachedcollections">
+
+    <class name="ManagedObject">
+        <cache usage="read-write"/>
+        <id name="moid" column="MOID">
+            <generator class="increment"/>
+        </id>
+        <natural-id>
+            <property name="name" type="string" column="NAME" not-null="true" length="100" />
+        </natural-id>
+        <version column="VERSION" name="version"/>
+        <property name="displayName" type="string" column="DISPLAYNAME" not-null="true" length="100" lazy="true"/>
+
+	    <set name="members" table="GroupTable" lazy="true">
+            <cache usage="read-write"/>
+            <key column="NAME" property-ref="name"/>
+            <element column="MEMBERNAME" type="string" not-null="true"/>
+        </set>
+    </class>
+
+</hibernate-mapping>
\ No newline at end of file



More information about the hibernate-commits mailing list