HHH-13587 Introduce SPI method PersistenceContext#getCollectionEntriesSize()

This commit is contained in:
Sanne Grinovero 2019-08-17 17:00:13 +01:00 committed by Sanne Grinovero
parent 0d10174c23
commit 3b30c60ce3
7 changed files with 25 additions and 13 deletions

View File

@ -1768,6 +1768,11 @@ public class StatefulPersistenceContext implements PersistenceContext {
return ( nullifiableEntityKeys == null || nullifiableEntityKeys.size() == 0 );
}
@Override
public int getCollectionEntriesSize() {
return collectionEntries == null ? 0 : collectionEntries.size();
}
private void cleanUpInsertedKeysAfterTransaction() {
if ( insertedKeysMap != null ) {
insertedKeysMap.clear();

View File

@ -745,6 +745,13 @@ public interface PersistenceContext {
*/
boolean isNullifiableEntityKeysEmpty();
/**
* The size of the internal map storing all collection entries.
* (The map is not exposed directly, but the size is often useful)
* @return the size
*/
int getCollectionEntriesSize();
/**
* Provides centralized access to natural-id-related functionality.
*/

View File

@ -125,7 +125,7 @@ public abstract class AbstractFlushingEventListener implements JpaBootstrapSensi
session.getActionQueue().numberOfCollectionCreations(),
session.getActionQueue().numberOfCollectionUpdates(),
session.getActionQueue().numberOfCollectionRemovals(),
persistenceContext.getCollectionEntries().size()
persistenceContext.getCollectionEntriesSize()
);
new EntityPrinter( session.getFactory() ).toString(
persistenceContext.getEntitiesByKey().entrySet()

View File

@ -89,6 +89,6 @@ public class DefaultAutoFlushEventListener extends AbstractFlushingEventListener
return !source.getHibernateFlushMode().lessThan( FlushMode.AUTO )
&& source.getDontFlushFromFind() == 0
&& ( persistenceContext.getNumberOfManagedEntities() > 0 ||
persistenceContext.getCollectionEntries().size() > 0 );
persistenceContext.getCollectionEntriesSize() > 0 );
}
}

View File

