ref #9204 further uncritical updates to AbstractPersistentCollection
authorAndreas Müller <a.mueller@bgbm.org>
Tue, 25 Aug 2020 11:11:20 +0000 (13:11 +0200)
committerAndreas Müller <a.mueller@bgbm.org>
Tue, 25 Aug 2020 11:36:10 +0000 (13:36 +0200)
eu.etaxonomy.taxeditor.cdmlib/src/main/java/org/hibernate/collection/internal/AbstractPersistentCollection.java

index 54d957fc761c320bc300b442a7f87a6547757913..ce2b1226a6bd3c17a4295294829269849455940c 100644 (file)
@@ -39,11 +39,13 @@ import java.util.Map;
 import javax.naming.NamingException;
 
 import org.hibernate.AssertionFailure;
+import org.hibernate.FlushMode;
 import org.hibernate.HibernateException;
 import org.hibernate.LazyInitializationException;
 import org.hibernate.Session;
 import org.hibernate.collection.internal.AbstractPersistentCollection.DelayedOperation;
 import org.hibernate.collection.internal.AbstractPersistentCollection.IteratorProxy;
+import org.hibernate.collection.internal.AbstractPersistentCollection.LazyInitializationWork;
 import org.hibernate.collection.internal.AbstractPersistentCollection.ListIteratorProxy;
 import org.hibernate.collection.internal.AbstractPersistentCollection.ValueDelayedOperation;
 import org.hibernate.collection.spi.PersistentCollection;
@@ -63,6 +65,7 @@ import org.hibernate.internal.util.collections.IdentitySet;
 import org.hibernate.persister.collection.CollectionPersister;
 import org.hibernate.persister.entity.EntityPersister;
 import org.hibernate.pretty.MessageHelper;
+import org.hibernate.type.CompositeType;
 import org.hibernate.type.IntegerType;
 import org.hibernate.type.LongType;
 import org.hibernate.type.PostgresUUIDType;
