From a63ae7041b53dae6c099dd5dd2c4ea72f6e85a1b Mon Sep 17 00:00:00 2001 From: Gavin King Date: Mon, 18 Aug 2025 18:36:28 +1000 Subject: [PATCH 1/4] use 'var' in actions --- .../internal/AbstractEntityInsertAction.java | 53 ++++++------- .../internal/BulkOperationCleanupAction.java | 46 +++++------ .../action/internal/CollectionAction.java | 2 +- .../internal/CollectionRecreateAction.java | 16 ++-- .../internal/CollectionRemoveAction.java | 14 ++-- .../internal/CollectionUpdateAction.java | 18 ++--- .../internal/DelayedPostInsertIdentifier.java | 11 +-- .../action/internal/EntityAction.java | 3 +- .../action/internal/EntityDeleteAction.java | 66 +++++++--------- .../internal/EntityIdentityInsertAction.java | 31 +++----- .../EntityIncrementVersionProcess.java | 3 +- .../action/internal/EntityInsertAction.java | 77 ++++++++---------- .../action/internal/EntityUpdateAction.java | 79 ++++++++----------- .../internal/EntityVerifyVersionProcess.java | 3 +- .../QueuedOperationCollectionAction.java | 5 +- .../UnresolvedEntityInsertActions.java | 35 ++++---- 16 files changed, 200 insertions(+), 262 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/action/internal/AbstractEntityInsertAction.java b/hibernate-core/src/main/java/org/hibernate/action/internal/AbstractEntityInsertAction.java index 18ae832287b6..599797633038 100644 --- a/hibernate-core/src/main/java/org/hibernate/action/internal/AbstractEntityInsertAction.java +++ b/hibernate-core/src/main/java/org/hibernate/action/internal/AbstractEntityInsertAction.java @@ -12,19 +12,13 @@ import org.hibernate.engine.internal.Nullability.NullabilityCheckType; import org.hibernate.engine.spi.CachedNaturalIdValueSource; import org.hibernate.engine.spi.CollectionKey; -import org.hibernate.engine.spi.EntityEntry; -import org.hibernate.engine.spi.EntityHolder; import org.hibernate.engine.spi.EntityKey; +import org.hibernate.engine.spi.NaturalIdResolutions; import org.hibernate.engine.spi.PersistenceContext; import org.hibernate.engine.spi.Status; import org.hibernate.event.spi.EventSource; -import org.hibernate.metamodel.mapping.AttributeMapping; -import org.hibernate.metamodel.mapping.AttributeMappingsList; -import org.hibernate.metamodel.mapping.EmbeddableMappingType; -import org.hibernate.metamodel.mapping.NaturalIdMapping; import org.hibernate.metamodel.mapping.PluralAttributeMapping; import org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping; -import org.hibernate.persister.collection.CollectionPersister; import org.hibernate.persister.entity.AbstractEntityPersister; import org.hibernate.persister.entity.EntityPersister; @@ -134,12 +128,9 @@ protected final void nullifyTransientReferencesIfNotAlready() { public final void makeEntityManaged() { nullifyTransientReferencesIfNotAlready(); final Object version = getVersion( getState(), getPersister() ); - final PersistenceContext persistenceContextInternal = getSession().getPersistenceContextInternal(); - final EntityHolder entityHolder = persistenceContextInternal.addEntityHolder( - getEntityKey(), - getInstance() - ); - final EntityEntry entityEntry = persistenceContextInternal.addEntry( + final var persistenceContext = getSession().getPersistenceContextInternal(); + final var entityHolder = persistenceContext.addEntityHolder( getEntityKey(), getInstance() ); + final var entityEntry = persistenceContext.addEntry( getInstance(), getPersister().isMutable() ? Status.MANAGED : Status.READ_ONLY, getState(), @@ -153,13 +144,13 @@ public final void makeEntityManaged() { ); entityHolder.setEntityEntry( entityEntry ); if ( isEarlyInsert() ) { - addCollectionsByKeyToPersistenceContext( persistenceContextInternal, getState() ); + addCollectionsByKeyToPersistenceContext( persistenceContext, getState() ); } } protected void addCollectionsByKeyToPersistenceContext(PersistenceContext persistenceContext, Object[] objects) { for ( int i = 0; i < objects.length; i++ ) { - final AttributeMapping attributeMapping = getPersister().getAttributeMapping( i ); + final var attributeMapping = getPersister().getAttributeMapping( i ); if ( attributeMapping.isEmbeddedAttributeMapping() ) { visitEmbeddedAttributeMapping( attributeMapping.asEmbeddedAttributeMapping(), @@ -182,13 +173,12 @@ private void visitEmbeddedAttributeMapping( Object object, PersistenceContext persistenceContext) { if ( object != null ) { - final EmbeddableMappingType descriptor = attributeMapping.getEmbeddableTypeDescriptor(); - final EmbeddableMappingType.ConcreteEmbeddableType concreteEmbeddableType = descriptor.findSubtypeBySubclass( - object.getClass().getName() - ); - final AttributeMappingsList attributeMappings = descriptor.getAttributeMappings(); + final var descriptor = attributeMapping.getEmbeddableTypeDescriptor(); + final var concreteEmbeddableType = + descriptor.findSubtypeBySubclass( object.getClass().getName() ); + final var attributeMappings = descriptor.getAttributeMappings(); for ( int i = 0; i < attributeMappings.size(); i++ ) { - final AttributeMapping attribute = attributeMappings.get( i ); + final var attribute = attributeMappings.get( i ); if ( concreteEmbeddableType.declaresAttribute( attribute ) ) { if ( attribute.isPluralAttributeMapping() ) { addCollectionKey( @@ -214,7 +204,7 @@ private void addCollectionKey( Object object, PersistenceContext persistenceContext) { if ( object instanceof PersistentCollection ) { - final CollectionPersister collectionPersister = pluralAttributeMapping.getCollectionDescriptor(); + final var collectionPersister = pluralAttributeMapping.getCollectionDescriptor(); final Object key = AbstractEntityPersister.getCollectionKey( collectionPersister, getInstance(), @@ -222,7 +212,7 @@ private void addCollectionKey( getSession() ); if ( key != null ) { - final CollectionKey collectionKey = new CollectionKey( collectionPersister, key ); + final var collectionKey = new CollectionKey( collectionPersister, key ); persistenceContext.addCollectionByKey( collectionKey, (PersistentCollection) object ); } } @@ -243,13 +233,17 @@ protected void markExecuted() { protected abstract Object getRowId(); + private NaturalIdResolutions getNaturalIdResolutions() { + return getSession().getPersistenceContextInternal().getNaturalIdResolutions(); + } + @Override public void afterDeserialize(EventSource session) { super.afterDeserialize( session ); // IMPL NOTE: non-flushed changes code calls this method with session == null... // guard against NullPointerException if ( session != null ) { - final EntityEntry entityEntry = session.getPersistenceContextInternal().getEntry( getInstance() ); + final var entityEntry = session.getPersistenceContextInternal().getEntry( getInstance() ); this.state = entityEntry.getLoadedState(); } } @@ -259,9 +253,9 @@ public void afterDeserialize(EventSource session) { */ protected void handleNaturalIdPreSaveNotifications() { // before save, we need to add a natural id cross-reference to the persistence-context - final NaturalIdMapping naturalIdMapping = getPersister().getNaturalIdMapping(); + final var naturalIdMapping = getPersister().getNaturalIdMapping(); if ( naturalIdMapping != null ) { - getSession().getPersistenceContextInternal().getNaturalIdResolutions().manageLocalResolution( + getNaturalIdResolutions().manageLocalResolution( getId(), naturalIdMapping.extractNaturalIdFromEntityState( state ), getPersister(), @@ -276,12 +270,13 @@ protected void handleNaturalIdPreSaveNotifications() { * @param generatedId The generated entity identifier */ public void handleNaturalIdPostSaveNotifications(Object generatedId) { - final NaturalIdMapping naturalIdMapping = getPersister().getNaturalIdMapping(); + final var naturalIdMapping = getPersister().getNaturalIdMapping(); if ( naturalIdMapping != null ) { final Object naturalIdValues = naturalIdMapping.extractNaturalIdFromEntityState( state ); + final var resolutions = getNaturalIdResolutions(); if ( isEarlyInsert() ) { // with early insert, we still need to add a local (transactional) natural id cross-reference - getSession().getPersistenceContextInternal().getNaturalIdResolutions().manageLocalResolution( + resolutions.manageLocalResolution( generatedId, naturalIdValues, getPersister(), @@ -289,7 +284,7 @@ public void handleNaturalIdPostSaveNotifications(Object generatedId) { ); } // after save, we need to manage the shared cache entries - getSession().getPersistenceContextInternal().getNaturalIdResolutions().manageSharedResolution( + resolutions.manageSharedResolution( generatedId, naturalIdValues, null, diff --git a/hibernate-core/src/main/java/org/hibernate/action/internal/BulkOperationCleanupAction.java b/hibernate-core/src/main/java/org/hibernate/action/internal/BulkOperationCleanupAction.java index 89b21400e8de..650b91d2f926 100644 --- a/hibernate-core/src/main/java/org/hibernate/action/internal/BulkOperationCleanupAction.java +++ b/hibernate-core/src/main/java/org/hibernate/action/internal/BulkOperationCleanupAction.java @@ -6,7 +6,6 @@ import java.io.Serializable; import java.util.ArrayList; -import java.util.Collections; import java.util.HashSet; import java.util.LinkedHashSet; import java.util.List; @@ -22,14 +21,12 @@ import org.hibernate.cache.spi.access.SoftLock; import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.event.spi.EventSource; -import org.hibernate.metamodel.spi.MappingMetamodelImplementor; -import org.hibernate.persister.collection.CollectionPersister; import org.hibernate.persister.entity.EntityPersister; import org.hibernate.query.sqm.tree.SqmDmlStatement; -import org.hibernate.query.sqm.tree.SqmQuery; -import org.hibernate.query.sqm.tree.cte.SqmCteStatement; import org.hibernate.sql.ast.tree.insert.InsertSelectStatement; +import static java.util.Collections.addAll; + /** * An {@link org.hibernate.engine.spi.ActionQueue} {@link Executable} for * ensuring shared cache cleanup in relation to performed bulk HQL queries. @@ -62,27 +59,25 @@ public class BulkOperationCleanupAction implements Executable, Serializable { */ public BulkOperationCleanupAction(SharedSessionContractImplementor session, EntityPersister... affectedQueryables) { final LinkedHashSet spacesList = new LinkedHashSet<>(); - for ( EntityPersister persister : affectedQueryables ) { - Collections.addAll( spacesList, (String[]) persister.getQuerySpaces() ); + for ( var persister : affectedQueryables ) { + addAll( spacesList, (String[]) persister.getQuerySpaces() ); if ( persister.canWriteToCache() ) { - final EntityDataAccess entityDataAccess = persister.getCacheAccessStrategy(); + final var entityDataAccess = persister.getCacheAccessStrategy(); if ( entityDataAccess != null ) { entityCleanups.add( new EntityCleanup( entityDataAccess, session ) ); } } if ( persister.hasNaturalIdentifier() && persister.hasNaturalIdCache() ) { - naturalIdCleanups.add( - new NaturalIdCleanup( persister.getNaturalIdCacheAccessStrategy(), session ) - ); + naturalIdCleanups.add( new NaturalIdCleanup( persister.getNaturalIdCacheAccessStrategy(), session ) ); } - final MappingMetamodelImplementor mappingMetamodel = session.getFactory().getMappingMetamodel(); - final Set roles = mappingMetamodel.getCollectionRolesByEntityParticipant( persister.getEntityName() ); + final var mappingMetamodel = session.getFactory().getMappingMetamodel(); + final var roles = mappingMetamodel.getCollectionRolesByEntityParticipant( persister.getEntityName() ); if ( roles != null ) { for ( String role : roles ) { - final CollectionPersister collectionPersister = mappingMetamodel.getCollectionDescriptor( role ); + final var collectionPersister = mappingMetamodel.getCollectionDescriptor( role ); if ( collectionPersister.hasCache() ) { collectionCleanups.add( new CollectionCleanup( collectionPersister.getCacheAccessStrategy(), session ) @@ -92,7 +87,7 @@ public BulkOperationCleanupAction(SharedSessionContractImplementor session, Enti } } - this.affectedTableSpaces = spacesList.toArray( new String[ 0 ] ); + affectedTableSpaces = spacesList.toArray( new String[ 0 ] ); } /** @@ -111,11 +106,11 @@ public BulkOperationCleanupAction(SharedSessionContractImplementor session, Enti public BulkOperationCleanupAction(SharedSessionContractImplementor session, Set tableSpaces) { final LinkedHashSet spacesList = new LinkedHashSet<>( tableSpaces ); - final MappingMetamodelImplementor metamodel = session.getFactory().getMappingMetamodel(); + final var metamodel = session.getFactory().getMappingMetamodel(); metamodel.forEachEntityDescriptor( (entityDescriptor) -> { final String[] entitySpaces = (String[]) entityDescriptor.getQuerySpaces(); if ( affectedEntity( tableSpaces, entitySpaces ) ) { - Collections.addAll( spacesList, entitySpaces ); + addAll( spacesList, entitySpaces ); if ( entityDescriptor.canWriteToCache() ) { entityCleanups.add( new EntityCleanup( entityDescriptor.getCacheAccessStrategy(), session ) ); @@ -124,10 +119,10 @@ public BulkOperationCleanupAction(SharedSessionContractImplementor session, Set< naturalIdCleanups.add( new NaturalIdCleanup( entityDescriptor.getNaturalIdCacheAccessStrategy(), session ) ); } - final Set roles = metamodel.getCollectionRolesByEntityParticipant( entityDescriptor.getEntityName() ); + final var roles = metamodel.getCollectionRolesByEntityParticipant( entityDescriptor.getEntityName() ); if ( roles != null ) { for ( String role : roles ) { - final CollectionPersister collectionDescriptor = metamodel.getCollectionDescriptor( role ); + final var collectionDescriptor = metamodel.getCollectionDescriptor( role ); if ( collectionDescriptor.hasCache() ) { collectionCleanups.add( new CollectionCleanup( collectionDescriptor.getCacheAccessStrategy(), session ) @@ -138,18 +133,17 @@ public BulkOperationCleanupAction(SharedSessionContractImplementor session, Set< } } ); - this.affectedTableSpaces = spacesList.toArray( new String[ 0 ] ); + affectedTableSpaces = spacesList.toArray( new String[ 0 ] ); } public static void schedule(SharedSessionContractImplementor session, SqmDmlStatement statement) { final List entityPersisters = new ArrayList<>( 1 ); - final MappingMetamodelImplementor metamodel = session.getFactory().getMappingMetamodel(); + final var metamodel = session.getFactory().getMappingMetamodel(); if ( !( statement instanceof InsertSelectStatement ) ) { entityPersisters.add( metamodel.getEntityDescriptor( statement.getTarget().getEntityName() ) ); } - for ( SqmCteStatement cteStatement : statement.getCteStatements() ) { - final SqmQuery cteDefinition = cteStatement.getCteDefinition(); - if ( cteDefinition instanceof SqmDmlStatement dmlStatement ) { + for ( var cteStatement : statement.getCteStatements() ) { + if ( cteStatement.getCteDefinition() instanceof SqmDmlStatement dmlStatement ) { entityPersisters.add( metamodel.getEntityDescriptor( dmlStatement.getTarget().getEntityName() ) ); @@ -160,7 +154,7 @@ public static void schedule(SharedSessionContractImplementor session, SqmDmlStat } public static void schedule(SharedSessionContractImplementor session, EntityPersister... affectedQueryables) { - final BulkOperationCleanupAction action = new BulkOperationCleanupAction( session, affectedQueryables ); + final var action = new BulkOperationCleanupAction( session, affectedQueryables ); if ( session.isEventSource() ) { session.asEventSource().getActionQueue().addAction( action ); } @@ -170,7 +164,7 @@ public static void schedule(SharedSessionContractImplementor session, EntityPers } public static void schedule(SharedSessionContractImplementor session, Set affectedQueryables) { - final BulkOperationCleanupAction action = new BulkOperationCleanupAction( session, affectedQueryables ); + final var action = new BulkOperationCleanupAction( session, affectedQueryables ); if ( session.isEventSource() ) { session.asEventSource().getActionQueue().addAction( action ); } diff --git a/hibernate-core/src/main/java/org/hibernate/action/internal/CollectionAction.java b/hibernate-core/src/main/java/org/hibernate/action/internal/CollectionAction.java index 76cef5478a97..b5be548ddd07 100644 --- a/hibernate-core/src/main/java/org/hibernate/action/internal/CollectionAction.java +++ b/hibernate-core/src/main/java/org/hibernate/action/internal/CollectionAction.java @@ -136,7 +136,7 @@ protected final EventSource getSession() { protected final void evict() throws CacheException { if ( persister.hasCache() ) { - final CollectionDataAccess cache = persister.getCacheAccessStrategy(); + final var cache = persister.getCacheAccessStrategy(); final Object ck = cache.generateCacheKey( key, persister, diff --git a/hibernate-core/src/main/java/org/hibernate/action/internal/CollectionRecreateAction.java b/hibernate-core/src/main/java/org/hibernate/action/internal/CollectionRecreateAction.java index 7e020cd14b8c..f637d5229e0c 100644 --- a/hibernate-core/src/main/java/org/hibernate/action/internal/CollectionRecreateAction.java +++ b/hibernate-core/src/main/java/org/hibernate/action/internal/CollectionRecreateAction.java @@ -6,16 +6,12 @@ import org.hibernate.HibernateException; import org.hibernate.collection.spi.PersistentCollection; -import org.hibernate.engine.spi.SharedSessionContractImplementor; -import org.hibernate.event.monitor.spi.EventMonitor; import org.hibernate.event.spi.EventSource; -import org.hibernate.event.monitor.spi.DiagnosticEvent; import org.hibernate.event.spi.PostCollectionRecreateEvent; import org.hibernate.event.spi.PostCollectionRecreateEventListener; import org.hibernate.event.spi.PreCollectionRecreateEvent; import org.hibernate.event.spi.PreCollectionRecreateEventListener; import org.hibernate.persister.collection.CollectionPersister; -import org.hibernate.stat.spi.StatisticsImplementor; /** * The action for recreating a collection @@ -41,13 +37,13 @@ public CollectionRecreateAction( public void execute() throws HibernateException { // this method is called when a new non-null collection is persisted // or when an existing (non-null) collection is moved to a new owner - final PersistentCollection collection = getCollection(); + final var collection = getCollection(); preRecreate(); - final SharedSessionContractImplementor session = getSession(); - final CollectionPersister persister = getPersister(); + final var session = getSession(); + final var persister = getPersister(); final Object key = getKey(); - final EventMonitor eventMonitor = session.getEventMonitor(); - final DiagnosticEvent event = eventMonitor.beginCollectionRecreateEvent(); + final var eventMonitor = session.getEventMonitor(); + final var event = eventMonitor.beginCollectionRecreateEvent(); boolean success = false; try { persister.recreate( collection, key, session ); @@ -61,7 +57,7 @@ public void execute() throws HibernateException { evict(); postRecreate(); - final StatisticsImplementor statistics = session.getFactory().getStatistics(); + final var statistics = session.getFactory().getStatistics(); if ( statistics.isStatisticsEnabled() ) { statistics.recreateCollection( persister.getRole() ); } diff --git a/hibernate-core/src/main/java/org/hibernate/action/internal/CollectionRemoveAction.java b/hibernate-core/src/main/java/org/hibernate/action/internal/CollectionRemoveAction.java index 6d9fc2d3b16d..e81c43c17d53 100644 --- a/hibernate-core/src/main/java/org/hibernate/action/internal/CollectionRemoveAction.java +++ b/hibernate-core/src/main/java/org/hibernate/action/internal/CollectionRemoveAction.java @@ -7,16 +7,12 @@ import org.hibernate.AssertionFailure; import org.hibernate.HibernateException; import org.hibernate.collection.spi.PersistentCollection; -import org.hibernate.engine.spi.SharedSessionContractImplementor; -import org.hibernate.event.monitor.spi.EventMonitor; import org.hibernate.event.spi.EventSource; -import org.hibernate.event.monitor.spi.DiagnosticEvent; import org.hibernate.event.spi.PostCollectionRemoveEvent; import org.hibernate.event.spi.PostCollectionRemoveEventListener; import org.hibernate.event.spi.PreCollectionRemoveEvent; import org.hibernate.event.spi.PreCollectionRemoveEventListener; import org.hibernate.persister.collection.CollectionPersister; -import org.hibernate.stat.spi.StatisticsImplementor; /** * The action for removing a collection @@ -104,16 +100,16 @@ public CollectionRemoveAction( @Override public void execute() throws HibernateException { preRemove(); - final SharedSessionContractImplementor session = getSession(); + final var session = getSession(); if ( !emptySnapshot ) { // an existing collection that was either nonempty or uninitialized // is replaced by null or a different collection // (if the collection is uninitialized, Hibernate has no way of // knowing if the collection is actually empty without querying the db) - final CollectionPersister persister = getPersister(); + final var persister = getPersister(); final Object key = getKey(); - final EventMonitor eventMonitor = session.getEventMonitor(); - final DiagnosticEvent event = eventMonitor.beginCollectionRemoveEvent(); + final var eventMonitor = session.getEventMonitor(); + final var event = eventMonitor.beginCollectionRemoveEvent(); boolean success = false; try { persister.remove( key, session ); @@ -131,7 +127,7 @@ public void execute() throws HibernateException { evict(); postRemove(); - final StatisticsImplementor statistics = session.getFactory().getStatistics(); + final var statistics = session.getFactory().getStatistics(); if ( statistics.isStatisticsEnabled() ) { statistics.removeCollection( getPersister().getRole() ); } diff --git a/hibernate-core/src/main/java/org/hibernate/action/internal/CollectionUpdateAction.java b/hibernate-core/src/main/java/org/hibernate/action/internal/CollectionUpdateAction.java index d2ed3a1e88af..e806805f9b2c 100644 --- a/hibernate-core/src/main/java/org/hibernate/action/internal/CollectionUpdateAction.java +++ b/hibernate-core/src/main/java/org/hibernate/action/internal/CollectionUpdateAction.java @@ -8,16 +8,12 @@ import org.hibernate.HibernateException; import org.hibernate.collection.spi.PersistentCollection; import org.hibernate.engine.spi.ComparableExecutable; -import org.hibernate.engine.spi.SharedSessionContractImplementor; -import org.hibernate.event.monitor.spi.EventMonitor; import org.hibernate.event.spi.EventSource; -import org.hibernate.event.monitor.spi.DiagnosticEvent; import org.hibernate.event.spi.PostCollectionUpdateEvent; import org.hibernate.event.spi.PostCollectionUpdateEventListener; import org.hibernate.event.spi.PreCollectionUpdateEvent; import org.hibernate.event.spi.PreCollectionUpdateEventListener; import org.hibernate.persister.collection.CollectionPersister; -import org.hibernate.stat.spi.StatisticsImplementor; import static org.hibernate.pretty.MessageHelper.collectionInfoString; @@ -49,9 +45,9 @@ public CollectionUpdateAction( @Override public void execute() throws HibernateException { final Object key = getKey(); - final SharedSessionContractImplementor session = getSession(); - final CollectionPersister persister = getPersister(); - final PersistentCollection collection = getCollection(); + final var session = getSession(); + final var persister = getPersister(); + final var collection = getCollection(); final boolean affectedByFilters = persister.isAffectedByEnabledFilters( session ); preUpdate(); @@ -66,8 +62,8 @@ public void execute() throws HibernateException { // Do nothing - we only need to notify the cache } else { - final EventMonitor eventMonitor = session.getEventMonitor(); - final DiagnosticEvent event = eventMonitor.beginCollectionUpdateEvent(); + final var eventMonitor = session.getEventMonitor(); + final var event = eventMonitor.beginCollectionUpdateEvent(); boolean success = false; try { if ( !affectedByFilters && collection.empty() ) { @@ -102,7 +98,7 @@ else if ( collection.needsRecreate( persister ) ) { evict(); postUpdate(); - final StatisticsImplementor statistics = session.getFactory().getStatistics(); + final var statistics = session.getFactory().getStatistics(); if ( statistics.isStatisticsEnabled() ) { statistics.updateCollection( persister.getRole() ); } @@ -116,7 +112,7 @@ else if ( collection.needsRecreate( persister ) ) { public int compareTo(ComparableExecutable executable) { if ( executable instanceof CollectionUpdateAction that && getPrimarySortClassifier().equals( executable.getPrimarySortClassifier() ) ) { - final CollectionPersister persister = getPersister(); + final var persister = getPersister(); final boolean hasDeletes = this.getCollection().hasDeletes( persister ); final boolean otherHasDeletes = that.getCollection().hasDeletes( persister ); if ( hasDeletes && !otherHasDeletes ) { diff --git a/hibernate-core/src/main/java/org/hibernate/action/internal/DelayedPostInsertIdentifier.java b/hibernate-core/src/main/java/org/hibernate/action/internal/DelayedPostInsertIdentifier.java index fabb0caa10bf..67a801834b83 100644 --- a/hibernate-core/src/main/java/org/hibernate/action/internal/DelayedPostInsertIdentifier.java +++ b/hibernate-core/src/main/java/org/hibernate/action/internal/DelayedPostInsertIdentifier.java @@ -41,7 +41,7 @@ public DelayedPostInsertIdentifier() { } } } - this.identifier = value; + identifier = value; } @Override @@ -49,16 +49,17 @@ public boolean equals(Object o) { if ( this == o ) { return true; } - if ( o == null || getClass() != o.getClass() ) { + else if ( !(o instanceof DelayedPostInsertIdentifier that) ) { return false; } - final DelayedPostInsertIdentifier that = (DelayedPostInsertIdentifier) o; - return identifier == that.identifier; + else { + return identifier == that.identifier; + } } @Override public int hashCode() { - return (int) ( identifier ^ ( identifier >>> 32 ) ); + return Long.hashCode( identifier ); } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/action/internal/EntityAction.java b/hibernate-core/src/main/java/org/hibernate/action/internal/EntityAction.java index 881af8169ba4..183cdb641393 100644 --- a/hibernate-core/src/main/java/org/hibernate/action/internal/EntityAction.java +++ b/hibernate-core/src/main/java/org/hibernate/action/internal/EntityAction.java @@ -8,7 +8,6 @@ import org.hibernate.action.spi.AfterTransactionCompletionProcess; import org.hibernate.action.spi.BeforeTransactionCompletionProcess; import org.hibernate.engine.spi.ComparableExecutable; -import org.hibernate.engine.spi.EntityEntry; import org.hibernate.event.spi.EventSource; import org.hibernate.event.service.spi.EventListenerGroups; import org.hibernate.persister.entity.EntityPersister; @@ -94,7 +93,7 @@ public String getEntityName() { */ public final Object getId() { if ( id instanceof DelayedPostInsertIdentifier ) { - final EntityEntry entry = session.getPersistenceContextInternal().getEntry( instance ); + final var entry = session.getPersistenceContextInternal().getEntry( instance ); final Object eeId = entry == null ? null : entry.getId(); return eeId instanceof DelayedPostInsertIdentifier ? null : eeId; } diff --git a/hibernate-core/src/main/java/org/hibernate/action/internal/EntityDeleteAction.java b/hibernate-core/src/main/java/org/hibernate/action/internal/EntityDeleteAction.java index 9a4fbd366810..7d5a45a12697 100644 --- a/hibernate-core/src/main/java/org/hibernate/action/internal/EntityDeleteAction.java +++ b/hibernate-core/src/main/java/org/hibernate/action/internal/EntityDeleteAction.java @@ -6,25 +6,15 @@ import org.hibernate.AssertionFailure; import org.hibernate.HibernateException; -import org.hibernate.cache.spi.access.EntityDataAccess; import org.hibernate.cache.spi.access.SoftLock; -import org.hibernate.engine.spi.EntityEntry; -import org.hibernate.engine.spi.EntityKey; -import org.hibernate.engine.spi.PersistenceContext; import org.hibernate.engine.spi.SharedSessionContractImplementor; -import org.hibernate.event.service.spi.EventListenerGroup; -import org.hibernate.event.monitor.spi.EventMonitor; import org.hibernate.event.spi.EventSource; -import org.hibernate.event.monitor.spi.DiagnosticEvent; import org.hibernate.event.spi.PostCommitDeleteEventListener; import org.hibernate.event.spi.PostDeleteEvent; import org.hibernate.event.spi.PostDeleteEventListener; import org.hibernate.event.spi.PreDeleteEvent; -import org.hibernate.event.spi.PreDeleteEventListener; -import org.hibernate.metamodel.mapping.NaturalIdMapping; import org.hibernate.persister.entity.EntityPersister; import org.hibernate.stat.internal.StatsHelper; -import org.hibernate.stat.spi.StatisticsImplementor; /** * The action for performing an entity deletion. @@ -110,13 +100,13 @@ private boolean isInstanceLoaded() { public void execute() throws HibernateException { final Object id = getId(); final Object version = getCurrentVersion(); - final EntityPersister persister = getPersister(); - final SharedSessionContractImplementor session = getSession(); + final var persister = getPersister(); + final var session = getSession(); final Object instance = getInstance(); final boolean veto = isInstanceLoaded() && preDelete(); - final NaturalIdMapping naturalIdMapping = persister.getNaturalIdMapping(); + final var naturalIdMapping = persister.getNaturalIdMapping(); if ( naturalIdMapping != null ) { naturalIdValues = session.getPersistenceContextInternal().getNaturalIdResolutions() .removeLocalResolution( @@ -129,8 +119,8 @@ public void execute() throws HibernateException { final Object ck = lockCacheItem(); if ( !isCascadeDeleteEnabled && !veto ) { - final EventMonitor eventMonitor = session.getEventMonitor(); - final DiagnosticEvent event = eventMonitor.beginEntityDeleteEvent(); + final var eventMonitor = session.getEventMonitor(); + final var event = eventMonitor.beginEntityDeleteEvent(); boolean success = false; try { persister.getDeleteCoordinator().delete( instance, id, version, session ); @@ -149,7 +139,7 @@ public void execute() throws HibernateException { postDeleteUnloaded( id, persister, session, ck ); } - final StatisticsImplementor statistics = getSession().getFactory().getStatistics(); + final var statistics = getSession().getFactory().getStatistics(); if ( statistics.isStatisticsEnabled() && !veto ) { statistics.deleteEntity( getPersister().getEntityName() ); } @@ -175,22 +165,23 @@ protected void postDeleteLoaded( // After actually deleting a row, record the fact that the instance no longer // exists on the database (needed for identity-column key generation), and // remove it from the session cache - final PersistenceContext persistenceContext = session.getPersistenceContextInternal(); - final EntityEntry entry = persistenceContext.removeEntry( instance ); + final var persistenceContext = session.getPersistenceContextInternal(); + final var entry = persistenceContext.removeEntry( instance ); if ( entry == null ) { throw new AssertionFailure( "possible non-threadsafe access to session" ); } entry.postDelete(); - final EntityKey key = entry.getEntityKey(); + final var key = entry.getEntityKey(); persistenceContext.removeEntityHolder( key ); removeCacheItem( ck ); - persistenceContext.getNaturalIdResolutions().removeSharedResolution( id, naturalIdValues, persister, true); + persistenceContext.getNaturalIdResolutions() + .removeSharedResolution( id, naturalIdValues, persister, true); postDelete(); } protected void postDeleteUnloaded(Object id, EntityPersister persister, SharedSessionContractImplementor session, Object ck) { - final PersistenceContext persistenceContext = session.getPersistenceContextInternal(); - final EntityKey key = session.generateEntityKey( id, persister ); + final var persistenceContext = session.getPersistenceContextInternal(); + final var key = session.generateEntityKey( id, persister ); if ( !persistenceContext.containsDeletedUnloadedEntityKey( key ) ) { throw new AssertionFailure( "deleted proxy should be for an unloaded entity: " + key ); } @@ -199,15 +190,15 @@ protected void postDeleteUnloaded(Object id, EntityPersister persister, SharedSe } protected boolean preDelete() { - final EventListenerGroup listenerGroup - = getEventListenerGroups().eventListenerGroup_PRE_DELETE; + final var listenerGroup = getEventListenerGroups().eventListenerGroup_PRE_DELETE; if ( listenerGroup.isEmpty() ) { return false; } else { - final PreDeleteEvent event = new PreDeleteEvent( getInstance(), getId(), state, getPersister(), eventSource() ); + final PreDeleteEvent event = + new PreDeleteEvent( getInstance(), getId(), state, getPersister(), eventSource() ); boolean veto = false; - for ( PreDeleteEventListener listener : listenerGroup.listeners() ) { + for ( var listener : listenerGroup.listeners() ) { veto |= listener.onPreDelete( event ); } return veto; @@ -224,9 +215,8 @@ PostDeleteEvent newPostDeleteEvent() { } protected void postCommitDelete(boolean success) { - final EventListenerGroup eventListeners - = getEventListenerGroups().eventListenerGroup_POST_COMMIT_DELETE; - if (success) { + final var eventListeners = getEventListenerGroups().eventListenerGroup_POST_COMMIT_DELETE; + if ( success ) { eventListeners.fireLazyEventOnEachListener( this::newPostDeleteEvent, PostDeleteEventListener::onPostDelete ); } else { @@ -252,7 +242,7 @@ public void doAfterTransactionCompletion(boolean success, SharedSessionContractI @Override protected boolean hasPostCommitEventListeners() { - for ( PostDeleteEventListener listener: getEventListenerGroups().eventListenerGroup_POST_COMMIT_DELETE.listeners() ) { + for ( var listener: getEventListenerGroups().eventListenerGroup_POST_COMMIT_DELETE.listeners() ) { if ( listener.requiresPostCommitHandling( getPersister() ) ) { return true; } @@ -261,10 +251,10 @@ protected boolean hasPostCommitEventListeners() { } protected Object lockCacheItem() { - final EntityPersister persister = getPersister(); + final var persister = getPersister(); if ( persister.canWriteToCache() ) { - final EntityDataAccess cache = persister.getCacheAccessStrategy(); - final SharedSessionContractImplementor session = getSession(); + final var cache = persister.getCacheAccessStrategy(); + final var session = getSession(); final Object ck = cache.generateCacheKey( getId(), persister, @@ -280,10 +270,10 @@ protected Object lockCacheItem() { } protected void unlockCacheItem() { - final EntityPersister persister = getPersister(); + final var persister = getPersister(); if ( persister.canWriteToCache() ) { - final EntityDataAccess cache = persister.getCacheAccessStrategy(); - final SharedSessionContractImplementor session = getSession(); + final var cache = persister.getCacheAccessStrategy(); + final var session = getSession(); final Object ck = cache.generateCacheKey( getId(), persister, @@ -295,11 +285,11 @@ protected void unlockCacheItem() { } protected void removeCacheItem(Object ck) { - final EntityPersister persister = getPersister(); + final var persister = getPersister(); if ( persister.canWriteToCache() ) { persister.getCacheAccessStrategy().remove( getSession(), ck ); - final StatisticsImplementor statistics = getSession().getFactory().getStatistics(); + final var statistics = getSession().getFactory().getStatistics(); if ( statistics.isStatisticsEnabled() ) { statistics.entityCacheRemove( StatsHelper.getRootEntityRole( persister ), diff --git a/hibernate-core/src/main/java/org/hibernate/action/internal/EntityIdentityInsertAction.java b/hibernate-core/src/main/java/org/hibernate/action/internal/EntityIdentityInsertAction.java index 7c70ef1dc9d9..6e53e6a3a031 100644 --- a/hibernate-core/src/main/java/org/hibernate/action/internal/EntityIdentityInsertAction.java +++ b/hibernate-core/src/main/java/org/hibernate/action/internal/EntityIdentityInsertAction.java @@ -7,20 +7,14 @@ import org.hibernate.AssertionFailure; import org.hibernate.HibernateException; import org.hibernate.engine.spi.EntityKey; -import org.hibernate.engine.spi.PersistenceContext; import org.hibernate.engine.spi.SharedSessionContractImplementor; -import org.hibernate.event.service.spi.EventListenerGroup; -import org.hibernate.event.monitor.spi.EventMonitor; import org.hibernate.event.spi.EventSource; -import org.hibernate.event.monitor.spi.DiagnosticEvent; import org.hibernate.event.spi.PostCommitInsertEventListener; import org.hibernate.event.spi.PostInsertEvent; import org.hibernate.event.spi.PostInsertEventListener; import org.hibernate.event.spi.PreInsertEvent; -import org.hibernate.event.spi.PreInsertEventListener; import org.hibernate.generator.values.GeneratedValues; import org.hibernate.persister.entity.EntityPersister; -import org.hibernate.stat.spi.StatisticsImplementor; import static org.hibernate.internal.util.NullnessUtil.castNonNull; @@ -72,8 +66,8 @@ public EntityIdentityInsertAction( public void execute() throws HibernateException { nullifyTransientReferencesIfNotAlready(); - final EntityPersister persister = getPersister(); - final SharedSessionContractImplementor session = getSession(); + final var persister = getPersister(); + final var session = getSession(); final Object instance = getInstance(); setVeto( preInsert() ); @@ -82,8 +76,8 @@ public void execute() throws HibernateException { // else inserted the same pk first, the insert would fail if ( !isVeto() ) { - final EventMonitor eventMonitor = session.getEventMonitor(); - final DiagnosticEvent event = eventMonitor.beginEntityInsertEvent(); + final var eventMonitor = session.getEventMonitor(); + final var event = eventMonitor.beginEntityInsertEvent(); boolean success = false; final GeneratedValues generatedValues; try { @@ -94,7 +88,7 @@ public void execute() throws HibernateException { finally { eventMonitor.completeEntityInsertEvent( event, generatedId, persister.getEntityName(), success, session ); } - final PersistenceContext persistenceContext = session.getPersistenceContextInternal(); + final var persistenceContext = session.getPersistenceContextInternal(); if ( persister.getRowIdMapping() != null ) { rowId = generatedValues.getGeneratedValue( persister.getRowIdMapping() ); if ( rowId != null && isDelayed ) { @@ -122,7 +116,7 @@ public void execute() throws HibernateException { postInsert(); - final StatisticsImplementor statistics = session.getFactory().getStatistics(); + final var statistics = session.getFactory().getStatistics(); if ( statistics.isStatisticsEnabled() && !isVeto() ) { statistics.insertEntity( getPersister().getEntityName() ); } @@ -138,9 +132,8 @@ public boolean needsAfterTransactionCompletion() { @Override protected boolean hasPostCommitEventListeners() { - final EventListenerGroup group - = getEventListenerGroups().eventListenerGroup_POST_COMMIT_INSERT; - for ( PostInsertEventListener listener : group.listeners() ) { + final var group = getEventListenerGroups().eventListenerGroup_POST_COMMIT_INSERT; + for ( var listener : group.listeners() ) { if ( listener.requiresPostCommitHandling( getPersister() ) ) { return true; } @@ -188,16 +181,16 @@ private void postCommitInsertOnFailure(PostInsertEventListener listener, PostIns } protected boolean preInsert() { - final EventListenerGroup listenerGroup - = getEventListenerGroups().eventListenerGroup_PRE_INSERT; + final var listenerGroup = getEventListenerGroups().eventListenerGroup_PRE_INSERT; if ( listenerGroup.isEmpty() ) { // NO_VETO return false; } else { - final PreInsertEvent event = new PreInsertEvent( getInstance(), null, getState(), getPersister(), eventSource() ); + final PreInsertEvent event = + new PreInsertEvent( getInstance(), null, getState(), getPersister(), eventSource() ); boolean veto = false; - for ( PreInsertEventListener listener : listenerGroup.listeners() ) { + for ( var listener : listenerGroup.listeners() ) { veto |= listener.onPreInsert( event ); } return veto; diff --git a/hibernate-core/src/main/java/org/hibernate/action/internal/EntityIncrementVersionProcess.java b/hibernate-core/src/main/java/org/hibernate/action/internal/EntityIncrementVersionProcess.java index 9f2e725a2f6f..6ca5276cff37 100644 --- a/hibernate-core/src/main/java/org/hibernate/action/internal/EntityIncrementVersionProcess.java +++ b/hibernate-core/src/main/java/org/hibernate/action/internal/EntityIncrementVersionProcess.java @@ -5,7 +5,6 @@ package org.hibernate.action.internal; import org.hibernate.action.spi.BeforeTransactionCompletionProcess; -import org.hibernate.engine.spi.EntityEntry; import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.internal.OptimisticLockHelper; @@ -35,7 +34,7 @@ public EntityIncrementVersionProcess(Object object) { */ @Override public void doBeforeTransactionCompletion(SessionImplementor session) { - final EntityEntry entry = session.getPersistenceContext().getEntry( object ); + final var entry = session.getPersistenceContext().getEntry( object ); // Don't increment version for an entity that is not in the PersistenceContext; if ( entry != null ) { OptimisticLockHelper.forceVersionIncrement( object, entry, session.asEventSource() ); diff --git a/hibernate-core/src/main/java/org/hibernate/action/internal/EntityInsertAction.java b/hibernate-core/src/main/java/org/hibernate/action/internal/EntityInsertAction.java index 0c3ff06563a4..5ffd333838c5 100644 --- a/hibernate-core/src/main/java/org/hibernate/action/internal/EntityInsertAction.java +++ b/hibernate-core/src/main/java/org/hibernate/action/internal/EntityInsertAction.java @@ -7,27 +7,20 @@ import org.hibernate.AssertionFailure; import org.hibernate.HibernateException; import org.hibernate.cache.spi.access.EntityDataAccess; -import org.hibernate.cache.spi.entry.CacheEntry; import org.hibernate.engine.internal.Versioning; import org.hibernate.engine.spi.EntityEntry; import org.hibernate.engine.spi.EntityKey; import org.hibernate.engine.spi.PersistenceContext; -import org.hibernate.engine.spi.SessionEventListenerManager; -import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.event.monitor.spi.EventMonitor; -import org.hibernate.event.monitor.spi.DiagnosticEvent; -import org.hibernate.event.service.spi.EventListenerGroup; import org.hibernate.event.spi.EventSource; import org.hibernate.event.spi.PostCommitInsertEventListener; import org.hibernate.event.spi.PostInsertEvent; import org.hibernate.event.spi.PostInsertEventListener; import org.hibernate.event.spi.PreInsertEvent; -import org.hibernate.event.spi.PreInsertEventListener; import org.hibernate.generator.values.GeneratedValues; import org.hibernate.persister.entity.EntityPersister; import org.hibernate.stat.internal.StatsHelper; -import org.hibernate.stat.spi.StatisticsImplementor; /** * The action for performing an entity insertion, for entities not defined to use {@code IDENTITY} generation. @@ -99,14 +92,14 @@ public void execute() throws HibernateException { // Don't need to lock the cache here, since if someone // else inserted the same pk first, the insert would fail - final SharedSessionContractImplementor session = getSession(); + final var session = getSession(); final Object id = getId(); final boolean veto = preInsert(); if ( !veto ) { - final EntityPersister persister = getPersister(); + final var persister = getPersister(); final Object instance = getInstance(); - final EventMonitor eventMonitor = session.getEventMonitor(); - final DiagnosticEvent event = eventMonitor.beginEntityInsertEvent(); + final var eventMonitor = session.getEventMonitor(); + final var event = eventMonitor.beginEntityInsertEvent(); boolean success = false; final GeneratedValues generatedValues; try { @@ -116,8 +109,8 @@ public void execute() throws HibernateException { finally { eventMonitor.completeEntityInsertEvent( event, id, persister.getEntityName(), success, session ); } - final PersistenceContext persistenceContext = session.getPersistenceContextInternal(); - final EntityEntry entry = persistenceContext.getEntry( instance ); + final var persistenceContext = session.getPersistenceContextInternal(); + final var entry = persistenceContext.getEntry( instance ); if ( entry == null ) { throw new AssertionFailure( "possible non-threadsafe access to session" ); } @@ -130,7 +123,7 @@ public void execute() throws HibernateException { handleNaturalIdPostSaveNotifications( id ); postInsert(); - final StatisticsImplementor statistics = session.getFactory().getStatistics(); + final var statistics = session.getFactory().getStatistics(); if ( statistics.isStatisticsEnabled() && !veto ) { statistics.insertEntity( getPersister().getEntityName() ); } @@ -142,7 +135,7 @@ private void handleGeneratedProperties( EntityEntry entry, GeneratedValues generatedValues, PersistenceContext persistenceContext) { - final EntityPersister persister = getPersister(); + final var persister = getPersister(); if ( persister.hasInsertGeneratedProperties() ) { final Object instance = getInstance(); persister.processInsertGeneratedProperties( getId(), instance, getState(), generatedValues, getSession() ); @@ -165,17 +158,17 @@ else if ( persister.isVersionPropertyGenerated() ) { } protected void putCacheIfNecessary() { - final EntityPersister persister = getPersister(); - final SharedSessionContractImplementor session = getSession(); + final var persister = getPersister(); + final var session = getSession(); if ( isCachePutEnabled( persister, session ) ) { - final SessionFactoryImplementor factory = session.getFactory(); - final CacheEntry ce = persister.buildCacheEntry( getInstance(), getState(), version, session ); + final var factory = session.getFactory(); + final var ce = persister.buildCacheEntry( getInstance(), getState(), version, session ); cacheEntry = persister.getCacheEntryStructure().structure( ce ); - final EntityDataAccess cache = persister.getCacheAccessStrategy(); + final var cache = persister.getCacheAccessStrategy(); final Object ck = cache.generateCacheKey( getId(), persister, factory, session.getTenantIdentifier() ); final boolean put = cacheInsert( persister, ck ); - final StatisticsImplementor statistics = factory.getStatistics(); + final var statistics = factory.getStatistics(); if ( put && statistics.isStatisticsEnabled() ) { statistics.entityCachePut( StatsHelper.getRootEntityRole( persister ), @@ -186,13 +179,14 @@ protected void putCacheIfNecessary() { } protected boolean cacheInsert(EntityPersister persister, Object ck) { - final SharedSessionContractImplementor session = getSession(); - final EventMonitor eventMonitor = session.getEventMonitor(); - final DiagnosticEvent cachePutEvent = eventMonitor.beginCachePutEvent(); - final EntityDataAccess cacheAccessStrategy = persister.getCacheAccessStrategy(); + final var session = getSession(); + final var eventMonitor = session.getEventMonitor(); + final var cachePutEvent = eventMonitor.beginCachePutEvent(); + final var cacheAccessStrategy = persister.getCacheAccessStrategy(); + final var eventListenerManager = session.getEventListenerManager(); boolean insert = false; try { - session.getEventListenerManager().cachePutStart(); + eventListenerManager.cachePutStart(); insert = cacheAccessStrategy.insert( session, ck, cacheEntry, version ); return insert; } @@ -205,7 +199,7 @@ protected boolean cacheInsert(EntityPersister persister, Object ck) { insert, EventMonitor.CacheActionDescription.ENTITY_INSERT ); - session.getEventListenerManager().cachePutEnd(); + eventListenerManager.cachePutEnd(); } } @@ -236,15 +230,15 @@ private void postCommitOnFailure(PostInsertEventListener listener, PostInsertEve } protected boolean preInsert() { - final EventListenerGroup listenerGroup - = getEventListenerGroups().eventListenerGroup_PRE_INSERT; + final var listenerGroup = getEventListenerGroups().eventListenerGroup_PRE_INSERT; if ( listenerGroup.isEmpty() ) { return false; } else { boolean veto = false; - final PreInsertEvent event = new PreInsertEvent( getInstance(), getId(), getState(), getPersister(), eventSource() ); - for ( PreInsertEventListener listener : listenerGroup.listeners() ) { + final PreInsertEvent event = + new PreInsertEvent( getInstance(), getId(), getState(), getPersister(), eventSource() ); + for ( var listener : listenerGroup.listeners() ) { veto |= listener.onPreInsert( event ); } return veto; @@ -253,14 +247,14 @@ protected boolean preInsert() { @Override public void doAfterTransactionCompletion(boolean success, SharedSessionContractImplementor session) throws HibernateException { - final EntityPersister persister = getPersister(); + final var persister = getPersister(); if ( success && isCachePutEnabled( persister, getSession() ) ) { - final EntityDataAccess cache = persister.getCacheAccessStrategy(); - final SessionFactoryImplementor factory = session.getFactory(); + final var cache = persister.getCacheAccessStrategy(); + final var factory = session.getFactory(); final Object ck = cache.generateCacheKey( getId(), persister, factory, session.getTenantIdentifier() ); final boolean put = cacheAfterInsert( cache, ck ); - final StatisticsImplementor statistics = factory.getStatistics(); + final var statistics = factory.getStatistics(); if ( put && statistics.isStatisticsEnabled() ) { statistics.entityCachePut( StatsHelper.getRootEntityRole( persister ), @@ -272,10 +266,10 @@ public void doAfterTransactionCompletion(boolean success, SharedSessionContractI } protected boolean cacheAfterInsert(EntityDataAccess cache, Object ck) { - final SharedSessionContractImplementor session = getSession(); - final SessionEventListenerManager eventListenerManager = session.getEventListenerManager(); - final EventMonitor eventMonitor = session.getEventMonitor(); - final DiagnosticEvent cachePutEvent = eventMonitor.beginCachePutEvent(); + final var session = getSession(); + final var eventListenerManager = session.getEventListenerManager(); + final var eventMonitor = session.getEventMonitor(); + final var cachePutEvent = eventMonitor.beginCachePutEvent(); boolean afterInsert = false; try { eventListenerManager.cachePutStart(); @@ -297,9 +291,8 @@ protected boolean cacheAfterInsert(EntityDataAccess cache, Object ck) { @Override protected boolean hasPostCommitEventListeners() { - final EventListenerGroup group - = getEventListenerGroups().eventListenerGroup_POST_COMMIT_INSERT; - for ( PostInsertEventListener listener : group.listeners() ) { + final var group = getEventListenerGroups().eventListenerGroup_POST_COMMIT_INSERT; + for ( var listener : group.listeners() ) { if ( listener.requiresPostCommitHandling( getPersister() ) ) { return true; } diff --git a/hibernate-core/src/main/java/org/hibernate/action/internal/EntityUpdateAction.java b/hibernate-core/src/main/java/org/hibernate/action/internal/EntityUpdateAction.java index 729f0d9ba06e..83def5c4be45 100644 --- a/hibernate-core/src/main/java/org/hibernate/action/internal/EntityUpdateAction.java +++ b/hibernate-core/src/main/java/org/hibernate/action/internal/EntityUpdateAction.java @@ -10,28 +10,20 @@ import org.hibernate.cache.CacheException; import org.hibernate.cache.spi.access.EntityDataAccess; import org.hibernate.cache.spi.access.SoftLock; -import org.hibernate.cache.spi.entry.CacheEntry; import org.hibernate.engine.spi.CachedNaturalIdValueSource; import org.hibernate.engine.spi.EntityEntry; -import org.hibernate.engine.spi.SessionEventListenerManager; -import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.engine.spi.Status; import org.hibernate.event.monitor.spi.EventMonitor; -import org.hibernate.event.monitor.spi.DiagnosticEvent; -import org.hibernate.event.service.spi.EventListenerGroup; import org.hibernate.event.spi.EventSource; import org.hibernate.event.spi.PostCommitUpdateEventListener; import org.hibernate.event.spi.PostUpdateEvent; import org.hibernate.event.spi.PostUpdateEventListener; import org.hibernate.event.spi.PreUpdateEvent; -import org.hibernate.event.spi.PreUpdateEventListener; import org.hibernate.generator.values.GeneratedValues; import org.hibernate.metamodel.mapping.NaturalIdMapping; import org.hibernate.persister.entity.EntityPersister; import org.hibernate.stat.internal.StatsHelper; -import org.hibernate.stat.spi.StatisticsImplementor; -import org.hibernate.tuple.entity.EntityMetamodel; import org.hibernate.type.TypeHelper; import static org.hibernate.engine.internal.Versioning.getVersion; @@ -158,14 +150,14 @@ protected void setCacheEntry(Object cacheEntry) { @Override public void execute() throws HibernateException { if ( !preUpdate() ) { - final EntityPersister persister = getPersister(); - final SharedSessionContractImplementor session = getSession(); + final var persister = getPersister(); + final var session = getSession(); final Object id = getId(); final Object instance = getInstance(); final Object previousVersion = getPreviousVersion(); final Object ck = lockCacheItem( previousVersion ); - final EventMonitor eventMonitor = session.getEventMonitor(); - final DiagnosticEvent event = eventMonitor.beginEntityUpdateEvent(); + final var eventMonitor = session.getEventMonitor(); + final var event = eventMonitor.beginEntityUpdateEvent(); boolean success = false; final GeneratedValues generatedValues; try { @@ -185,7 +177,7 @@ public void execute() throws HibernateException { finally { eventMonitor.completeEntityUpdateEvent( event, id, persister.getEntityName(), success, session ); } - final EntityEntry entry = session.getPersistenceContextInternal().getEntry( instance ); + final var entry = session.getPersistenceContextInternal().getEntry( instance ); if ( entry == null ) { throw new AssertionFailure( "possible non thread safe access to session" ); } @@ -195,7 +187,7 @@ public void execute() throws HibernateException { handleNaturalIdResolutions( persister, session, id ); postUpdate(); - final StatisticsImplementor statistics = session.getFactory().getStatistics(); + final var statistics = session.getFactory().getStatistics(); if ( statistics.isStatisticsEnabled() ) { statistics.updateEntity( getPersister().getEntityName() ); } @@ -215,19 +207,19 @@ protected void handleNaturalIdResolutions(EntityPersister persister, SharedSessi } protected void updateCacheItem(Object previousVersion, Object ck, EntityEntry entry) { - final EntityPersister persister = getPersister(); + final var persister = getPersister(); if ( persister.canWriteToCache() ) { - final SharedSessionContractImplementor session = getSession(); + final var session = getSession(); if ( isCacheInvalidationRequired( persister, session ) || entry.getStatus() != Status.MANAGED ) { persister.getCacheAccessStrategy().remove( session, ck ); } else if ( session.getCacheMode().isPutEnabled() ) { //TODO: inefficient if that cache is just going to ignore the updated state! - final CacheEntry ce = persister.buildCacheEntry( getInstance(), state, nextVersion, getSession() ); + final var ce = persister.buildCacheEntry( getInstance(), state, nextVersion, getSession() ); cacheEntry = persister.getCacheEntryStructure().structure( ce ); final boolean put = updateCache( persister, previousVersion, ck ); - final StatisticsImplementor statistics = session.getFactory().getStatistics(); + final var statistics = session.getFactory().getStatistics(); if ( put && statistics.isStatisticsEnabled() ) { statistics.entityCachePut( StatsHelper.getRootEntityRole( persister ), @@ -248,9 +240,9 @@ private static boolean isCacheInvalidationRequired( } private void handleGeneratedProperties(EntityEntry entry, GeneratedValues generatedValues) { - final EntityPersister persister = getPersister(); + final var persister = getPersister(); if ( entry.getStatus() == Status.MANAGED || persister.isVersionPropertyGenerated() ) { - final SharedSessionContractImplementor session = getSession(); + final var session = getSession(); final Object instance = getInstance(); final Object id = getId(); // get the updated snapshot of the entity state by cloning current state; @@ -282,7 +274,7 @@ private void handleGeneratedProperties(EntityEntry entry, GeneratedValues genera */ protected void handleDeleted(EntityEntry entry) { if ( entry.getStatus() == Status.DELETED ) { - final EntityMetamodel entityMetamodel = getPersister().getEntityMetamodel(); + final var entityMetamodel = getPersister().getEntityMetamodel(); final boolean isImpliedOptimisticLocking = !entityMetamodel.isVersioned() && entityMetamodel.getOptimisticLockStyle().isAllOrDirty(); if ( isImpliedOptimisticLocking && entry.getLoadedState() != null ) { @@ -295,7 +287,7 @@ protected void handleDeleted(EntityEntry entry) { } protected Object getPreviousVersion() { - final EntityPersister persister = getPersister(); + final var persister = getPersister(); if ( persister.isVersionPropertyGenerated() ) { // we need to grab the version value from the entity, otherwise // we have issues with generated-version entities that may have @@ -308,10 +300,10 @@ protected Object getPreviousVersion() { } protected Object lockCacheItem(Object previousVersion) { - final EntityPersister persister = getPersister(); + final var persister = getPersister(); if ( persister.canWriteToCache() ) { - final SharedSessionContractImplementor session = getSession(); - final EntityDataAccess cache = persister.getCacheAccessStrategy(); + final var session = getSession(); + final var cache = persister.getCacheAccessStrategy(); final Object ck = cache.generateCacheKey( getId(), persister, @@ -327,13 +319,14 @@ protected Object lockCacheItem(Object previousVersion) { } protected boolean updateCache(EntityPersister persister, Object previousVersion, Object ck) { - final SharedSessionContractImplementor session = getSession(); - final EventMonitor eventMonitor = session.getEventMonitor(); - final DiagnosticEvent cachePutEvent = eventMonitor.beginCachePutEvent(); - final EntityDataAccess cacheAccessStrategy = persister.getCacheAccessStrategy(); + final var session = getSession(); + final var eventMonitor = session.getEventMonitor(); + final var cachePutEvent = eventMonitor.beginCachePutEvent(); + final var cacheAccessStrategy = persister.getCacheAccessStrategy(); + final var eventListenerManager = session.getEventListenerManager(); boolean update = false; try { - session.getEventListenerManager().cachePutStart(); + eventListenerManager.cachePutStart(); update = cacheAccessStrategy.update( session, ck, cacheEntry, nextVersion, previousVersion ); return update; } @@ -346,13 +339,12 @@ protected boolean updateCache(EntityPersister persister, Object previousVersion, update, EventMonitor.CacheActionDescription.ENTITY_UPDATE ); - session.getEventListenerManager().cachePutEnd(); + eventListenerManager.cachePutEnd(); } } protected boolean preUpdate() { - final EventListenerGroup listenerGroup - = getEventListenerGroups().eventListenerGroup_PRE_UPDATE; + final var listenerGroup = getEventListenerGroups().eventListenerGroup_PRE_UPDATE; if ( listenerGroup.isEmpty() ) { return false; } @@ -360,7 +352,7 @@ protected boolean preUpdate() { final PreUpdateEvent event = new PreUpdateEvent( getInstance(), getId(), state, previousState, getPersister(), eventSource() ); boolean veto = false; - for ( PreUpdateEventListener listener : listenerGroup.listeners() ) { + for ( var listener : listenerGroup.listeners() ) { veto |= listener.onPreUpdate( event ); } return veto; @@ -394,9 +386,8 @@ private void onPostCommitFailure(PostUpdateEventListener listener, PostUpdateEve @Override protected boolean hasPostCommitEventListeners() { - final EventListenerGroup group - = getEventListenerGroups().eventListenerGroup_POST_COMMIT_UPDATE; - for ( PostUpdateEventListener listener : group.listeners() ) { + final var group = getEventListenerGroups().eventListenerGroup_POST_COMMIT_UPDATE; + for ( var listener : group.listeners() ) { if ( listener.requiresPostCommitHandling( getPersister() ) ) { return true; } @@ -411,10 +402,10 @@ public void doAfterTransactionCompletion(boolean success, SharedSessionContractI } private void updateCacheIfNecessary(boolean success, SharedSessionContractImplementor session) { - final EntityPersister persister = getPersister(); + final var persister = getPersister(); if ( persister.canWriteToCache() ) { - final EntityDataAccess cache = persister.getCacheAccessStrategy(); - final SessionFactoryImplementor factory = session.getFactory(); + final var cache = persister.getCacheAccessStrategy(); + final var factory = session.getFactory(); final Object ck = cache.generateCacheKey( getId(), persister, @@ -439,9 +430,9 @@ private boolean cacheUpdateRequired(boolean success, EntityPersister persister, } protected void cacheAfterUpdate(EntityDataAccess cache, Object ck, SharedSessionContractImplementor session) { - final SessionEventListenerManager eventListenerManager = session.getEventListenerManager(); - final EventMonitor eventMonitor = session.getEventMonitor(); - final DiagnosticEvent cachePutEvent = eventMonitor.beginCachePutEvent(); + final var eventListenerManager = session.getEventListenerManager(); + final var eventMonitor = session.getEventMonitor(); + final var cachePutEvent = eventMonitor.beginCachePutEvent(); boolean put = false; try { eventListenerManager.cachePutStart(); @@ -456,7 +447,7 @@ protected void cacheAfterUpdate(EntityDataAccess cache, Object ck, SharedSession put, EventMonitor.CacheActionDescription.ENTITY_AFTER_UPDATE ); - final StatisticsImplementor statistics = session.getFactory().getStatistics(); + final var statistics = session.getFactory().getStatistics(); if ( put && statistics.isStatisticsEnabled() ) { statistics.entityCachePut( StatsHelper.getRootEntityRole( getPersister() ), diff --git a/hibernate-core/src/main/java/org/hibernate/action/internal/EntityVerifyVersionProcess.java b/hibernate-core/src/main/java/org/hibernate/action/internal/EntityVerifyVersionProcess.java index 0b5f468ab02f..0483e8d7c02f 100644 --- a/hibernate-core/src/main/java/org/hibernate/action/internal/EntityVerifyVersionProcess.java +++ b/hibernate-core/src/main/java/org/hibernate/action/internal/EntityVerifyVersionProcess.java @@ -6,7 +6,6 @@ import org.hibernate.action.spi.BeforeTransactionCompletionProcess; import org.hibernate.dialect.lock.OptimisticEntityLockException; -import org.hibernate.engine.spi.EntityEntry; import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.pretty.MessageHelper; @@ -31,7 +30,7 @@ public EntityVerifyVersionProcess(Object object) { @Override public void doBeforeTransactionCompletion(SessionImplementor session) { - final EntityEntry entry = session.getPersistenceContext().getEntry( object ); + final var entry = session.getPersistenceContext().getEntry( object ); // Don't check version for an entity that is not in the PersistenceContext if ( entry != null ) { final Object latestVersion = entry.getPersister().getCurrentVersion( entry.getId(), session ); diff --git a/hibernate-core/src/main/java/org/hibernate/action/internal/QueuedOperationCollectionAction.java b/hibernate-core/src/main/java/org/hibernate/action/internal/QueuedOperationCollectionAction.java index ee03206d7601..a321365e08c6 100644 --- a/hibernate-core/src/main/java/org/hibernate/action/internal/QueuedOperationCollectionAction.java +++ b/hibernate-core/src/main/java/org/hibernate/action/internal/QueuedOperationCollectionAction.java @@ -7,7 +7,6 @@ import org.hibernate.HibernateException; import org.hibernate.collection.spi.AbstractPersistentCollection; import org.hibernate.collection.spi.PersistentCollection; -import org.hibernate.engine.spi.CollectionEntry; import org.hibernate.event.spi.EventSource; import org.hibernate.persister.collection.CollectionPersister; @@ -46,13 +45,13 @@ public void execute() throws HibernateException { // TODO: It would be nice if this could be done safely by CollectionPersister#processQueuedOps; // Can't change the SPI to do this though. - AbstractPersistentCollection collection = (AbstractPersistentCollection) getCollection(); + final var collection = (AbstractPersistentCollection) getCollection(); collection.clearOperationQueue(); // The other CollectionAction types call CollectionEntry#afterAction, which // clears the dirty flag. We don't want to call CollectionEntry#afterAction unless // there is no other CollectionAction that will be executed on the same collection. - final CollectionEntry ce = getSession().getPersistenceContextInternal().getCollectionEntry( getCollection() ); + final var ce = getSession().getPersistenceContextInternal().getCollectionEntry( getCollection() ); if ( !ce.isDoremove() && !ce.isDoupdate() && !ce.isDorecreate() ) { ce.afterAction( getCollection() ); } diff --git a/hibernate-core/src/main/java/org/hibernate/action/internal/UnresolvedEntityInsertActions.java b/hibernate-core/src/main/java/org/hibernate/action/internal/UnresolvedEntityInsertActions.java index cbdc72ca1de1..edebc3979ca1 100644 --- a/hibernate-core/src/main/java/org/hibernate/action/internal/UnresolvedEntityInsertActions.java +++ b/hibernate-core/src/main/java/org/hibernate/action/internal/UnresolvedEntityInsertActions.java @@ -15,7 +15,6 @@ import org.hibernate.PropertyValueException; import org.hibernate.TransientPropertyValueException; import org.hibernate.engine.internal.NonNullableTransientDependencies; -import org.hibernate.engine.spi.EntityEntry; import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.engine.spi.Status; @@ -100,13 +99,11 @@ public void checkNoUnresolvedActionsAfterOperation() throws PropertyValueExcepti LOG.trace( "No entity insert actions have non-nullable, transient entity dependencies." ); } else { - final AbstractEntityInsertAction firstDependentAction = - dependenciesByAction.keySet().iterator().next(); + final var firstDependentAction = dependenciesByAction.keySet().iterator().next(); logCannotResolveNonNullableTransientDependencies( firstDependentAction.getSession() ); - final NonNullableTransientDependencies nonNullableTransientDependencies = - dependenciesByAction.get( firstDependentAction ); + final var nonNullableTransientDependencies = dependenciesByAction.get( firstDependentAction ); final Object firstTransientDependency = nonNullableTransientDependencies.getNonNullableTransientEntities().iterator().next(); final String firstPropertyPath = @@ -127,7 +124,7 @@ public void checkNoUnresolvedActionsAfterOperation() throws PropertyValueExcepti } private void logCannotResolveNonNullableTransientDependencies(SharedSessionContractImplementor session) { - for ( Map.Entry> entry : dependentActionsByTransientEntity.entrySet() ) { + for ( var entry : dependentActionsByTransientEntity.entrySet() ) { final Object transientEntity = entry.getKey(); final String transientEntityName = session.guessEntityName( transientEntity ); final Object transientEntityId = @@ -137,7 +134,7 @@ private void logCannotResolveNonNullableTransientDependencies(SharedSessionContr final String transientEntityString = infoString( transientEntityName, transientEntityId ); final Set dependentEntityStrings = new TreeSet<>(); final Set nonNullableTransientPropertyPaths = new TreeSet<>(); - for ( AbstractEntityInsertAction dependentAction : entry.getValue() ) { + for ( var dependentAction : entry.getValue() ) { dependentEntityStrings.add( infoString( dependentAction.getEntityName(), dependentAction.getId() ) ); for ( String path : dependenciesByAction.get( dependentAction ).getNonNullableTransientPropertyPaths( transientEntity ) ) { final String fullPath = dependentAction.getEntityName() + '.' + path; @@ -163,7 +160,7 @@ public boolean isEmpty() { private void addDependenciesByTransientEntity(AbstractEntityInsertAction insert, NonNullableTransientDependencies dependencies) { for ( Object transientEntity : dependencies.getNonNullableTransientEntities() ) { - Set dependentActions = dependentActionsByTransientEntity.get( transientEntity ); + var dependentActions = dependentActionsByTransientEntity.get( transientEntity ); if ( dependentActions == null ) { dependentActions = new IdentitySet<>(); dependentActionsByTransientEntity.put( transientEntity, dependentActions ); @@ -183,15 +180,16 @@ private void addDependenciesByTransientEntity(AbstractEntityInsertAction insert, * @throws IllegalArgumentException if {@code managedEntity} did not have managed or read-only status. */ public Set resolveDependentActions(Object managedEntity, SessionImplementor session) { - final EntityEntry entityEntry = session.getPersistenceContextInternal().getEntry( managedEntity ); - if ( entityEntry.getStatus() != Status.MANAGED && entityEntry.getStatus() != Status.READ_ONLY ) { + final var entityEntry = session.getPersistenceContextInternal().getEntry( managedEntity ); + final Status status = entityEntry.getStatus(); + if ( status != Status.MANAGED && status != Status.READ_ONLY ) { throw new IllegalArgumentException( "EntityEntry did not have status MANAGED or READ_ONLY: " + entityEntry ); } final boolean traceEnabled = LOG.isTraceEnabled(); // Find out if there are any unresolved insertions that are waiting for the // specified entity to be resolved. - final Set dependentActions = dependentActionsByTransientEntity.remove( managedEntity ); + final var dependentActions = dependentActionsByTransientEntity.remove( managedEntity ); if ( dependentActions == null ) { if ( traceEnabled ) { LOG.tracev( @@ -202,7 +200,7 @@ public Set resolveDependentActions(Object managedEnt return emptySet(); } else { - final Set resolvedActions = new IdentitySet<>( ); + final Set resolvedActions = new IdentitySet<>(); if ( traceEnabled ) { LOG.tracev( "Unresolved inserts before resolving [{0}]: [{1}]", @@ -210,7 +208,7 @@ public Set resolveDependentActions(Object managedEnt toString() ); } - for ( AbstractEntityInsertAction dependentAction : dependentActions ) { + for ( var dependentAction : dependentActions ) { if ( traceEnabled ) { LOG.tracev( "Resolving insert [{0}] dependency on [{1}]", @@ -218,7 +216,7 @@ public Set resolveDependentActions(Object managedEnt infoString( entityEntry.getEntityName(), entityEntry.getId() ) ); } - final NonNullableTransientDependencies dependencies = dependenciesByAction.get( dependentAction ); + final var dependencies = dependenciesByAction.get( dependentAction ); dependencies.resolveNonNullableTransientEntity( managedEntity ); if ( dependencies.isEmpty() ) { if ( traceEnabled ) { @@ -254,9 +252,8 @@ public void clear() { @Override public String toString() { - final StringBuilder sb = new StringBuilder( getClass().getSimpleName() ) - .append( '[' ); - for ( Map.Entry entry : dependenciesByAction.entrySet() ) { + final var sb = new StringBuilder( getClass().getSimpleName() ).append( '[' ); + for ( var entry : dependenciesByAction.entrySet() ) { final AbstractEntityInsertAction insert = entry.getKey(); final NonNullableTransientDependencies dependencies = entry.getValue(); sb.append( "[insert=" ) @@ -297,12 +294,12 @@ public static UnresolvedEntityInsertActions deserialize( ObjectInputStream ois, EventSource session) throws IOException, ClassNotFoundException { - final UnresolvedEntityInsertActions rtn = new UnresolvedEntityInsertActions(); + final var rtn = new UnresolvedEntityInsertActions(); final int queueSize = ois.readInt(); LOG.tracev( "Starting deserialization of [{0}] unresolved insert entries", queueSize ); for ( int i = 0; i < queueSize; i++ ) { - final AbstractEntityInsertAction unresolvedAction = (AbstractEntityInsertAction) ois.readObject(); + final var unresolvedAction = (AbstractEntityInsertAction) ois.readObject(); unresolvedAction.afterDeserialize( session ); rtn.addUnresolvedEntityInsertAction( unresolvedAction, From c908efa45b7658faf18bcd3e08fc410ff67c53f7 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Mon, 18 Aug 2025 18:57:14 +1000 Subject: [PATCH 2/4] consistently reuse local vars in actions --- .../internal/AbstractEntityInsertAction.java | 24 +++++++++++-------- .../action/internal/EntityDeleteAction.java | 20 +++++++++------- .../internal/EntityIdentityInsertAction.java | 9 +++---- .../action/internal/EntityInsertAction.java | 11 +++++---- .../action/internal/EntityUpdateAction.java | 10 ++++---- 5 files changed, 42 insertions(+), 32 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/action/internal/AbstractEntityInsertAction.java b/hibernate-core/src/main/java/org/hibernate/action/internal/AbstractEntityInsertAction.java index 599797633038..b34d5cd9179a 100644 --- a/hibernate-core/src/main/java/org/hibernate/action/internal/AbstractEntityInsertAction.java +++ b/hibernate-core/src/main/java/org/hibernate/action/internal/AbstractEntityInsertAction.java @@ -127,24 +127,27 @@ protected final void nullifyTransientReferencesIfNotAlready() { */ public final void makeEntityManaged() { nullifyTransientReferencesIfNotAlready(); - final Object version = getVersion( getState(), getPersister() ); + final var persister = getPersister(); + final var key = getEntityKey(); + final Object[] state = getState(); + final Object version = getVersion( state, persister ); final var persistenceContext = getSession().getPersistenceContextInternal(); - final var entityHolder = persistenceContext.addEntityHolder( getEntityKey(), getInstance() ); + final var entityHolder = persistenceContext.addEntityHolder( key, getInstance() ); final var entityEntry = persistenceContext.addEntry( getInstance(), - getPersister().isMutable() ? Status.MANAGED : Status.READ_ONLY, - getState(), + persister.isMutable() ? Status.MANAGED : Status.READ_ONLY, + state, getRowId(), - getEntityKey().getIdentifier(), + key.getIdentifier(), version, LockMode.WRITE, isExecuted, - getPersister(), + persister, isVersionIncrementDisabled ); entityHolder.setEntityEntry( entityEntry ); if ( isEarlyInsert() ) { - addCollectionsByKeyToPersistenceContext( persistenceContext, getState() ); + addCollectionsByKeyToPersistenceContext( persistenceContext, state ); } } @@ -270,7 +273,8 @@ protected void handleNaturalIdPreSaveNotifications() { * @param generatedId The generated entity identifier */ public void handleNaturalIdPostSaveNotifications(Object generatedId) { - final var naturalIdMapping = getPersister().getNaturalIdMapping(); + final var persister = getPersister(); + final var naturalIdMapping = persister.getNaturalIdMapping(); if ( naturalIdMapping != null ) { final Object naturalIdValues = naturalIdMapping.extractNaturalIdFromEntityState( state ); final var resolutions = getNaturalIdResolutions(); @@ -279,7 +283,7 @@ public void handleNaturalIdPostSaveNotifications(Object generatedId) { resolutions.manageLocalResolution( generatedId, naturalIdValues, - getPersister(), + persister, CachedNaturalIdValueSource.INSERT ); } @@ -288,7 +292,7 @@ public void handleNaturalIdPostSaveNotifications(Object generatedId) { generatedId, naturalIdValues, null, - getPersister(), + persister, CachedNaturalIdValueSource.INSERT ); } diff --git a/hibernate-core/src/main/java/org/hibernate/action/internal/EntityDeleteAction.java b/hibernate-core/src/main/java/org/hibernate/action/internal/EntityDeleteAction.java index 7d5a45a12697..935e8031ee33 100644 --- a/hibernate-core/src/main/java/org/hibernate/action/internal/EntityDeleteAction.java +++ b/hibernate-core/src/main/java/org/hibernate/action/internal/EntityDeleteAction.java @@ -112,7 +112,7 @@ public void execute() throws HibernateException { .removeLocalResolution( getId(), naturalIdMapping.extractNaturalIdFromEntityState( state ), - getPersister() + persister ); } @@ -139,20 +139,21 @@ public void execute() throws HibernateException { postDeleteUnloaded( id, persister, session, ck ); } - final var statistics = getSession().getFactory().getStatistics(); + final var statistics = session.getFactory().getStatistics(); if ( statistics.isStatisticsEnabled() && !veto ) { - statistics.deleteEntity( getPersister().getEntityName() ); + statistics.deleteEntity( persister.getEntityName() ); } } protected Object getCurrentVersion() { - return getPersister().isVersionPropertyGenerated() - // skip if we're deleting an unloaded proxy, no need for the version - && isInstanceLoaded() + final var persister = getPersister(); + return persister.isVersionPropertyGenerated() + // skip if we're deleting an unloaded proxy, no need for the version + && isInstanceLoaded() // we need to grab the version value from the entity, otherwise // we have issues with generated-version entities that may have // multiple actions queued during the same flush - ? getPersister().getVersion( getInstance() ) + ? persister.getVersion( getInstance() ) : version; } @@ -287,13 +288,14 @@ protected void unlockCacheItem() { protected void removeCacheItem(Object ck) { final var persister = getPersister(); if ( persister.canWriteToCache() ) { - persister.getCacheAccessStrategy().remove( getSession(), ck ); + final var cache = persister.getCacheAccessStrategy(); + cache.remove( getSession(), ck ); final var statistics = getSession().getFactory().getStatistics(); if ( statistics.isStatisticsEnabled() ) { statistics.entityCacheRemove( StatsHelper.getRootEntityRole( persister ), - getPersister().getCacheAccessStrategy().getRegion().getName() + cache.getRegion().getName() ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/action/internal/EntityIdentityInsertAction.java b/hibernate-core/src/main/java/org/hibernate/action/internal/EntityIdentityInsertAction.java index 6e53e6a3a031..cc2b6e5fc4cd 100644 --- a/hibernate-core/src/main/java/org/hibernate/action/internal/EntityIdentityInsertAction.java +++ b/hibernate-core/src/main/java/org/hibernate/action/internal/EntityIdentityInsertAction.java @@ -79,9 +79,10 @@ public void execute() throws HibernateException { final var eventMonitor = session.getEventMonitor(); final var event = eventMonitor.beginEntityInsertEvent(); boolean success = false; + final Object[] state = getState(); final GeneratedValues generatedValues; try { - generatedValues = persister.getInsertCoordinator().insert( instance, getState(), session ); + generatedValues = persister.getInsertCoordinator().insert( instance, state, session ); generatedId = castNonNull( generatedValues ).getGeneratedValue( persister.getIdentifierMapping() ); success = true; } @@ -96,12 +97,12 @@ public void execute() throws HibernateException { } } if ( persister.hasInsertGeneratedProperties() ) { - persister.processInsertGeneratedProperties( generatedId, instance, getState(), generatedValues, session ); + persister.processInsertGeneratedProperties( generatedId, instance, state, generatedValues, session ); } //need to do that here rather than in the save event listener to let //the post insert events to have an id-filled entity when IDENTITY is used (EJB3) persister.setIdentifier( instance, generatedId, session ); - persistenceContext.registerInsertedKey( getPersister(), generatedId ); + persistenceContext.registerInsertedKey( persister, generatedId ); entityKey = session.generateEntityKey( generatedId, persister ); persistenceContext.checkUniqueness( entityKey, getInstance() ); } @@ -118,7 +119,7 @@ public void execute() throws HibernateException { final var statistics = session.getFactory().getStatistics(); if ( statistics.isStatisticsEnabled() && !isVeto() ) { - statistics.insertEntity( getPersister().getEntityName() ); + statistics.insertEntity( persister.getEntityName() ); } markExecuted(); diff --git a/hibernate-core/src/main/java/org/hibernate/action/internal/EntityInsertAction.java b/hibernate-core/src/main/java/org/hibernate/action/internal/EntityInsertAction.java index 5ffd333838c5..4b5e222a74c6 100644 --- a/hibernate-core/src/main/java/org/hibernate/action/internal/EntityInsertAction.java +++ b/hibernate-core/src/main/java/org/hibernate/action/internal/EntityInsertAction.java @@ -116,7 +116,7 @@ public void execute() throws HibernateException { } entry.postInsert( getState() ); handleGeneratedProperties( entry, generatedValues, persistenceContext ); - persistenceContext.registerInsertedKey( persister, getId() ); + persistenceContext.registerInsertedKey( persister, id ); addCollectionsByKeyToPersistenceContext( persistenceContext, getState() ); } putCacheIfNecessary(); @@ -136,16 +136,17 @@ private void handleGeneratedProperties( GeneratedValues generatedValues, PersistenceContext persistenceContext) { final var persister = getPersister(); + final Object[] state = getState(); if ( persister.hasInsertGeneratedProperties() ) { final Object instance = getInstance(); - persister.processInsertGeneratedProperties( getId(), instance, getState(), generatedValues, getSession() ); + persister.processInsertGeneratedProperties( getId(), instance, state, generatedValues, getSession() ); if ( persister.isVersionPropertyGenerated() ) { - version = Versioning.getVersion( getState(), persister ); + version = Versioning.getVersion( state, persister ); } - entry.postUpdate( instance, getState(), version ); + entry.postUpdate( instance, state, version ); } else if ( persister.isVersionPropertyGenerated() ) { - version = Versioning.getVersion( getState(), persister ); + version = Versioning.getVersion( state, persister ); entry.postInsert( version ); } // Process row-id values when available early by replacing the entity entry diff --git a/hibernate-core/src/main/java/org/hibernate/action/internal/EntityUpdateAction.java b/hibernate-core/src/main/java/org/hibernate/action/internal/EntityUpdateAction.java index 83def5c4be45..57651fcf2984 100644 --- a/hibernate-core/src/main/java/org/hibernate/action/internal/EntityUpdateAction.java +++ b/hibernate-core/src/main/java/org/hibernate/action/internal/EntityUpdateAction.java @@ -87,7 +87,8 @@ public EntityUpdateAction( previousNaturalIdValues = null; } else { - previousNaturalIdValues = determinePreviousNaturalIdValues( persister, naturalIdMapping, id, previousState, session ); + previousNaturalIdValues = + determinePreviousNaturalIdValues( persister, naturalIdMapping, id, previousState, session ); session.getPersistenceContextInternal().getNaturalIdResolutions().manageLocalResolution( id, naturalIdMapping.extractNaturalIdFromEntityState( state ), @@ -223,7 +224,7 @@ else if ( session.getCacheMode().isPutEnabled() ) { if ( put && statistics.isStatisticsEnabled() ) { statistics.entityCachePut( StatsHelper.getRootEntityRole( persister ), - getPersister().getCacheAccessStrategy().getRegion().getName() + persister.getCacheAccessStrategy().getRegion().getName() ); } } @@ -439,18 +440,19 @@ protected void cacheAfterUpdate(EntityDataAccess cache, Object ck, SharedSession put = cache.afterUpdate( session, ck, cacheEntry, nextVersion, previousVersion, lock ); } finally { + final var persister = getPersister(); eventMonitor.completeCachePutEvent( cachePutEvent, session, cache, - getPersister(), + persister, put, EventMonitor.CacheActionDescription.ENTITY_AFTER_UPDATE ); final var statistics = session.getFactory().getStatistics(); if ( put && statistics.isStatisticsEnabled() ) { statistics.entityCachePut( - StatsHelper.getRootEntityRole( getPersister() ), + StatsHelper.getRootEntityRole( persister ), cache.getRegion().getName() ); } From 4e9bcf8f88008712fd69dab517bb3ee4d62462c7 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Mon, 18 Aug 2025 18:59:37 +1000 Subject: [PATCH 3/4] use existing constant --- .../action/internal/BulkOperationCleanupAction.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/action/internal/BulkOperationCleanupAction.java b/hibernate-core/src/main/java/org/hibernate/action/internal/BulkOperationCleanupAction.java index 650b91d2f926..659fa7213ec0 100644 --- a/hibernate-core/src/main/java/org/hibernate/action/internal/BulkOperationCleanupAction.java +++ b/hibernate-core/src/main/java/org/hibernate/action/internal/BulkOperationCleanupAction.java @@ -26,6 +26,7 @@ import org.hibernate.sql.ast.tree.insert.InsertSelectStatement; import static java.util.Collections.addAll; +import static org.hibernate.internal.util.collections.ArrayHelper.EMPTY_STRING_ARRAY; /** * An {@link org.hibernate.engine.spi.ActionQueue} {@link Executable} for @@ -87,7 +88,7 @@ public BulkOperationCleanupAction(SharedSessionContractImplementor session, Enti } } - affectedTableSpaces = spacesList.toArray( new String[ 0 ] ); + affectedTableSpaces = spacesList.toArray( EMPTY_STRING_ARRAY ); } /** @@ -133,7 +134,7 @@ public BulkOperationCleanupAction(SharedSessionContractImplementor session, Set< } } ); - affectedTableSpaces = spacesList.toArray( new String[ 0 ] ); + affectedTableSpaces = spacesList.toArray( EMPTY_STRING_ARRAY ); } public static void schedule(SharedSessionContractImplementor session, SqmDmlStatement statement) { From 3c0c653767e4a383c05c2da9c0ca131d63ded567 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Mon, 18 Aug 2025 19:01:32 +1000 Subject: [PATCH 4/4] minor changes to BulkOperationCleanupAction --- .../internal/BulkOperationCleanupAction.java | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/action/internal/BulkOperationCleanupAction.java b/hibernate-core/src/main/java/org/hibernate/action/internal/BulkOperationCleanupAction.java index 659fa7213ec0..dd2148b7d076 100644 --- a/hibernate-core/src/main/java/org/hibernate/action/internal/BulkOperationCleanupAction.java +++ b/hibernate-core/src/main/java/org/hibernate/action/internal/BulkOperationCleanupAction.java @@ -108,7 +108,7 @@ public BulkOperationCleanupAction(SharedSessionContractImplementor session, Set< final LinkedHashSet spacesList = new LinkedHashSet<>( tableSpaces ); final var metamodel = session.getFactory().getMappingMetamodel(); - metamodel.forEachEntityDescriptor( (entityDescriptor) -> { + metamodel.forEachEntityDescriptor( entityDescriptor -> { final String[] entitySpaces = (String[]) entityDescriptor.getQuerySpaces(); if ( affectedEntity( tableSpaces, entitySpaces ) ) { addAll( spacesList, entitySpaces ); @@ -145,9 +145,7 @@ public static void schedule(SharedSessionContractImplementor session, SqmDmlStat } for ( var cteStatement : statement.getCteStatements() ) { if ( cteStatement.getCteDefinition() instanceof SqmDmlStatement dmlStatement ) { - entityPersisters.add( - metamodel.getEntityDescriptor( dmlStatement.getTarget().getEntityName() ) - ); + entityPersisters.add( metamodel.getEntityDescriptor( dmlStatement.getTarget().getEntityName() ) ); } } @@ -194,13 +192,14 @@ private boolean affectedEntity(Set affectedTableSpaces, Serializable[] checkT if ( affectedTableSpaces == null || affectedTableSpaces.isEmpty() ) { return true; } - - for ( Serializable checkTableSpace : checkTableSpaces ) { - if ( affectedTableSpaces.contains( checkTableSpace ) ) { - return true; + else { + for ( Serializable checkTableSpace : checkTableSpaces ) { + if ( affectedTableSpaces.contains( checkTableSpace ) ) { + return true; + } } + return false; } - return false; } @Override @@ -216,17 +215,17 @@ public BeforeTransactionCompletionProcess getBeforeTransactionCompletionProcess( @Override public AfterTransactionCompletionProcess getAfterTransactionCompletionProcess() { return (success, session) -> { - for ( EntityCleanup cleanup : entityCleanups ) { + for ( var cleanup : entityCleanups ) { cleanup.release(); } entityCleanups.clear(); - for ( NaturalIdCleanup cleanup : naturalIdCleanups ) { + for ( var cleanup : naturalIdCleanups ) { cleanup.release(); } naturalIdCleanups.clear(); - for ( CollectionCleanup cleanup : collectionCleanups ) { + for ( var cleanup : collectionCleanups ) { cleanup.release(); } collectionCleanups.clear();