cleanup warnings in PersistenceContext

+ code cleanups in CacheEntityLoaderHelper
This commit is contained in:
Gavin King 2024-09-11 07:40:37 +02:00
parent 3a4da8dd85
commit c57a90e088
3 changed files with 100 additions and 142 deletions

View File

@ -699,6 +699,7 @@ public class StatefulPersistenceContext implements PersistenceContext {
return e; return e;
} }
@Override
public EntityEntry addReferenceEntry( public EntityEntry addReferenceEntry(
final Object entity, final Object entity,
final Status status) { final Status status) {
@ -885,12 +886,14 @@ public class StatefulPersistenceContext implements PersistenceContext {
} }
private Object removeProxyByKey(final EntityKey key) { private Object removeProxyByKey(final EntityKey key) {
final EntityHolderImpl entityHolder; if ( entitiesByKey != null ) {
if ( entitiesByKey != null && ( entityHolder = entitiesByKey.get( key ) ) != null ) { final EntityHolderImpl entityHolder = entitiesByKey.get( key );
if ( entityHolder != null ) {
final Object proxy = entityHolder.proxy; final Object proxy = entityHolder.proxy;
entityHolder.proxy = null; entityHolder.proxy = null;
return proxy; return proxy;
} }
}
return null; return null;
} }
@ -911,11 +914,6 @@ public class StatefulPersistenceContext implements PersistenceContext {
return e == null ? impl : proxyFor( e.getPersister(), e.getEntityKey(), impl ); return e == null ? impl : proxyFor( e.getPersister(), e.getEntityKey(), impl );
} }
@Override
public Object proxyFor(EntityHolder holder) throws HibernateException {
return proxyFor( holder, holder.getDescriptor() );
}
@Override @Override
public Object proxyFor(EntityHolder holder, EntityPersister persister) { public Object proxyFor(EntityHolder holder, EntityPersister persister) {
final Object proxy = holder.getProxy(); final Object proxy = holder.getProxy();
@ -2089,7 +2087,7 @@ public class StatefulPersistenceContext implements PersistenceContext {
@Override @Override
public boolean containsNullifiableEntityKey(Supplier<EntityKey> sek) { public boolean containsNullifiableEntityKey(Supplier<EntityKey> sek) {
return nullifiableEntityKeys != null return nullifiableEntityKeys != null
&& nullifiableEntityKeys.size() != 0 && !nullifiableEntityKeys.isEmpty()
&& nullifiableEntityKeys.contains( sek.get() ); && nullifiableEntityKeys.contains( sek.get() );
} }
@ -2104,7 +2102,7 @@ public class StatefulPersistenceContext implements PersistenceContext {
@Override @Override
public boolean isNullifiableEntityKeysEmpty() { public boolean isNullifiableEntityKeysEmpty() {
return nullifiableEntityKeys == null return nullifiableEntityKeys == null
|| nullifiableEntityKeys.size() == 0; || nullifiableEntityKeys.isEmpty();
} }
@Override @Override

View File

@ -17,7 +17,6 @@ import org.hibernate.HibernateException;
import org.hibernate.Incubating; import org.hibernate.Incubating;
import org.hibernate.Internal; import org.hibernate.Internal;
import org.hibernate.LockMode; import org.hibernate.LockMode;
import org.hibernate.query.Query;
import org.hibernate.collection.spi.PersistentCollection; import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.internal.util.MarkerObject; import org.hibernate.internal.util.MarkerObject;
import org.hibernate.persister.collection.CollectionPersister; import org.hibernate.persister.collection.CollectionPersister;
@ -272,6 +271,10 @@ public interface PersistenceContext {
final EntityPersister persister, final EntityPersister persister,
final boolean disableVersionIncrement); final boolean disableVersionIncrement);
EntityEntry addReferenceEntry(
final Object entity,
final Status status);
/** /**
* Is the given collection associated with this persistence context? * Is the given collection associated with this persistence context?
*/ */
@ -354,16 +357,6 @@ public interface PersistenceContext {
*/ */
Object proxyFor(EntityHolder holder, EntityPersister persister); Object proxyFor(EntityHolder holder, EntityPersister persister);
/**
* Return the existing {@linkplain EntityHolder#getProxy() proxy} associated with
* the given {@link EntityHolder}, or the {@linkplain EntityHolder#getEntity() entity}
* if it contains no proxy.
*
* @deprecated Use {@link #proxyFor(EntityHolder, EntityPersister)} instead.
*/
@Deprecated( forRemoval = true )
Object proxyFor(EntityHolder holder);
/** /**
* Cross between {@link #addEntity(EntityKey, Object)} and {@link #addProxy(EntityKey, Object)} * Cross between {@link #addEntity(EntityKey, Object)} and {@link #addProxy(EntityKey, Object)}
* for use with enhancement-as-proxy * for use with enhancement-as-proxy
@ -498,13 +491,6 @@ public interface PersistenceContext {
*/ */
Object removeProxy(EntityKey key); Object removeProxy(EntityKey key);
// /**
// * Retrieve the set of EntityKeys representing nullifiable references
// * @deprecated Use {@link #containsNullifiableEntityKey(Supplier)} or {@link #registerNullifiableEntityKey(EntityKey)} or {@link #isNullifiableEntityKeysEmpty()}
// */
// @Deprecated
// HashSet getNullifiableEntityKeys();
/** /**
* Return an existing entity holder for the entity key, possibly creating one if necessary. * Return an existing entity holder for the entity key, possibly creating one if necessary.
* Will claim the entity holder by registering the given entity initializer, if it isn't claimed yet. * Will claim the entity holder by registering the given entity initializer, if it isn't claimed yet.
@ -675,16 +661,15 @@ public interface PersistenceContext {
/** /**
* Will entities and proxies that are loaded into this persistence * Will entities and proxies that are loaded into this persistence
* context be made read-only by default? * context be made read-only by default?
* * <p>
* To determine the read-only/modifiable setting for a particular entity * To determine the read-only/modifiable setting for a particular
* or proxy: * entity or proxy, call {@link #isReadOnly(Object)}.
* @see PersistenceContext#isReadOnly(Object)
* @see org.hibernate.Session#isReadOnly(Object)
* *
* @return true, loaded entities/proxies will be made read-only by default; * @return true, loaded entities/proxies will be made read-only by default;
* false, loaded entities/proxies will be made modifiable by default. * false, loaded entities/proxies will be made modifiable by default.
* *
* @see org.hibernate.Session#isDefaultReadOnly() * @see org.hibernate.Session#isDefaultReadOnly()
* @see org.hibernate.Session#isReadOnly(Object)
*/ */
boolean isDefaultReadOnly(); boolean isDefaultReadOnly();
@ -692,28 +677,25 @@ public interface PersistenceContext {
* Change the default for entities and proxies loaded into this persistence * Change the default for entities and proxies loaded into this persistence
* context from modifiable to read-only mode, or from read-only mode to * context from modifiable to read-only mode, or from read-only mode to
* modifiable. * modifiable.
* * <p>
* Read-only entities are not dirty-checked and snapshots of persistent * Read-only entities are not dirty-checked and snapshots of persistent
* state are not maintained. Read-only entities can be modified, but * state are not maintained. Read-only entities can be modified, but
* changes are not persisted. * changes are not persisted.
* * <p>
* When a proxy is initialized, the loaded entity will have the same * When a proxy is initialized, the loaded entity will have the same
* read-only/modifiable setting as the uninitialized * read-only/modifiable setting as the uninitialized
* proxy has, regardless of the persistence context's current setting. * proxy has, regardless of the persistence context's current setting.
* * <p>
* To change the read-only/modifiable setting for a particular entity * To change the read-only/modifiable setting for a particular entity
* or proxy that is already in this session: * or proxy that is already in this session, call
+ * @see PersistenceContext#setReadOnly(Object,boolean) * {@link #setReadOnly(Object,boolean)}.
* @see org.hibernate.Session#setReadOnly(Object, boolean)
*
* To override this session's read-only/modifiable setting for entities
* and proxies loaded by a Query:
* @see Query#setReadOnly(boolean)
* *
* @param readOnly true, the default for loaded entities/proxies is read-only; * @param readOnly true, the default for loaded entities/proxies is read-only;
* false, the default for loaded entities/proxies is modifiable * false, the default for loaded entities/proxies is modifiable
* *
* @see org.hibernate.Session#setDefaultReadOnly(boolean) * @see org.hibernate.Session#setDefaultReadOnly(boolean)
* @see org.hibernate.query.Query#setReadOnly(boolean)
* @see org.hibernate.Session#isReadOnly(Object)
*/ */
void setDefaultReadOnly(boolean readOnly); void setDefaultReadOnly(boolean readOnly);
@ -733,25 +715,25 @@ public interface PersistenceContext {
/** /**
* Set an unmodified persistent object to read-only mode, or a read-only * Set an unmodified persistent object to read-only mode, or a read-only
* object to modifiable mode. * object to modifiable mode.
* * <p>
* Read-only entities are not dirty-checked and snapshots of persistent * Read-only entities are not dirty-checked and snapshots of persistent
* state are not maintained. Read-only entities can be modified, but * state are not maintained. Read-only entities can be modified, but
* changes are not persisted. * changes are not persisted.
* * <p>
* When a proxy is initialized, the loaded entity will have the same * When a proxy is initialized, the loaded entity will have the same
* read-only/modifiable setting as the uninitialized * read-only/modifiable setting as the uninitialized
* proxy has, regardless of the session's current setting. * proxy has, regardless of the session's current setting.
* * <p>
* If the entity or proxy already has the specified read-only/modifiable * If the entity or proxy already has the specified read-only/modifiable
* setting, then this method does nothing. * setting, then this method does nothing.
* *
* @param entityOrProxy an entity or proxy * @param entityOrProxy an entity or proxy
* @param readOnly if {@code true}, the entity or proxy is made read-only; otherwise, the entity or proxy is made * @param readOnly if {@code true}, the entity or proxy is made read-only;
* modifiable. * otherwise, the entity or proxy is made modifiable.
* *
* @see org.hibernate.Session#setDefaultReadOnly * @see org.hibernate.Session#setDefaultReadOnly
* @see org.hibernate.Session#setReadOnly * @see org.hibernate.Session#setReadOnly
* @see Query#setReadOnly * @see org.hibernate.query.Query#setReadOnly
*/ */
void setReadOnly(Object entityOrProxy, boolean readOnly); void setReadOnly(Object entityOrProxy, boolean readOnly);

View File

@ -15,8 +15,6 @@ import org.hibernate.cache.spi.access.EntityDataAccess;
import org.hibernate.cache.spi.entry.CacheEntry; import org.hibernate.cache.spi.entry.CacheEntry;
import org.hibernate.cache.spi.entry.ReferenceCacheEntryImpl; import org.hibernate.cache.spi.entry.ReferenceCacheEntryImpl;
import org.hibernate.cache.spi.entry.StandardCacheEntryImpl; import org.hibernate.cache.spi.entry.StandardCacheEntryImpl;
import org.hibernate.engine.internal.CacheHelper;
import org.hibernate.engine.internal.StatefulPersistenceContext;
import org.hibernate.engine.internal.TwoPhaseLoad; import org.hibernate.engine.internal.TwoPhaseLoad;
import org.hibernate.engine.spi.EntityEntry; import org.hibernate.engine.spi.EntityEntry;
import org.hibernate.engine.spi.EntityHolder; import org.hibernate.engine.spi.EntityHolder;
@ -35,18 +33,19 @@ import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.persister.entity.EntityPersister; import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.pretty.MessageHelper; import org.hibernate.pretty.MessageHelper;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.sql.results.LoadingLogger; import org.hibernate.sql.results.LoadingLogger;
import org.hibernate.stat.internal.StatsHelper; import org.hibernate.stat.internal.StatsHelper;
import org.hibernate.stat.spi.StatisticsImplementor; import org.hibernate.stat.spi.StatisticsImplementor;
import org.hibernate.type.Type; import org.hibernate.type.Type;
import org.hibernate.type.TypeHelper; import org.hibernate.type.TypeHelper;
import static org.hibernate.engine.internal.CacheHelper.fromSharedCache;
import static org.hibernate.engine.internal.ManagedTypeHelper.asPersistentAttributeInterceptable; import static org.hibernate.engine.internal.ManagedTypeHelper.asPersistentAttributeInterceptable;
import static org.hibernate.engine.internal.ManagedTypeHelper.isManagedEntity; import static org.hibernate.engine.internal.ManagedTypeHelper.isManagedEntity;
import static org.hibernate.engine.internal.ManagedTypeHelper.isPersistentAttributeInterceptable; import static org.hibernate.engine.internal.ManagedTypeHelper.isPersistentAttributeInterceptable;
import static org.hibernate.engine.internal.Versioning.getVersion; import static org.hibernate.engine.internal.Versioning.getVersion;
import static org.hibernate.loader.ast.internal.LoaderHelper.upgradeLock; import static org.hibernate.loader.ast.internal.LoaderHelper.upgradeLock;
import static org.hibernate.proxy.HibernateProxy.extractLazyInitializer;
/** /**
* @author Vlad Mihalcea * @author Vlad Mihalcea
@ -73,10 +72,9 @@ public class CacheEntityLoaderHelper {
LockOptions lockOptions, LockOptions lockOptions,
EventSource session) { EventSource session) {
final Object old = session.getEntityUsingInterceptor( keyToLoad ); final Object old = session.getEntityUsingInterceptor( keyToLoad );
if ( old != null ) { if ( old != null ) {
// this object was already loaded // this object was already loaded
EntityEntry oldEntry = session.getPersistenceContext().getEntry( old ); final EntityEntry oldEntry = session.getPersistenceContext().getEntry( old );
if ( options.isCheckDeleted() ) { if ( options.isCheckDeleted() ) {
if ( oldEntry.getStatus().isDeletedOrGone() ) { if ( oldEntry.getStatus().isDeletedOrGone() ) {
LoadingLogger.LOGGER.debug( LoadingLogger.LOGGER.debug(
@ -85,9 +83,8 @@ public class CacheEntityLoaderHelper {
} }
} }
if ( options.isAllowNulls() ) { if ( options.isAllowNulls() ) {
final EntityPersister persister = session.getFactory() final EntityPersister persister =
.getRuntimeMetamodels() session.getFactory().getMappingMetamodel()
.getMappingMetamodel()
.getEntityDescriptor( keyToLoad.getEntityName() ); .getEntityDescriptor( keyToLoad.getEntityName() );
if ( ! persister.isInstance( old ) ) { if ( ! persister.isInstance( old ) ) {
LOG.debugf( LOG.debugf(
@ -98,10 +95,8 @@ public class CacheEntityLoaderHelper {
return new PersistenceContextEntry( old, EntityStatus.INCONSISTENT_RTN_CLASS_MARKER ); return new PersistenceContextEntry( old, EntityStatus.INCONSISTENT_RTN_CLASS_MARKER );
} }
} }
upgradeLock( old, oldEntry, lockOptions, session ); upgradeLock( old, oldEntry, lockOptions, session );
} }
return new PersistenceContextEntry( old, EntityStatus.MANAGED ); return new PersistenceContextEntry( old, EntityStatus.MANAGED );
} }
@ -154,13 +149,11 @@ public class CacheEntityLoaderHelper {
final LoadEvent event, final LoadEvent event,
final EntityKey keyToLoad, final EntityKey keyToLoad,
final LoadEventListener.LoadType options) throws HibernateException { final LoadEventListener.LoadType options) throws HibernateException {
final SessionImplementor session = event.getSession();
SessionImplementor session = event.getSession(); final Object old = session.getEntityUsingInterceptor( keyToLoad );
Object old = session.getEntityUsingInterceptor( keyToLoad );
if ( old != null ) { if ( old != null ) {
// this object was already loaded // this object was already loaded
EntityEntry oldEntry = session.getPersistenceContext().getEntry( old ); final EntityEntry oldEntry = session.getPersistenceContext().getEntry( old );
if ( options.isCheckDeleted() ) { if ( options.isCheckDeleted() ) {
if ( oldEntry.getStatus().isDeletedOrGone() ) { if ( oldEntry.getStatus().isDeletedOrGone() ) {
LoadingLogger.LOGGER.debug( LoadingLogger.LOGGER.debug(
@ -169,9 +162,8 @@ public class CacheEntityLoaderHelper {
} }
} }
if ( options.isAllowNulls() ) { if ( options.isAllowNulls() ) {
final EntityPersister persister = event.getFactory() final EntityPersister persister =
.getRuntimeMetamodels() event.getFactory().getMappingMetamodel()
.getMappingMetamodel()
.getEntityDescriptor( keyToLoad.getEntityName() ); .getEntityDescriptor( keyToLoad.getEntityName() );
if ( !persister.isInstance( old ) ) { if ( !persister.isInstance( old ) ) {
LOG.debug( LOG.debug(
@ -182,7 +174,6 @@ public class CacheEntityLoaderHelper {
} }
upgradeLock( old, oldEntry, event.getLockOptions(), event.getSession() ); upgradeLock( old, oldEntry, event.getLockOptions(), event.getSession() );
} }
return new PersistenceContextEntry( old, EntityStatus.MANAGED ); return new PersistenceContextEntry( old, EntityStatus.MANAGED );
} }
@ -206,14 +197,13 @@ public class CacheEntityLoaderHelper {
persister, persister,
entityKey entityKey
); );
if ( entity != null ) { if ( entity != null ) {
//PostLoad is needed for EJB3 //PostLoad is needed for EJB3
final PostLoadEvent postLoadEvent = event.getPostLoadEvent() final PostLoadEvent postLoadEvent =
event.getPostLoadEvent()
.setEntity( entity ) .setEntity( entity )
.setId( event.getEntityId() ) .setId( event.getEntityId() )
.setPersister( persister ); .setPersister( persister );
event.getFactory() event.getFactory()
.getFastSessionServices() .getFastSessionServices()
.firePostLoadEvent( postLoadEvent ); .firePostLoadEvent( postLoadEvent );
@ -238,24 +228,19 @@ public class CacheEntityLoaderHelper {
final LockMode lockMode, final LockMode lockMode,
final EntityPersister persister, final EntityPersister persister,
final EntityKey entityKey) { final EntityKey entityKey) {
final boolean useCache =
final boolean useCache = persister.canReadFromCache() persister.canReadFromCache()
&& source.getCacheMode().isGetEnabled() && source.getCacheMode().isGetEnabled()
&& lockMode.lessThan( LockMode.READ ); && lockMode.lessThan( LockMode.READ );
if ( !useCache ) { if ( !useCache ) {
// we can't use cache here // we can't use cache here
return null; return null;
} }
else {
final Object ce = getFromSharedCache( entityKey.getIdentifier(), persister, source ); final Object ce = getFromSharedCache( entityKey.getIdentifier(), persister, source );
if ( ce == null ) {
// nothing was found in cache // nothing was found in cache
return null; return ce == null ? null : processCachedEntry( entity, persister, ce, source, entityKey );
} }
return processCachedEntry( entity, persister, ce, source, entityKey );
} }
@ -265,14 +250,13 @@ public class CacheEntityLoaderHelper {
SessionImplementor source) { SessionImplementor source) {
final EntityDataAccess cache = persister.getCacheAccessStrategy(); final EntityDataAccess cache = persister.getCacheAccessStrategy();
final SessionFactoryImplementor factory = source.getFactory(); final SessionFactoryImplementor factory = source.getFactory();
final Object ck = cache.generateCacheKey( final Object cacheKey = cache.generateCacheKey(
entityId, entityId,
persister, persister,
factory, factory,
source.getTenantIdentifier() source.getTenantIdentifier()
); );
final Object ce = fromSharedCache( source, cacheKey, persister, persister.getCacheAccessStrategy() );
final Object ce = CacheHelper.fromSharedCache( source, ck, persister, persister.getCacheAccessStrategy() );
final StatisticsImplementor statistics = factory.getStatistics(); final StatisticsImplementor statistics = factory.getStatistics();
if ( statistics.isStatisticsEnabled() ) { if ( statistics.isStatisticsEnabled() ) {
if ( ce == null ) { if ( ce == null ) {
@ -297,8 +281,8 @@ public class CacheEntityLoaderHelper {
final Object ce, final Object ce,
final EventSource source, final EventSource source,
final EntityKey entityKey) { final EntityKey entityKey) {
final CacheEntry entry = (CacheEntry)
CacheEntry entry = (CacheEntry) persister.getCacheEntryStructure().destructure( ce, source.getFactory() ); persister.getCacheEntryStructure().destructure( ce, source.getFactory() );
if ( entry.isReferenceEntry() ) { if ( entry.isReferenceEntry() ) {
if ( instanceToLoad != null ) { if ( instanceToLoad != null ) {
throw new HibernateException( throw new HibernateException(
@ -314,8 +298,15 @@ public class CacheEntityLoaderHelper {
} }
} }
else { else {
Object entity = convertCacheEntryToEntity( entry, entityKey.getIdentifier(), source, persister, instanceToLoad, entityKey ); final Object entity =
convertCacheEntryToEntity(
entry,
entityKey.getIdentifier(),
source,
persister,
instanceToLoad,
entityKey
);
if ( !persister.isInstance( entity ) ) { if ( !persister.isInstance( entity ) ) {
// Cleanup the inconsistent return class entity from the persistence context // Cleanup the inconsistent return class entity from the persistence context
final PersistenceContext persistenceContext = source.getPersistenceContext(); final PersistenceContext persistenceContext = source.getPersistenceContext();
@ -323,7 +314,6 @@ public class CacheEntityLoaderHelper {
persistenceContext.removeEntity( entityKey ); persistenceContext.removeEntity( entityKey );
return null; return null;
} }
return entity; return entity;
} }
} }
@ -333,12 +323,8 @@ public class CacheEntityLoaderHelper {
SharedSessionContractImplementor session, SharedSessionContractImplementor session,
EntityKey entityKey) { EntityKey entityKey) {
final Object entity = referenceCacheEntry.getReference(); final Object entity = referenceCacheEntry.getReference();
if ( entity == null ) { if ( entity == null ) {
throw new IllegalStateException( throw new IllegalStateException( "Reference cache entry contained null : " + referenceCacheEntry );
"Reference cache entry contained null : "
+ referenceCacheEntry.toString()
);
} }
else { else {
makeEntityCircularReferenceSafe( referenceCacheEntry, session, entity, entityKey ); makeEntityCircularReferenceSafe( referenceCacheEntry, session, entity, entityKey );
@ -351,19 +337,13 @@ public class CacheEntityLoaderHelper {
SharedSessionContractImplementor session, SharedSessionContractImplementor session,
Object entity, Object entity,
EntityKey entityKey) { EntityKey entityKey) {
// make it circular-reference safe // make it circular-reference safe
final StatefulPersistenceContext statefulPersistenceContext = (StatefulPersistenceContext) session.getPersistenceContext(); final PersistenceContext persistenceContext = session.getPersistenceContext();
if ( isManagedEntity( entity ) ) {
if ( ( isManagedEntity( entity ) ) ) { final EntityHolder entityHolder =
final EntityHolder entityHolder = statefulPersistenceContext.addEntityHolder( persistenceContext.addEntityHolder( entityKey, entity );
entityKey, final EntityEntry entityEntry =
entity persistenceContext.addReferenceEntry( entity, Status.READ_ONLY );
);
final EntityEntry entityEntry = statefulPersistenceContext.addReferenceEntry(
entity,
Status.READ_ONLY
);
entityHolder.setEntityEntry( entityEntry ); entityHolder.setEntityEntry( entityEntry );
} }
else { else {
@ -376,7 +356,7 @@ public class CacheEntityLoaderHelper {
session session
); );
} }
statefulPersistenceContext.initializeNonLazyCollections(); persistenceContext.initializeNonLazyCollections();
} }
private Object convertCacheEntryToEntity( private Object convertCacheEntryToEntity(
@ -386,9 +366,7 @@ public class CacheEntityLoaderHelper {
EntityPersister persister, EntityPersister persister,
Object instanceToLoad, Object instanceToLoad,
EntityKey entityKey) { EntityKey entityKey) {
final SessionFactoryImplementor factory = source.getFactory(); final SessionFactoryImplementor factory = source.getFactory();
final EntityPersister subclassPersister;
if ( LOG.isTraceEnabled() ) { if ( LOG.isTraceEnabled() ) {
LOG.tracef( LOG.tracef(
@ -398,11 +376,11 @@ public class CacheEntityLoaderHelper {
); );
} }
final Object entity; final EntityPersister subclassPersister =
factory.getRuntimeMetamodels().getMappingMetamodel()
subclassPersister = factory.getRuntimeMetamodels()
.getMappingMetamodel()
.getEntityDescriptor( entry.getSubclass() ); .getEntityDescriptor( entry.getSubclass() );
final Object entity;
if ( instanceToLoad != null ) { if ( instanceToLoad != null ) {
entity = instanceToLoad; entity = instanceToLoad;
} }
@ -418,14 +396,15 @@ public class CacheEntityLoaderHelper {
} }
if ( isPersistentAttributeInterceptable( entity ) ) { if ( isPersistentAttributeInterceptable( entity ) ) {
PersistentAttributeInterceptor persistentAttributeInterceptor = asPersistentAttributeInterceptable( entity ).$$_hibernate_getInterceptor(); PersistentAttributeInterceptor persistentAttributeInterceptor =
// if we do this after the entity has been initialized the BytecodeLazyAttributeInterceptor#isAttributeLoaded(String fieldName) would return false; asPersistentAttributeInterceptable( entity ).$$_hibernate_getInterceptor();
if ( persistentAttributeInterceptor == null || persistentAttributeInterceptor instanceof EnhancementAsProxyLazinessInterceptor ) { // if we do this after the entity has been initialized the
persister.getBytecodeEnhancementMetadata().injectInterceptor( // BytecodeLazyAttributeInterceptor#isAttributeLoaded(String fieldName)
entity, // would return false
entityId, if ( persistentAttributeInterceptor == null
source || persistentAttributeInterceptor instanceof EnhancementAsProxyLazinessInterceptor ) {
); persister.getBytecodeEnhancementMetadata()
.injectInterceptor( entity, entityId, source );
} }
} }
@ -439,21 +418,17 @@ public class CacheEntityLoaderHelper {
source source
); );
final PersistenceContext persistenceContext = source.getPersistenceContext();
final Object[] values;
final Object version;
final boolean isReadOnly;
final Type[] types = subclassPersister.getPropertyTypes(); final Type[] types = subclassPersister.getPropertyTypes();
// initializes the entity by (desired) side-effect // initializes the entity by (desired) side effect
values = ( (StandardCacheEntryImpl) entry ).assemble( final StandardCacheEntryImpl standardCacheEntry = (StandardCacheEntryImpl) entry;
final Object[] values = standardCacheEntry.assemble(
entity, entity,
entityId, entityId,
subclassPersister, subclassPersister,
source.getInterceptor(), source.getInterceptor(),
source source
); );
if ( ( (StandardCacheEntryImpl) entry ).isDeepCopyNeeded() ) { if ( standardCacheEntry.isDeepCopyNeeded() ) {
TypeHelper.deepCopy( TypeHelper.deepCopy(
values, values,
types, types,
@ -462,22 +437,25 @@ public class CacheEntityLoaderHelper {
source source
); );
} }
version = getVersion( values, subclassPersister ); final Object version = getVersion( values, subclassPersister );
LOG.tracef( "Cached Version : %s", version ); LOG.tracef( "Cached Version : %s", version );
final PersistenceContext persistenceContext = source.getPersistenceContext();
final Object proxy = persistenceContext.getProxy( entityKey ); final Object proxy = persistenceContext.getProxy( entityKey );
final boolean isReadOnly;
if ( proxy != null ) { if ( proxy != null ) {
// there is already a proxy for this impl // there is already a proxy for this impl
// only set the status to read-only if the proxy is read-only // only set the status to read-only if the proxy is read-only
isReadOnly = HibernateProxy.extractLazyInitializer( proxy ).isReadOnly(); isReadOnly = extractLazyInitializer( proxy ).isReadOnly();
} }
else { else {
isReadOnly = source.isDefaultReadOnly(); isReadOnly = source.isDefaultReadOnly();
} }
EntityEntry entityEntry = persistenceContext.addEntry( final EntityEntry entityEntry = persistenceContext.addEntry(
entity, entity,
( isReadOnly ? Status.READ_ONLY : Status.MANAGED ), isReadOnly ? Status.READ_ONLY : Status.MANAGED,
values, values,
null, null,
entityId, entityId,