HHH-11147 micro optimisation: avoid looking up the same object multiple times

This commit is contained in:
Sanne Grinovero 2019-06-27 15:28:15 +01:00
parent 272dbefac5
commit d968b0a3a5
2 changed files with 11 additions and 6 deletions

View File

@ -24,6 +24,7 @@ import org.hibernate.ScrollMode;
import org.hibernate.SessionException;
import org.hibernate.StatelessSession;
import org.hibernate.UnresolvableObjectException;
import org.hibernate.bytecode.spi.BytecodeEnhancementMetadata;
import org.hibernate.cache.spi.access.EntityDataAccess;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.internal.StatefulPersistenceContext;
@ -46,6 +47,7 @@ import org.hibernate.persister.entity.OuterJoinLoadable;
import org.hibernate.pretty.MessageHelper;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.query.spi.ScrollableResultsImplementor;
import org.hibernate.tuple.entity.EntityMetamodel;
/**
* @author Gavin King
@ -298,12 +300,13 @@ public class StatelessSessionImpl extends AbstractSharedSessionContract implemen
// first, check to see if we can use "bytecode proxies"
if ( allowBytecodeProxy
&& persister.getEntityMetamodel().getBytecodeEnhancementMetadata().isEnhancedForLazyLoading() ) {
final EntityMetamodel entityMetamodel = persister.getEntityMetamodel();
final BytecodeEnhancementMetadata bytecodeEnhancementMetadata = entityMetamodel.getBytecodeEnhancementMetadata();
if ( allowBytecodeProxy && bytecodeEnhancementMetadata.isEnhancedForLazyLoading() ) {
// we cannot use bytecode proxy for entities with subclasses
if ( !persister.getEntityMetamodel().hasSubclasses() ) {
return persister.getBytecodeEnhancementMetadata().createEnhancedProxy( entityKey, false, this );
if ( !entityMetamodel.hasSubclasses() ) {
return bytecodeEnhancementMetadata.createEnhancedProxy( entityKey, false, this );
}
}

View File

@ -18,6 +18,7 @@ import org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributesMetadata;
import org.hibernate.bytecode.spi.BytecodeEnhancementMetadata;
import org.hibernate.bytecode.spi.NotInstrumentedException;
import org.hibernate.engine.spi.EntityKey;
import org.hibernate.engine.spi.PersistenceContext;
import org.hibernate.engine.spi.PersistentAttributeInterceptable;
import org.hibernate.engine.spi.PersistentAttributeInterceptor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
@ -138,16 +139,17 @@ public class BytecodeEnhancementMetadataPojoImpl implements BytecodeEnhancementM
public PersistentAttributeInterceptable createEnhancedProxy(EntityKey entityKey, boolean addEmptyEntry, SharedSessionContractImplementor session) {
final EntityPersister persister = entityKey.getPersister();
final Serializable identifier = entityKey.getIdentifier();
final PersistenceContext persistenceContext = session.getPersistenceContext();
// first, instantiate the entity instance to use as the proxy
final PersistentAttributeInterceptable entity = (PersistentAttributeInterceptable) persister.getEntityTuplizer().instantiate( identifier, session );
// add the entity (proxy) instance to the PC
session.getPersistenceContext().addEnhancedProxy( entityKey, entity );
persistenceContext.addEnhancedProxy( entityKey, entity );
// if requested, add the "holder entry" to the PC
if ( addEmptyEntry ) {
session.getPersistenceContext().addEntry(
persistenceContext.addEntry(
entity,
Status.MANAGED,
// loaded state