From 04eba997e24597bdd731bb4fd95e94cc592b42ef Mon Sep 17 00:00:00 2001 From: Gail Badner Date: Wed, 16 Oct 2019 11:19:13 -0700 Subject: [PATCH] HHH-13634 : PersistenceContext can get cleared before load completes using StatelessSessionImpl (cherry picked from commit 42de569a37f54fc3eec39fed3cb03ad6e763ddc1) --- .../internal/StatelessSessionImpl.java | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/internal/StatelessSessionImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/StatelessSessionImpl.java index b75662e44e..3cd3e7e332 100755 --- a/hibernate-core/src/main/java/org/hibernate/internal/StatelessSessionImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/StatelessSessionImpl.java @@ -283,11 +283,12 @@ public class StatelessSessionImpl extends AbstractSharedSessionContract implemen boolean nullable) throws HibernateException { checkOpen(); - EntityPersister persister = getFactory().getMetamodel().entityPersister( entityName ); + final EntityPersister persister = getFactory().getMetamodel().entityPersister( entityName ); final EntityKey entityKey = generateEntityKey( id, persister ); // first, try to load it from the temp PC associated to this SS - Object loaded = temporaryPersistenceContext.getEntity( entityKey ); + final PersistenceContext persistenceContext = getPersistenceContext(); + Object loaded = persistenceContext.getEntity( entityKey ); if ( loaded != null ) { // we found it in the temp PC. Should indicate we are in the midst of processing a result set // containing eager fetches via join fetch @@ -307,7 +308,6 @@ public class StatelessSessionImpl extends AbstractSharedSessionContract implemen // if the entity defines a HibernateProxy factory, see if there is an // existing proxy associated with the PC - and if so, use it if ( persister.getEntityMetamodel().getTuplizer().getProxyFactory() != null ) { - final PersistenceContext persistenceContext = getPersistenceContext(); final Object proxy = persistenceContext.getProxy( entityKey ); if ( proxy != null ) { @@ -338,9 +338,9 @@ public class StatelessSessionImpl extends AbstractSharedSessionContract implemen } else { if ( persister.hasProxy() ) { - final Object existingProxy = getPersistenceContext().getProxy( entityKey ); + final Object existingProxy = persistenceContext.getProxy( entityKey ); if ( existingProxy != null ) { - return getPersistenceContext().narrowProxy( existingProxy, persister, entityKey, null ); + return persistenceContext.narrowProxy( existingProxy, persister, entityKey, null ); } else { return createProxy( entityKey ); @@ -350,7 +350,16 @@ public class StatelessSessionImpl extends AbstractSharedSessionContract implemen } // otherwise immediately materialize it - return get( entityName, id ); + + // IMPLEMENTATION NOTE: increment/decrement the load count before/after getting the value + // to ensure that #get does not clear the PersistenceContext. + persistenceContext.beforeLoad(); + try { + return get( entityName, id ); + } + finally { + persistenceContext.afterLoad(); + } } private Object createProxy(EntityKey entityKey) {