minor code changes to SessionImpl and supertype

This commit is contained in:
Gavin 2023-04-12 16:07:33 +02:00 committed by Gavin King
parent e5c9b2a865
commit 8415ffefda
2 changed files with 33 additions and 40 deletions

View File

@ -708,7 +708,7 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
try { try {
final HqlInterpretation interpretation = interpretHql( hql, resultType ); final HqlInterpretation interpretation = interpretHql( hql, resultType );
checkSelectionQuery( hql, interpretation ); checkSelectionQuery( hql, interpretation );
return createSelectionQuery( hql, resultType, interpretation); return createSelectionQuery( hql, resultType, interpretation );
} }
catch ( RuntimeException e ) { catch ( RuntimeException e ) {
markForRollbackOnly(); markForRollbackOnly();
@ -778,7 +778,6 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
final QuerySqmImpl<T> query = new QuerySqmImpl<>( queryString, interpretation, expectedResultType, this ); final QuerySqmImpl<T> query = new QuerySqmImpl<>( queryString, interpretation, expectedResultType, this );
applyQuerySettingsAndHints( query ); applyQuerySettingsAndHints( query );
query.setComment( queryString ); query.setComment( queryString );
return query; return query;
} }
catch (RuntimeException e) { catch (RuntimeException e) {
@ -827,12 +826,12 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
} }
} }
private NamedResultSetMappingMemento getResultSetMappingMemento(String resultSetMappingName) { protected NamedResultSetMappingMemento getResultSetMappingMemento(String resultSetMappingName) {
final NamedResultSetMappingMemento resultSetMappingMemento = getFactory().getQueryEngine() final NamedResultSetMappingMemento resultSetMappingMemento = getFactory().getQueryEngine()
.getNamedObjectRepository().getResultSetMappingMemento( resultSetMappingName ); .getNamedObjectRepository().getResultSetMappingMemento( resultSetMappingName );
if ( resultSetMappingMemento == null ) { if ( resultSetMappingMemento == null ) {
throw new HibernateException( "Could not resolve specified result-set mapping name : " throw new HibernateException( "Could not resolve specified result-set mapping name: "
+ resultSetMappingName); + resultSetMappingName );
} }
return resultSetMappingMemento; return resultSetMappingMemento;
} }
@ -860,11 +859,11 @@ public abstract class AbstractSharedSessionContract implements SharedSessionCont
final NativeQueryImplementor<T> query = createNativeQuery( sqlString ); final NativeQueryImplementor<T> query = createNativeQuery( sqlString );
if ( getFactory().getMappingMetamodel().isEntityClass( resultClass ) ) { if ( getFactory().getMappingMetamodel().isEntityClass( resultClass ) ) {
query.addEntity( tableAlias, resultClass.getName(), LockMode.READ ); query.addEntity( tableAlias, resultClass.getName(), LockMode.READ );
return query;
} }
else { else {
throw new UnknownEntityTypeException( "unable to locate persister: " + resultClass.getName() ); throw new UnknownEntityTypeException( "unable to locate persister: " + resultClass.getName() );
} }
return query;
} }
@Override @Override

View File

