HHH-5853: Problem loading cachable collections defined with a property-ref key with a versioned owner

This commit is contained in:
Steve Ebersole 2011-01-20 15:12:52 -06:00 committed by JPAV
parent 2641842ba9
commit 7c3aafb7c2
4 changed files with 253 additions and 1 deletions

View File

@ -32,6 +32,7 @@ import java.util.List;
import java.util.Set;
import org.hibernate.CacheMode;
import org.hibernate.EntityMode;
import org.hibernate.HibernateException;
import org.hibernate.HibernateLogger;
import org.hibernate.cache.CacheKey;
import org.hibernate.cache.entry.CollectionCacheEntry;
@ -283,7 +284,28 @@ public class CollectionLoadContext {
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 );
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 {

View File

@ -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.testing.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();
}
}

View File

@ -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;
}
}

View File

@ -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>