@@ -203,35 +206,35 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
                                        LOG.info("--> readSize, of " + getRole() + " with key " + getKey());
                                        read();
                                } else {
-                                       boolean isExtraLazy = withTemporarySessionIfNeeded(
-                                                       new LazyInitializationWork<Boolean>() {
-                                                               @Override
-                                                               public Boolean doWork() {
-                                                                       CollectionEntry entry = session.getPersistenceContext().getCollectionEntry( AbstractPersistentCollection.this );
-
-                                                                       if ( entry != null ) {
-                                                                               CollectionPersister persister = entry.getLoadedPersister();
-                                                                               if ( persister.isExtraLazy() ) {
-                                                                                       if ( hasQueuedOperations() ) {
-                                                                                               session.flush();
-                                                                                       }
-                                                                                       cachedSize = persister.getSize( entry.getLoadedKey(), session );
-                                                                                       return true;
-                                                                               }
-                                                                               else {
-                                                                                       read();
+                               final boolean isExtraLazy = withTemporarySessionIfNeeded(
+                                               new LazyInitializationWork<Boolean>() {
+                                                       @Override
+                                                       public Boolean doWork() {
+                                                               final CollectionEntry entry = session.getPersistenceContext().getCollectionEntry( AbstractPersistentCollection.this );
+
+                                                               if ( entry != null ) {
+                                                                       final CollectionPersister persister = entry.getLoadedPersister();
+                                                                       if ( persister.isExtraLazy() ) {
+                                                                               if ( hasQueuedOperations() ) {
+                                                                                       session.flush();
                                                                                }
+                                                                               cachedSize = persister.getSize( entry.getLoadedKey(), session );
+                                                                               return true;
                                                                        }
-                                                                       else{
-                                                                               throwLazyInitializationExceptionIfNotConnected();
+                                                                       else {
+                                                                               read();
                                                                        }
-                                                                       return false;
                                                                }
+                                                               else{
+                                                                       throwLazyInitializationExceptionIfNotConnected();
+                                                               }
+                                                               return false;
                                                        }
-                                                       );
-                                       if ( isExtraLazy ) {
-                                               return true;
-                                       }
+                                               }
+                               );
+                               if ( isExtraLazy ) {
+                                       return true;
+                               }
                                }
                        }
                }
@@ -295,13 +298,14 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
                                        .getTransactionFactory()
                                        .compatibleWithJtaSynchronization();*/
                        isJTA = session.getTransactionCoordinator().getTransactionCoordinatorBuilder().isJta();
+                       
                        if ( !isJTA ) {
                                // Explicitly handle the transactions only if we're not in
                                // a JTA environment.  A lazy loading temporary session can
                                // be created even if a current session and transaction are
                                // open (ex: session.clear() was used).  We must prevent
                                // multiple transactions.
-                               ( ( Session) session ).beginTransaction();
+                               ( (Session) session ).beginTransaction();
                        }
 
                        session.getPersistenceContext().addUninitializedDetachedCollection(
@@ -770,12 +774,8 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
                return false;
        }
 
-       /**
-        * To be called internally by the session, forcing
-        * immediate initialization.
-        */
        @Override
-    public final void forceInitialization() throws HibernateException {
+       public final void forceInitialization() throws HibernateException {
                if ( !initialized ) {
                        if ( initializing ) {
                                throw new AssertionFailure( "force initialize loading collection" );
@@ -901,7 +901,7 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
                        itr.remove();
                }
        }
-       
+
        protected final class ListIteratorProxy implements ListIterator {
                protected final ListIterator itr;
 
@@ -1198,11 +1198,11 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
 
                public Object getOrphan();
        }
-       
+
        protected interface ValueDelayedOperation extends DelayedOperation {
                void replace(CollectionPersister collectionPersister, Map copyCache);
        }
-       
+
        protected abstract class AbstractValueDelayedOperation implements ValueDelayedOperation {
                private Object addedValue;
                private Object orphan;
@@ -1247,29 +1247,31 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
 
                // short-circuit(s)
                if ( currentElements.size() == 0 ) {
-                       return oldElements; // no new elements, the old list contains only Orphans
+                       // no new elements, the old list contains only Orphans
+                       return oldElements;
                }
                if ( oldElements.size() == 0 ) {
-                       return oldElements; // no old elements, so no Orphans neither
+                       // no old elements, so no Orphans neither
+                       return oldElements;
                }
 
                final EntityPersister entityPersister = session.getFactory().getEntityPersister( entityName );
                final Type idType = entityPersister.getIdentifierType();
 
                // create the collection holding the Orphans
-               Collection res = new ArrayList();
+               final Collection res = new ArrayList();
 
                // collect EntityIdentifier(s) of the *current* elements - add them into a HashSet for fast access
-               java.util.Set currentIds = new HashSet();
-               java.util.Set currentSaving = new IdentitySet();
+               final java.util.Set currentIds = new HashSet();
+               final java.util.Set currentSaving = new IdentitySet();
                for ( Object current : currentElements ) {
                        if ( current != null && ForeignKeys.isNotTransient( entityName, current, null, session ) ) {
-                               EntityEntry ee = session.getPersistenceContext().getEntry( current );
+                               final EntityEntry ee = session.getPersistenceContext().getEntry( current );
                                if ( ee != null && ee.getStatus() == Status.SAVING ) {
                                        currentSaving.add( current );
                                }
                                else {
-                                       Serializable currentId = ForeignKeys.getEntityIdentifierIfNotUnsaved(
+                                       final Serializable currentId = ForeignKeys.getEntityIdentifierIfNotUnsaved(
                                                        entityName,
                                                        current,
                                                        session
@@ -1301,20 +1303,28 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
                        || idType == PostgresUUIDType.INSTANCE;
        }
 
+       /**
+        * Removes entity entries that have an equal identifier with the incoming entity instance
+        *
+        * @param list The list containing the entity instances
+        * @param entityInstance The entity instance to match elements.
+        * @param entityName The entity name
+        * @param session The session
+        */
        public static void identityRemove(
                        Collection list,
-                       Object object,
+                       Object entityInstance,
                        String entityName,
-                       SessionImplementor session) throws HibernateException {
+                       SessionImplementor session) {
 
-               if ( object != null && ForeignKeys.isNotTransient( entityName, object, null, session ) ) {
+               if ( entityInstance != null && ForeignKeys.isNotTransient( entityName, entityInstance, null, session ) ) {
                        final EntityPersister entityPersister = session.getFactory().getEntityPersister( entityName );
-                       Type idType = entityPersister.getIdentifierType();
+                       final Type idType = entityPersister.getIdentifierType();
 
-                       Serializable idOfCurrent = ForeignKeys.getEntityIdentifierIfNotUnsaved( entityName, object, session );
-                       Iterator itr = list.iterator();
+                       final Serializable idOfCurrent = ForeignKeys.getEntityIdentifierIfNotUnsaved( entityName, entityInstance, session );
+                       final Iterator itr = list.iterator();
                        while ( itr.hasNext() ) {
-                               Serializable idOfOld = ForeignKeys.getEntityIdentifierIfNotUnsaved( entityName, itr.next(), session );
+                               final Serializable idOfOld = ForeignKeys.getEntityIdentifierIfNotUnsaved( entityName, itr.next(), session );
                                if ( idType.isEqual( idOfCurrent, idOfOld, session.getFactory() ) ) {
                                        itr.remove();
                                        break;