Author: smendenh(a)redhat.com
Date: 2011-02-02 19:32:32 -0500 (Wed, 02 Feb 2011)
New Revision: 20885
Added:
core/tags/hibernate-3.3.2.GA_CP03_JBPAPP-5842/testsuite/src/test/java/org/hibernate/test/propertyref/cachedcollections/
core/tags/hibernate-3.3.2.GA_CP03_JBPAPP-5842/testsuite/src/test/java/org/hibernate/test/propertyref/cachedcollections/CachedPropertyRefCollectionTest.java
core/tags/hibernate-3.3.2.GA_CP03_JBPAPP-5842/testsuite/src/test/java/org/hibernate/test/propertyref/cachedcollections/ManagedObject.java
core/tags/hibernate-3.3.2.GA_CP03_JBPAPP-5842/testsuite/src/test/java/org/hibernate/test/propertyref/cachedcollections/Mappings.hbm.xml
Modified:
core/tags/hibernate-3.3.2.GA_CP03_JBPAPP-5842/core/src/main/java/org/hibernate/engine/loading/CollectionLoadContext.java
Log:
One-off
https://issues.jboss.org/browse/JBPAPP-5842
Modified:
core/tags/hibernate-3.3.2.GA_CP03_JBPAPP-5842/core/src/main/java/org/hibernate/engine/loading/CollectionLoadContext.java
===================================================================
---
core/tags/hibernate-3.3.2.GA_CP03_JBPAPP-5842/core/src/main/java/org/hibernate/engine/loading/CollectionLoadContext.java 2011-01-31
21:11:52 UTC (rev 20884)
+++
core/tags/hibernate-3.3.2.GA_CP03_JBPAPP-5842/core/src/main/java/org/hibernate/engine/loading/CollectionLoadContext.java 2011-02-03
00:32:32 UTC (rev 20885)
@@ -39,6 +39,7 @@
import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.EntityMode;
import org.hibernate.CacheMode;
+import org.hibernate.HibernateException;
import org.hibernate.cache.entry.CollectionCacheEntry;
import org.hibernate.cache.CacheKey;
import org.hibernate.pretty.MessageHelper;
@@ -315,7 +316,28 @@
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/tags/hibernate-3.3.2.GA_CP03_JBPAPP-5842/testsuite/src/test/java/org/hibernate/test/propertyref/cachedcollections/CachedPropertyRefCollectionTest.java
===================================================================
---
core/tags/hibernate-3.3.2.GA_CP03_JBPAPP-5842/testsuite/src/test/java/org/hibernate/test/propertyref/cachedcollections/CachedPropertyRefCollectionTest.java
(rev 0)
+++
core/tags/hibernate-3.3.2.GA_CP03_JBPAPP-5842/testsuite/src/test/java/org/hibernate/test/propertyref/cachedcollections/CachedPropertyRefCollectionTest.java 2011-02-03
00:32:32 UTC (rev 20885)
@@ -0,0 +1,91 @@
+/*
+ * 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/tags/hibernate-3.3.2.GA_CP03_JBPAPP-5842/testsuite/src/test/java/org/hibernate/test/propertyref/cachedcollections/ManagedObject.java
===================================================================
---
core/tags/hibernate-3.3.2.GA_CP03_JBPAPP-5842/testsuite/src/test/java/org/hibernate/test/propertyref/cachedcollections/ManagedObject.java
(rev 0)
+++
core/tags/hibernate-3.3.2.GA_CP03_JBPAPP-5842/testsuite/src/test/java/org/hibernate/test/propertyref/cachedcollections/ManagedObject.java 2011-02-03
00:32:32 UTC (rev 20885)
@@ -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 java.util.HashSet;
+import java.util.Set;
+
+/**
+ * @author Steve Ebersole
+ */
+@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/tags/hibernate-3.3.2.GA_CP03_JBPAPP-5842/testsuite/src/test/java/org/hibernate/test/propertyref/cachedcollections/Mappings.hbm.xml
===================================================================
---
core/tags/hibernate-3.3.2.GA_CP03_JBPAPP-5842/testsuite/src/test/java/org/hibernate/test/propertyref/cachedcollections/Mappings.hbm.xml
(rev 0)
+++
core/tags/hibernate-3.3.2.GA_CP03_JBPAPP-5842/testsuite/src/test/java/org/hibernate/test/propertyref/cachedcollections/Mappings.hbm.xml 2011-02-03
00:32:32 UTC (rev 20885)
@@ -0,0 +1,31 @@
+<!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>