@ -160,6 +160,10 @@ import static org.hibernate.cfg.AvailableSettings.JPA_LOCK_SCOPE;
import static org.hibernate.cfg.AvailableSettings.JPA_LOCK_TIMEOUT; import static org.hibernate.cfg.AvailableSettings.JPA_LOCK_TIMEOUT;
import static org.hibernate.cfg.AvailableSettings.JPA_SHARED_CACHE_RETRIEVE_MODE; import static org.hibernate.cfg.AvailableSettings.JPA_SHARED_CACHE_RETRIEVE_MODE;
import static org.hibernate.cfg.AvailableSettings.JPA_SHARED_CACHE_STORE_MODE; import static org.hibernate.cfg.AvailableSettings.JPA_SHARED_CACHE_STORE_MODE;
import static org.hibernate.event.spi.LoadEventListener.IMMEDIATE_LOAD;
import static org.hibernate.event.spi.LoadEventListener.INTERNAL_LOAD_EAGER;
import static org.hibernate.event.spi.LoadEventListener.INTERNAL_LOAD_LAZY;
import static org.hibernate.event.spi.LoadEventListener.INTERNAL_LOAD_NULLABLE;
import static org.hibernate.jpa.HibernateHints.HINT_READ_ONLY; import static org.hibernate.jpa.HibernateHints.HINT_READ_ONLY;
import static org.hibernate.jpa.HibernateHints.HINT_FLUSH_MODE; import static org.hibernate.jpa.HibernateHints.HINT_FLUSH_MODE;
import static org.hibernate.jpa.LegacySpecHints.HINT_JAVAEE_LOCK_TIMEOUT; import static org.hibernate.jpa.LegacySpecHints.HINT_JAVAEE_LOCK_TIMEOUT;
@ -1032,48 +1036,41 @@ public class SessionImpl
@Override @Override
public Object immediateLoad(String entityName, Object id) throws HibernateException { public Object immediateLoad(String entityName, Object id) throws HibernateException {
if ( log.isDebugEnabled() ) { if ( log.isDebugEnabled() ) {
final EntityPersister persister = getFactory().getRuntimeMetamodels() final EntityPersister persister = getFactory().getMappingMetamodel()
.getMappingMetamodel()
.getEntityDescriptor( entityName ); .getEntityDescriptor( entityName );
log.debugf( "Initializing proxy: %s", MessageHelper.infoString( persister, id, getFactory() ) ); log.debugf( "Initializing proxy: %s", MessageHelper.infoString( persister, id, getFactory() ) );
} }
LoadEvent event = loadEvent; LoadEvent event = loadEvent;
loadEvent = null; loadEvent = null;
event = recycleEventInstance( event, id, entityName ); event = recycleEventInstance( event, id, entityName );
fireLoadNoChecks( event, LoadEventListener.IMMEDIATE_LOAD ); fireLoadNoChecks( event, IMMEDIATE_LOAD );
Object result = event.getResult(); final Object result = event.getResult();
finishWithEventInstance( event ); finishWithEventInstance( event );
final LazyInitializer lazyInitializer = extractLazyInitializer( result ); final LazyInitializer lazyInitializer = extractLazyInitializer( result );
if ( lazyInitializer != null ) { return lazyInitializer != null ? lazyInitializer.getImplementation() : result;
return lazyInitializer.getImplementation();
}
return result;
} }
@Override @Override
public Object internalLoad( public Object internalLoad(String entityName, Object id, boolean eager, boolean nullable) {
String entityName,
Object id,
boolean eager,
boolean nullable) {
final LoadType type = internalLoadType( eager, nullable ); final LoadType type = internalLoadType( eager, nullable );
final EffectiveEntityGraph effectiveEntityGraph = loadQueryInfluencers.getEffectiveEntityGraph(); final EffectiveEntityGraph effectiveEntityGraph = loadQueryInfluencers.getEffectiveEntityGraph();
final GraphSemantic semantic = effectiveEntityGraph.getSemantic(); final GraphSemantic semantic = effectiveEntityGraph.getSemantic();
final RootGraphImplementor<?> graph = effectiveEntityGraph.getGraph(); final RootGraphImplementor<?> graph = effectiveEntityGraph.getGraph();
boolean clearedEffectiveGraph = false; boolean clearedEffectiveGraph;
if ( semantic != null ) { if ( semantic == null || graph.appliesTo( entityName ) ) {
if ( ! graph.appliesTo( entityName ) ) { clearedEffectiveGraph = false;
log.debug( "Clearing effective entity graph for subsequent-select" ); }
else {
log.debug("Clearing effective entity graph for subsequent-select");
clearedEffectiveGraph = true; clearedEffectiveGraph = true;
effectiveEntityGraph.clear(); effectiveEntityGraph.clear();
} }
}
try { try {
LoadEvent event = loadEvent; LoadEvent event = loadEvent;
loadEvent = null; loadEvent = null;
event = recycleEventInstance( event, id, entityName ); event = recycleEventInstance( event, id, entityName );
fireLoadNoChecks( event, type ); fireLoadNoChecks( event, type );
Object result = event.getResult(); final Object result = event.getResult();
if ( !nullable ) { if ( !nullable ) {
UnresolvableObjectException.throwIfNull( result, id, entityName ); UnresolvableObjectException.throwIfNull( result, id, entityName );
} }
@ -1087,14 +1084,12 @@ public class SessionImpl
} }
} }
private static LoadType internalLoadType(boolean eager, boolean nullable) { protected static LoadType internalLoadType(boolean eager, boolean nullable) {
if ( nullable ) { if ( nullable ) {
return LoadEventListener.INTERNAL_LOAD_NULLABLE; return INTERNAL_LOAD_NULLABLE;
} }
else { else {
return eager return eager ? INTERNAL_LOAD_EAGER : INTERNAL_LOAD_LAZY;
? LoadEventListener.INTERNAL_LOAD_EAGER
: LoadEventListener.INTERNAL_LOAD_LAZY;
} }
} }
@ -1615,12 +1610,11 @@ public class SessionImpl
} }
try { try {
final LazyInitializer li = extractLazyInitializer( object ); final LazyInitializer lazyInitializer = extractLazyInitializer( object );
if ( li == null && persistenceContext.getEntry( object ) == null ) { if ( lazyInitializer == null && persistenceContext.getEntry( object ) == null ) {
// check if it is an entity -> if not throw an exception (per JPA) // check if it is an entity -> if not throw an exception (per JPA)
try { try {
getFactory().getRuntimeMetamodels() getFactory().getMappingMetamodel()
.getMappingMetamodel()
.getEntityDescriptor( entityName ); .getEntityDescriptor( entityName );
} }
catch (HibernateException e) { catch (HibernateException e) {
@ -1628,22 +1622,22 @@ public class SessionImpl
} }
} }
if ( li != null ) { if ( lazyInitializer != null ) {
//do not use proxiesByKey, since not all //do not use proxiesByKey, since not all
//proxies that point to this session's //proxies that point to this session's
//instances are in that collection! //instances are in that collection!
if ( li.isUninitialized() ) { if ( lazyInitializer.isUninitialized() ) {
//if it is an uninitialized proxy, pointing //if it is an uninitialized proxy, pointing
//with this session, then when it is accessed, //with this session, then when it is accessed,
//the underlying instance will be "contained" //the underlying instance will be "contained"
return li.getSession() == this; return lazyInitializer.getSession() == this;
} }
else { else {
//if it is initialized, see if the underlying //if it is initialized, see if the underlying
//instance is contained, since we need to //instance is contained, since we need to
//account for the fact that it might have been //account for the fact that it might have been
//evicted //evicted
object = li.getImplementation(); object = lazyInitializer.getImplementation();
} }
} }
// A session is considered to contain an entity only if the entity has // A session is considered to contain an entity only if the entity has