From ddb3a8cd184d4ed3a1eda846b2a10b4576571298 Mon Sep 17 00:00:00 2001 From: Sanne Grinovero Date: Mon, 28 Nov 2011 17:42:25 +0000 Subject: [PATCH] HHH-6858 reuse reference to PersistenceContext --- .../AbstractFlushingEventListener.java | 26 +++++++++---------- .../tuple/entity/AbstractEntityTuplizer.java | 9 ++++--- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/event/internal/AbstractFlushingEventListener.java b/hibernate-core/src/main/java/org/hibernate/event/internal/AbstractFlushingEventListener.java index 5d56bef640..e6a0669c80 100644 --- a/hibernate-core/src/main/java/org/hibernate/event/internal/AbstractFlushingEventListener.java +++ b/hibernate-core/src/main/java/org/hibernate/event/internal/AbstractFlushingEventListener.java @@ -87,19 +87,19 @@ public abstract class AbstractFlushingEventListener implements Serializable { final PersistenceContext persistenceContext = session.getPersistenceContext(); session.getInterceptor().preFlush( new LazyIterator( persistenceContext.getEntitiesByKey() ) ); - prepareEntityFlushes(session); + prepareEntityFlushes( session, persistenceContext ); // we could move this inside if we wanted to // tolerate collection initializations during // collection dirty checking: - prepareCollectionFlushes(session); + prepareCollectionFlushes( persistenceContext ); // now, any collections that are initialized // inside this block do not get updated - they // are ignored until the next flush persistenceContext.setFlushing(true); try { - flushEntities(event); - flushCollections(session); + flushEntities( event, persistenceContext ); + flushCollections( session, persistenceContext ); } finally { persistenceContext.setFlushing(false); @@ -140,11 +140,11 @@ public abstract class AbstractFlushingEventListener implements Serializable { * any newly referenced entity that must be passed to saveOrUpdate(), * and also apply orphan delete */ - private void prepareEntityFlushes(EventSource session) throws HibernateException { + private void prepareEntityFlushes(EventSource session, PersistenceContext persistenceContext) throws HibernateException { LOG.debug( "Processing flush-time cascades" ); - final Map.Entry[] list = IdentityMap.concurrentEntries( session.getPersistenceContext().getEntityEntries() ); + final Map.Entry[] list = IdentityMap.concurrentEntries( persistenceContext.getEntityEntries() ); //safe from concurrent modification because of how entryList() is implemented on IdentityMap final int size = list.length; final Object anything = getAnything(); @@ -180,14 +180,14 @@ public abstract class AbstractFlushingEventListener implements Serializable { * Initialize the flags of the CollectionEntry, including the * dirty check. */ - private void prepareCollectionFlushes(SessionImplementor session) throws HibernateException { + private void prepareCollectionFlushes(PersistenceContext persistenceContext) throws HibernateException { // Initialize dirty flags for arrays + collections with composite elements // and reset reached, doupdate, etc. LOG.debug( "Dirty checking collections" ); - final List list = IdentityMap.entries( session.getPersistenceContext().getCollectionEntries() ); + final List list = IdentityMap.entries( persistenceContext.getCollectionEntries() ); final int size = list.size(); for ( int i = 0; i < size; i++ ) { Map.Entry e = ( Map.Entry ) list.get( i ); @@ -200,7 +200,7 @@ public abstract class AbstractFlushingEventListener implements Serializable { * 2. schedule any entity updates * 3. search out any reachable collections */ - private void flushEntities(FlushEvent event) throws HibernateException { + private void flushEntities(final FlushEvent event, final PersistenceContext persistenceContext) throws HibernateException { LOG.trace( "Flushing entities and processing referenced collections" ); @@ -219,7 +219,7 @@ public abstract class AbstractFlushingEventListener implements Serializable { .getEventListenerGroup( EventType.FLUSH_ENTITY ) .listeners(); - final Map.Entry[] list = IdentityMap.concurrentEntries( source.getPersistenceContext().getEntityEntries() ); + final Map.Entry[] list = IdentityMap.concurrentEntries( persistenceContext.getEntityEntries() ); final int size = list.length; for ( int i = 0; i < size; i++ ) { @@ -244,11 +244,11 @@ public abstract class AbstractFlushingEventListener implements Serializable { * process any unreferenced collections and then inspect all known collections, * scheduling creates/removes/updates */ - private void flushCollections(EventSource session) throws HibernateException { + private void flushCollections(final EventSource session, final PersistenceContext persistenceContext) throws HibernateException { LOG.trace( "Processing unreferenced collections" ); - List list = IdentityMap.entries( session.getPersistenceContext().getCollectionEntries() ); + List list = IdentityMap.entries( persistenceContext.getCollectionEntries() ); int size = list.size(); for ( int i = 0; i < size; i++ ) { Map.Entry me = ( Map.Entry ) list.get( i ); @@ -262,7 +262,7 @@ public abstract class AbstractFlushingEventListener implements Serializable { LOG.trace( "Scheduling collection removes/(re)creates/updates" ); - list = IdentityMap.entries( session.getPersistenceContext().getCollectionEntries() ); + list = IdentityMap.entries( persistenceContext.getCollectionEntries() ); size = list.size(); ActionQueue actionQueue = session.getActionQueue(); for ( int i = 0; i < size; i++ ) { diff --git a/hibernate-core/src/main/java/org/hibernate/tuple/entity/AbstractEntityTuplizer.java b/hibernate-core/src/main/java/org/hibernate/tuple/entity/AbstractEntityTuplizer.java index 911abb3f1d..5ab5271115 100644 --- a/hibernate-core/src/main/java/org/hibernate/tuple/entity/AbstractEntityTuplizer.java +++ b/hibernate-core/src/main/java/org/hibernate/tuple/entity/AbstractEntityTuplizer.java @@ -36,6 +36,7 @@ import org.hibernate.MappingException; import org.hibernate.bytecode.instrumentation.spi.LazyPropertyInitializer; import org.hibernate.engine.spi.EntityEntry; import org.hibernate.engine.spi.EntityKey; +import org.hibernate.engine.spi.PersistenceContext; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.event.service.spi.EventListenerRegistry; @@ -460,6 +461,7 @@ public abstract class AbstractEntityTuplizer implements EntityTuplizer { final Type[] subTypes = virtualIdComponent.getSubtypes(); final Type[] copierSubTypes = mappedIdentifierType.getSubtypes(); final Iterable persistEventListeners = persistEventListeners( session ); + final PersistenceContext persistenceContext = session.getPersistenceContext(); final int length = subTypes.length; for ( int i = 0 ; i < length; i++ ) { if ( propertyValues[i] == null ) { @@ -488,7 +490,7 @@ public abstract class AbstractEntityTuplizer implements EntityTuplizer { for ( PersistEventListener listener : persistEventListeners ) { listener.onPersist( event ); } - pcEntry = session.getPersistenceContext().getEntry( propertyValues[i] ); + pcEntry = persistenceContext.getEntry( propertyValues[i] ); if ( pcEntry == null || pcEntry.getId() == null ) { throw new HibernateException( "Unable to process implicit derived identity cascade" ); } @@ -507,6 +509,7 @@ public abstract class AbstractEntityTuplizer implements EntityTuplizer { public void setIdentifier(Object entity, Serializable id, EntityMode entityMode, SessionImplementor session) { final Object[] extractedValues = mappedIdentifierType.getPropertyValues( id, entityMode ); final Object[] injectionValues = new Object[ extractedValues.length ]; + final PersistenceContext persistenceContext = session.getPersistenceContext(); for ( int i = 0; i < virtualIdComponent.getSubtypes().length; i++ ) { final Type virtualPropertyType = virtualIdComponent.getSubtypes()[i]; final Type idClassPropertyType = mappedIdentifierType.getSubtypes()[i]; @@ -522,10 +525,10 @@ public abstract class AbstractEntityTuplizer implements EntityTuplizer { session.getFactory().getEntityPersister( associatedEntityName ) ); // it is conceivable there is a proxy, so check that first - Object association = session.getPersistenceContext().getProxy( entityKey ); + Object association = persistenceContext.getProxy( entityKey ); if ( association == null ) { // otherwise look for an initialized version - association = session.getPersistenceContext().getEntity( entityKey ); + association = persistenceContext.getEntity( entityKey ); } injectionValues[i] = association; }