From e2de2a6fcf33dafd9d795bff50d5c4ccfb81b53d Mon Sep 17 00:00:00 2001 From: Gail Badner Date: Mon, 20 Oct 2014 16:35:00 -0700 Subject: [PATCH] HHH-9455 : Unnecessary select count query in some cases (cherry picked from commit 552c1c06d513bd066c0a3c6386119ea33e083cf7) (cherry picked from commit 460e9662146dbeda74c2cbb54d6964baf7c4f610) --- .../collection/AbstractCollectionPersister.java | 3 +-- .../collection/OneToManyPersister.java | 17 +++++++++++------ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/persister/collection/AbstractCollectionPersister.java b/hibernate-core/src/main/java/org/hibernate/persister/collection/AbstractCollectionPersister.java index 1291663ca2..0775336718 100644 --- a/hibernate-core/src/main/java/org/hibernate/persister/collection/AbstractCollectionPersister.java +++ b/hibernate-core/src/main/java/org/hibernate/persister/collection/AbstractCollectionPersister.java @@ -1766,8 +1766,7 @@ public abstract class AbstractCollectionPersister public void processQueuedOps(PersistentCollection collection, Serializable key, SessionImplementor session) throws HibernateException { if ( collection.hasQueuedOperations() ) { - int nextIndex = getSize( key, session ); - doProcessQueuedOps( collection, key, nextIndex, session ); + doProcessQueuedOps( collection, key, session ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/persister/collection/OneToManyPersister.java b/hibernate-core/src/main/java/org/hibernate/persister/collection/OneToManyPersister.java index 42613f40fc..3646910a94 100644 --- a/hibernate-core/src/main/java/org/hibernate/persister/collection/OneToManyPersister.java +++ b/hibernate-core/src/main/java/org/hibernate/persister/collection/OneToManyPersister.java @@ -195,28 +195,33 @@ public class OneToManyPersister extends AbstractCollectionPersister { public void recreate(PersistentCollection collection, Serializable id, SessionImplementor session) throws HibernateException { super.recreate( collection, id, session ); - writeIndex( collection, collection.entries( this ), id, 0, session ); + writeIndex( collection, collection.entries( this ), id, true, session ); } @Override public void insertRows(PersistentCollection collection, Serializable id, SessionImplementor session) throws HibernateException { super.insertRows( collection, id, session ); - writeIndex( collection, collection.entries( this ), id, 0, session ); + writeIndex( collection, collection.entries( this ), id, true, session ); } @Override protected void doProcessQueuedOps(PersistentCollection collection, Serializable id, SessionImplementor session) throws HibernateException { - writeIndex( collection, collection.queuedAdditionIterator(), id, getSize( id, session ), session ); + writeIndex( collection, collection.queuedAdditionIterator(), id, false, session ); } - - private void writeIndex(PersistentCollection collection, Iterator entries, Serializable id, - int nextIndex, SessionImplementor session) { + + private void writeIndex( + PersistentCollection collection, + Iterator entries, + Serializable id, + boolean resetIndex, + SessionImplementor session) { // If one-to-many and inverse, still need to create the index. See HHH-5732. if ( isInverse && hasIndex && !indexContainsFormula ) { try { if ( entries.hasNext() ) { + int nextIndex = resetIndex ? 0 : getSize( id, session ); Expectation expectation = Expectations.appropriateExpectation( getUpdateCheckStyle() ); while ( entries.hasNext() ) {