From ceaf2e320fd743357beb8eb9f4a6524d0c96e60b Mon Sep 17 00:00:00 2001 From: Emmanuel Bernard Date: Wed, 2 Jul 2014 01:06:43 +0200 Subject: [PATCH] HHH-9269 Store persister to ReferenceCacheEntryImpl to avoid costly lookup at read time --- .../spi/entry/ReferenceCacheEntryImpl.java | 17 ++++++++++++----- .../internal/DefaultLoadEventListener.java | 7 +++++-- .../entity/AbstractEntityPersister.java | 2 +- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/cache/spi/entry/ReferenceCacheEntryImpl.java b/hibernate-core/src/main/java/org/hibernate/cache/spi/entry/ReferenceCacheEntryImpl.java index 6babb78bb8..fb7372533f 100644 --- a/hibernate-core/src/main/java/org/hibernate/cache/spi/entry/ReferenceCacheEntryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/cache/spi/entry/ReferenceCacheEntryImpl.java @@ -25,6 +25,8 @@ package org.hibernate.cache.spi.entry; import java.io.Serializable; +import org.hibernate.persister.entity.EntityPersister; + /** * Specialized CacheEntry for storing direct references to entity instances. * @@ -32,17 +34,18 @@ import java.io.Serializable; */ public class ReferenceCacheEntryImpl implements CacheEntry { private final Object reference; - private final String subclass; + // passing the persister avoids a costly persister lookup by class name at cache retrieval time + private final EntityPersister subclassPersister; /** * Constructs a ReferenceCacheEntryImpl * * @param reference The reference entity instance - * @param subclass The specific subclass + * @param subclassPersister The specific subclass persister */ - public ReferenceCacheEntryImpl(Object reference, String subclass) { + public ReferenceCacheEntryImpl(Object reference, EntityPersister subclassPersister) { this.reference = reference; - this.subclass = subclass; + this.subclassPersister = subclassPersister; } /** @@ -61,7 +64,11 @@ public class ReferenceCacheEntryImpl implements CacheEntry { @Override public String getSubclass() { - return subclass; + return subclassPersister.getEntityName(); + } + + public EntityPersister getSubclassPersister() { + return subclassPersister; } @Override 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 192569456b..62e63c2c78 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 @@ -636,7 +636,7 @@ public class DefaultLoadEventListener extends AbstractLockUpgradeEventListener i final EventSource session = event.getSession(); final SessionFactoryImplementor factory = session.getFactory(); - final EntityPersister subclassPersister = factory.getEntityPersister( entry.getSubclass() ); + final EntityPersister subclassPersister; if ( LOG.isTraceEnabled() ) { LOG.tracef( @@ -659,7 +659,8 @@ public class DefaultLoadEventListener extends AbstractLockUpgradeEventListener i ); } - entity = ( (ReferenceCacheEntryImpl) entry ).getReference(); + ReferenceCacheEntryImpl referenceCacheEntry = (ReferenceCacheEntryImpl) entry; + entity = referenceCacheEntry.getReference(); if ( entity == null ) { throw new IllegalStateException( "Reference cache entry contained null : " + MessageHelper.infoString( @@ -669,8 +670,10 @@ public class DefaultLoadEventListener extends AbstractLockUpgradeEventListener i ) ); } + subclassPersister = referenceCacheEntry.getSubclassPersister(); } else { + subclassPersister = factory.getEntityPersister( entry.getSubclass() ); final Object optionalObject = event.getInstanceToLoad(); entity = optionalObject == null ? session.instantiate( subclassPersister, entityId ) diff --git a/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java b/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java index b20f0b1c4a..212e5ac311 100644 --- a/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java +++ b/hibernate-core/src/main/java/org/hibernate/persister/entity/AbstractEntityPersister.java @@ -5267,7 +5267,7 @@ public abstract class AbstractEntityPersister @Override public CacheEntry buildCacheEntry(Object entity, Object[] state, Object version, SessionImplementor session) { - return new ReferenceCacheEntryImpl( entity, persister.getEntityName() ); + return new ReferenceCacheEntryImpl( entity, persister ); } }