From f6399cd15956d6c3ac45b3a66f45ba6144e08fb5 Mon Sep 17 00:00:00 2001 From: Brett Meyer Date: Tue, 8 Apr 2014 11:19:05 -0400 Subject: [PATCH] HHH-9107 throw WrongClassException if attempting to load incorrect subclass from 2lc --- .../event/internal/DefaultLoadEventListener.java | 15 +++++++++++++-- .../cache/polymorphism/PolymorphicCacheTest.java | 15 ++++++++++----- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultLoadEventListener.java b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultLoadEventListener.java index c1cb26f80d..192569456b 100644 --- a/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultLoadEventListener.java +++ b/hibernate-core/src/main/java/org/hibernate/event/internal/DefaultLoadEventListener.java @@ -30,6 +30,7 @@ import org.hibernate.LockMode; import org.hibernate.NonUniqueObjectException; import org.hibernate.PersistentObjectException; import org.hibernate.TypeMismatchException; +import org.hibernate.WrongClassException; import org.hibernate.cache.spi.CacheKey; import org.hibernate.cache.spi.access.SoftLock; import org.hibernate.cache.spi.entry.CacheEntry; @@ -591,7 +592,7 @@ public class DefaultLoadEventListener extends AbstractLockUpgradeEventListener i final CacheKey ck = source.generateCacheKey( event.getEntityId(), persister.getIdentifierType(), - persister.getEntityName() + persister.getRootEntityName() ); final Object ce = CacheHelper.fromSharedCache( source, ck, persister.getCacheAccessStrategy() ); @@ -614,7 +615,17 @@ public class DefaultLoadEventListener extends AbstractLockUpgradeEventListener i } CacheEntry entry = (CacheEntry) persister.getCacheEntryStructure().destructure( ce, factory ); - return convertCacheEntryToEntity( entry, event.getEntityId(), persister, event ); + Object entity = convertCacheEntryToEntity( entry, event.getEntityId(), persister, event ); + + if ( !persister.isInstance( entity ) ) { + throw new WrongClassException( + "loaded object was of wrong class " + entity.getClass(), + event.getEntityId(), + persister.getEntityName() + ); + } + + return entity; } private Object convertCacheEntryToEntity( diff --git a/hibernate-core/src/test/java/org/hibernate/test/cache/polymorphism/PolymorphicCacheTest.java b/hibernate-core/src/test/java/org/hibernate/test/cache/polymorphism/PolymorphicCacheTest.java index 63fd946bab..095edb2360 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/cache/polymorphism/PolymorphicCacheTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/cache/polymorphism/PolymorphicCacheTest.java @@ -26,8 +26,10 @@ package org.hibernate.test.cache.polymorphism; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.fail; import org.hibernate.Session; +import org.hibernate.WrongClassException; import org.hibernate.testing.TestForIssue; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.junit.Test; @@ -58,11 +60,14 @@ public class PolymorphicCacheTest extends BaseCoreFunctionalTestCase { s = openSession(); s.beginTransaction(); - // As the first item is supposed to be a CachedItem1, it shouldn't be returned. - // Note that the Session API is not type safe but, when using the EntityManager.find API, you get a ClassCastException - // if calling find returns the object. - Object thisObjectShouldBeNull = s.get( CachedItem2.class, item1.getId() ); - assertNull( thisObjectShouldBeNull ); + // See HHH-9107 + try { + s.get( CachedItem2.class, item1.getId() ); + fail( "Expected a WrongClassException to be thrown." ); + } + catch (WrongClassException e) { + //expected + } s.getTransaction().commit(); s.close();