From f1ddacda47272eebcbe37fcb2113b279e4212979 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 2b6c182ffe..16c1d2b5ec 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 @@ -32,6 +32,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; @@ -545,7 +546,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() ); @@ -568,7 +569,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();