@ -31,7 +31,7 @@ public class DefaultFlushEventListener extends AbstractFlushingEventListener imp
final PersistenceContext persistenceContext = source.getPersistenceContextInternal();
if ( persistenceContext.getNumberOfManagedEntities() > 0 ||
persistenceContext.getCollectionEntries().size() > 0 ) {
persistenceContext.getCollectionEntriesSize() > 0 ) {
try {
source.getEventListenerManager().flushStart();

View File

@ -28,7 +28,7 @@ public class SessionStatisticsImpl implements SessionStatistics {
}
public int getCollectionCount() {
return session.getPersistenceContextInternal().getCollectionEntries().size();
return session.getPersistenceContextInternal().getCollectionEntriesSize();
}
public Set getEntityKeys() {

View File

@ -60,7 +60,7 @@ public class TestAutoFlushBeforeQueryExecution extends BaseCoreFunctionalTestCas
final PersistenceContext persistenceContext = ( (SessionImplementor) s ).getPersistenceContext();
final ActionQueue actionQueue = ( (SessionImpl) s ).getActionQueue();
assertEquals( 1, persistenceContext.getCollectionEntries().size() );
assertEquals( 1, persistenceContext.getCollectionEntriesSize() );
assertEquals( 1, persistenceContext.getCollectionsByKey().size() );
assertTrue( persistenceContext.getCollectionEntries().containsKey( publisher.getAuthors() ) );
assertTrue( persistenceContext.getCollectionsByKey().values().contains( publisher.getAuthors() ) );
@ -73,7 +73,7 @@ public class TestAutoFlushBeforeQueryExecution extends BaseCoreFunctionalTestCas
"autoflush collection update",
s.createQuery( "select a from Publisher p join p.authors a" ).list().size() == 1
);
assertEquals( 2, persistenceContext.getCollectionEntries().size() );
assertEquals( 2, persistenceContext.getCollectionEntriesSize() );
assertEquals( 2, persistenceContext.getCollectionsByKey().size() );
assertTrue( persistenceContext.getCollectionEntries().containsKey( publisher.getAuthors() ) );
assertTrue( persistenceContext.getCollectionEntries().containsKey( author1.getBooks() ) );
@ -88,7 +88,7 @@ public class TestAutoFlushBeforeQueryExecution extends BaseCoreFunctionalTestCas
assertTrue( "autoflush collection update",
s.createQuery( "select a from Publisher p join p.authors a" ).list().size() == 0
);
assertEquals( 1, persistenceContext.getCollectionEntries().size() );
assertEquals( 1, persistenceContext.getCollectionEntriesSize() );
assertEquals( 1, persistenceContext.getCollectionsByKey().size() );
assertTrue( persistenceContext.getCollectionEntries().containsKey( publisher.getAuthors() ) );
assertTrue( persistenceContext.getCollectionsByKey().values().contains( publisher.getAuthors() ) );
@ -102,7 +102,7 @@ public class TestAutoFlushBeforeQueryExecution extends BaseCoreFunctionalTestCas
publisher.getAuthors().add( author2 );
List results = s.createQuery( "select a from Publisher p join p.authors a" ).list();
assertEquals( 1, results.size() );
assertEquals( 2, persistenceContext.getCollectionEntries().size() );
assertEquals( 2, persistenceContext.getCollectionEntriesSize() );
assertEquals( 2, persistenceContext.getCollectionsByKey().size() );
assertTrue( persistenceContext.getCollectionEntries().containsKey( publisher.getAuthors() ) );
assertTrue( persistenceContext.getCollectionEntries().containsKey( author2.getBooks() ) );
@ -139,7 +139,7 @@ public class TestAutoFlushBeforeQueryExecution extends BaseCoreFunctionalTestCas
final PersistenceContext persistenceContext = ( (SessionImplementor) s ).getPersistenceContext();
final ActionQueue actionQueue = ( (SessionImpl) s ).getActionQueue();
assertEquals( 1, persistenceContext.getCollectionEntries().size() );
assertEquals( 1, persistenceContext.getCollectionEntriesSize() );
assertEquals( 1, persistenceContext.getCollectionsByKey().size() );
assertTrue( persistenceContext.getCollectionEntries().containsKey( publisher.getAuthors() ) );
assertTrue( persistenceContext.getCollectionsByKey().values().contains( publisher.getAuthors() ) );
@ -149,7 +149,7 @@ public class TestAutoFlushBeforeQueryExecution extends BaseCoreFunctionalTestCas
author1.setPublisher( publisher );
publisher.getAuthors().add( author1 );
assertTrue( s.createQuery( "from UnrelatedEntity" ).list().size() == 1 );
assertEquals( 2, persistenceContext.getCollectionEntries().size() );
assertEquals( 2, persistenceContext.getCollectionEntriesSize() );
assertEquals( 1, persistenceContext.getCollectionsByKey().size() );
assertTrue( persistenceContext.getCollectionEntries().containsKey( publisher.getAuthors() ) );
assertTrue( persistenceContext.getCollectionEntries().containsKey( author1.getBooks() ) );
@ -161,7 +161,7 @@ public class TestAutoFlushBeforeQueryExecution extends BaseCoreFunctionalTestCas
publisher.getAuthors().clear();
assertEquals( 0, actionQueue.numberOfCollectionRemovals() );
assertTrue( s.createQuery( "from UnrelatedEntity" ).list().size() == 1 );
assertEquals( 2, persistenceContext.getCollectionEntries().size() );
assertEquals( 2, persistenceContext.getCollectionEntriesSize() );
assertEquals( 1, persistenceContext.getCollectionsByKey().size() );
assertTrue( persistenceContext.getCollectionEntries().containsKey( publisher.getAuthors() ) );
assertTrue( persistenceContext.getCollectionEntries().containsKey( author1.getBooks() ) );
@ -176,7 +176,7 @@ public class TestAutoFlushBeforeQueryExecution extends BaseCoreFunctionalTestCas
publisher.getAuthors().add( author2 );
List results = s.createQuery( "from UnrelatedEntity" ).list();
assertEquals( 1, results.size() );
assertEquals( 4, persistenceContext.getCollectionEntries().size() );
assertEquals( 4, persistenceContext.getCollectionEntriesSize() );
assertEquals( 1, persistenceContext.getCollectionsByKey().size() );
assertTrue( persistenceContext.getCollectionEntries().containsKey( publisher.getAuthors() ) );
assertTrue( persistenceContext.getCollectionEntries().containsKey( author2.getBooks() ) );
@ -186,7 +186,7 @@ public class TestAutoFlushBeforeQueryExecution extends BaseCoreFunctionalTestCas
assertEquals( 0, actionQueue.numberOfCollectionRemovals() );
s.flush();
assertEquals( 2, persistenceContext.getCollectionEntries().size() );
assertEquals( 2, persistenceContext.getCollectionEntriesSize() );
assertEquals( 2, persistenceContext.getCollectionsByKey().size() );
assertTrue( persistenceContext.getCollectionEntries().containsKey( publisher.getAuthors() ) );
assertTrue( persistenceContext.getCollectionEntries().containsKey( author2.getBooks() ) );