Skip to content

Commit a63ae70

Browse files
committed
use 'var' in actions
1 parent 1d00b97 commit a63ae70

16 files changed

+200
-262
lines changed

hibernate-core/src/main/java/org/hibernate/action/internal/AbstractEntityInsertAction.java

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,13 @@
1212
import org.hibernate.engine.internal.Nullability.NullabilityCheckType;
1313
import org.hibernate.engine.spi.CachedNaturalIdValueSource;
1414
import org.hibernate.engine.spi.CollectionKey;
15-
import org.hibernate.engine.spi.EntityEntry;
16-
import org.hibernate.engine.spi.EntityHolder;
1715
import org.hibernate.engine.spi.EntityKey;
16+
import org.hibernate.engine.spi.NaturalIdResolutions;
1817
import org.hibernate.engine.spi.PersistenceContext;
1918
import org.hibernate.engine.spi.Status;
2019
import org.hibernate.event.spi.EventSource;
21-
import org.hibernate.metamodel.mapping.AttributeMapping;
22-
import org.hibernate.metamodel.mapping.AttributeMappingsList;
23-
import org.hibernate.metamodel.mapping.EmbeddableMappingType;
24-
import org.hibernate.metamodel.mapping.NaturalIdMapping;
2520
import org.hibernate.metamodel.mapping.PluralAttributeMapping;
2621
import org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping;
27-
import org.hibernate.persister.collection.CollectionPersister;
2822
import org.hibernate.persister.entity.AbstractEntityPersister;
2923
import org.hibernate.persister.entity.EntityPersister;
3024

@@ -134,12 +128,9 @@ protected final void nullifyTransientReferencesIfNotAlready() {
134128
public final void makeEntityManaged() {
135129
nullifyTransientReferencesIfNotAlready();
136130
final Object version = getVersion( getState(), getPersister() );
137-
final PersistenceContext persistenceContextInternal = getSession().getPersistenceContextInternal();
138-
final EntityHolder entityHolder = persistenceContextInternal.addEntityHolder(
139-
getEntityKey(),
140-
getInstance()
141-
);
142-
final EntityEntry entityEntry = persistenceContextInternal.addEntry(
131+
final var persistenceContext = getSession().getPersistenceContextInternal();
132+
final var entityHolder = persistenceContext.addEntityHolder( getEntityKey(), getInstance() );
133+
final var entityEntry = persistenceContext.addEntry(
143134
getInstance(),
144135
getPersister().isMutable() ? Status.MANAGED : Status.READ_ONLY,
145136
getState(),
@@ -153,13 +144,13 @@ public final void makeEntityManaged() {
153144
);
154145
entityHolder.setEntityEntry( entityEntry );
155146
if ( isEarlyInsert() ) {
156-
addCollectionsByKeyToPersistenceContext( persistenceContextInternal, getState() );
147+
addCollectionsByKeyToPersistenceContext( persistenceContext, getState() );
157148
}
158149
}
159150

160151
protected void addCollectionsByKeyToPersistenceContext(PersistenceContext persistenceContext, Object[] objects) {
161152
for ( int i = 0; i < objects.length; i++ ) {
162-
final AttributeMapping attributeMapping = getPersister().getAttributeMapping( i );
153+
final var attributeMapping = getPersister().getAttributeMapping( i );
163154
if ( attributeMapping.isEmbeddedAttributeMapping() ) {
164155
visitEmbeddedAttributeMapping(
165156
attributeMapping.asEmbeddedAttributeMapping(),
@@ -182,13 +173,12 @@ private void visitEmbeddedAttributeMapping(
182173
Object object,
183174
PersistenceContext persistenceContext) {
184175
if ( object != null ) {
185-
final EmbeddableMappingType descriptor = attributeMapping.getEmbeddableTypeDescriptor();
186-
final EmbeddableMappingType.ConcreteEmbeddableType concreteEmbeddableType = descriptor.findSubtypeBySubclass(
187-
object.getClass().getName()
188-
);
189-
final AttributeMappingsList attributeMappings = descriptor.getAttributeMappings();
176+
final var descriptor = attributeMapping.getEmbeddableTypeDescriptor();
177+
final var concreteEmbeddableType =
178+
descriptor.findSubtypeBySubclass( object.getClass().getName() );
179+
final var attributeMappings = descriptor.getAttributeMappings();
190180
for ( int i = 0; i < attributeMappings.size(); i++ ) {
191-
final AttributeMapping attribute = attributeMappings.get( i );
181+
final var attribute = attributeMappings.get( i );
192182
if ( concreteEmbeddableType.declaresAttribute( attribute ) ) {
193183
if ( attribute.isPluralAttributeMapping() ) {
194184
addCollectionKey(
@@ -214,15 +204,15 @@ private void addCollectionKey(
214204
Object object,
215205
PersistenceContext persistenceContext) {
216206
if ( object instanceof PersistentCollection ) {
217-
final CollectionPersister collectionPersister = pluralAttributeMapping.getCollectionDescriptor();
207+
final var collectionPersister = pluralAttributeMapping.getCollectionDescriptor();
218208
final Object key = AbstractEntityPersister.getCollectionKey(
219209
collectionPersister,
220210
getInstance(),
221211
persistenceContext.getEntry( getInstance() ),
222212
getSession()
223213
);
224214
if ( key != null ) {
225-
final CollectionKey collectionKey = new CollectionKey( collectionPersister, key );
215+
final var collectionKey = new CollectionKey( collectionPersister, key );
226216
persistenceContext.addCollectionByKey( collectionKey, (PersistentCollection<?>) object );
227217
}
228218
}
@@ -243,13 +233,17 @@ protected void markExecuted() {
243233

244234
protected abstract Object getRowId();
245235

236+
private NaturalIdResolutions getNaturalIdResolutions() {
237+
return getSession().getPersistenceContextInternal().getNaturalIdResolutions();
238+
}
239+
246240
@Override
247241
public void afterDeserialize(EventSource session) {
248242
super.afterDeserialize( session );
249243
// IMPL NOTE: non-flushed changes code calls this method with session == null...
250244
// guard against NullPointerException
251245
if ( session != null ) {
252-
final EntityEntry entityEntry = session.getPersistenceContextInternal().getEntry( getInstance() );
246+
final var entityEntry = session.getPersistenceContextInternal().getEntry( getInstance() );
253247
this.state = entityEntry.getLoadedState();
254248
}
255249
}
@@ -259,9 +253,9 @@ public void afterDeserialize(EventSource session) {
259253
*/
260254
protected void handleNaturalIdPreSaveNotifications() {
261255
// before save, we need to add a natural id cross-reference to the persistence-context
262-
final NaturalIdMapping naturalIdMapping = getPersister().getNaturalIdMapping();
256+
final var naturalIdMapping = getPersister().getNaturalIdMapping();
263257
if ( naturalIdMapping != null ) {
264-
getSession().getPersistenceContextInternal().getNaturalIdResolutions().manageLocalResolution(
258+
getNaturalIdResolutions().manageLocalResolution(
265259
getId(),
266260
naturalIdMapping.extractNaturalIdFromEntityState( state ),
267261
getPersister(),
@@ -276,20 +270,21 @@ protected void handleNaturalIdPreSaveNotifications() {
276270
* @param generatedId The generated entity identifier
277271
*/
278272
public void handleNaturalIdPostSaveNotifications(Object generatedId) {
279-
final NaturalIdMapping naturalIdMapping = getPersister().getNaturalIdMapping();
273+
final var naturalIdMapping = getPersister().getNaturalIdMapping();
280274
if ( naturalIdMapping != null ) {
281275
final Object naturalIdValues = naturalIdMapping.extractNaturalIdFromEntityState( state );
276+
final var resolutions = getNaturalIdResolutions();
282277
if ( isEarlyInsert() ) {
283278
// with early insert, we still need to add a local (transactional) natural id cross-reference
284-
getSession().getPersistenceContextInternal().getNaturalIdResolutions().manageLocalResolution(
279+
resolutions.manageLocalResolution(
285280
generatedId,
286281
naturalIdValues,
287282
getPersister(),
288283
CachedNaturalIdValueSource.INSERT
289284
);
290285
}
291286
// after save, we need to manage the shared cache entries
292-
getSession().getPersistenceContextInternal().getNaturalIdResolutions().manageSharedResolution(
287+
resolutions.manageSharedResolution(
293288
generatedId,
294289
naturalIdValues,
295290
null,

hibernate-core/src/main/java/org/hibernate/action/internal/BulkOperationCleanupAction.java

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import java.io.Serializable;
88
import java.util.ArrayList;
9-
import java.util.Collections;
109
import java.util.HashSet;
1110
import java.util.LinkedHashSet;
1211
import java.util.List;
@@ -22,14 +21,12 @@
2221
import org.hibernate.cache.spi.access.SoftLock;
2322
import org.hibernate.engine.spi.SharedSessionContractImplementor;
2423
import org.hibernate.event.spi.EventSource;
25-
import org.hibernate.metamodel.spi.MappingMetamodelImplementor;
26-
import org.hibernate.persister.collection.CollectionPersister;
2724
import org.hibernate.persister.entity.EntityPersister;
2825
import org.hibernate.query.sqm.tree.SqmDmlStatement;
29-
import org.hibernate.query.sqm.tree.SqmQuery;
30-
import org.hibernate.query.sqm.tree.cte.SqmCteStatement;
3126
import org.hibernate.sql.ast.tree.insert.InsertSelectStatement;
3227

28+
import static java.util.Collections.addAll;
29+
3330
/**
3431
* An {@link org.hibernate.engine.spi.ActionQueue} {@link Executable} for
3532
* ensuring shared cache cleanup in relation to performed bulk HQL queries.
@@ -62,27 +59,25 @@ public class BulkOperationCleanupAction implements Executable, Serializable {
6259
*/
6360
public BulkOperationCleanupAction(SharedSessionContractImplementor session, EntityPersister... affectedQueryables) {
6461
final LinkedHashSet<String> spacesList = new LinkedHashSet<>();
65-
for ( EntityPersister persister : affectedQueryables ) {
66-
Collections.addAll( spacesList, (String[]) persister.getQuerySpaces() );
62+
for ( var persister : affectedQueryables ) {
63+
addAll( spacesList, (String[]) persister.getQuerySpaces() );
6764

6865
if ( persister.canWriteToCache() ) {
69-
final EntityDataAccess entityDataAccess = persister.getCacheAccessStrategy();
66+
final var entityDataAccess = persister.getCacheAccessStrategy();
7067
if ( entityDataAccess != null ) {
7168
entityCleanups.add( new EntityCleanup( entityDataAccess, session ) );
7269
}
7370
}
7471

7572
if ( persister.hasNaturalIdentifier() && persister.hasNaturalIdCache() ) {
76-
naturalIdCleanups.add(
77-
new NaturalIdCleanup( persister.getNaturalIdCacheAccessStrategy(), session )
78-
);
73+
naturalIdCleanups.add( new NaturalIdCleanup( persister.getNaturalIdCacheAccessStrategy(), session ) );
7974
}
8075

81-
final MappingMetamodelImplementor mappingMetamodel = session.getFactory().getMappingMetamodel();
82-
final Set<String> roles = mappingMetamodel.getCollectionRolesByEntityParticipant( persister.getEntityName() );
76+
final var mappingMetamodel = session.getFactory().getMappingMetamodel();
77+
final var roles = mappingMetamodel.getCollectionRolesByEntityParticipant( persister.getEntityName() );
8378
if ( roles != null ) {
8479
for ( String role : roles ) {
85-
final CollectionPersister collectionPersister = mappingMetamodel.getCollectionDescriptor( role );
80+
final var collectionPersister = mappingMetamodel.getCollectionDescriptor( role );
8681
if ( collectionPersister.hasCache() ) {
8782
collectionCleanups.add(
8883
new CollectionCleanup( collectionPersister.getCacheAccessStrategy(), session )
@@ -92,7 +87,7 @@ public BulkOperationCleanupAction(SharedSessionContractImplementor session, Enti
9287
}
9388
}
9489

95-
this.affectedTableSpaces = spacesList.toArray( new String[ 0 ] );
90+
affectedTableSpaces = spacesList.toArray( new String[ 0 ] );
9691
}
9792

9893
/**
@@ -111,11 +106,11 @@ public BulkOperationCleanupAction(SharedSessionContractImplementor session, Enti
111106
public BulkOperationCleanupAction(SharedSessionContractImplementor session, Set<String> tableSpaces) {
112107
final LinkedHashSet<String> spacesList = new LinkedHashSet<>( tableSpaces );
113108

114-
final MappingMetamodelImplementor metamodel = session.getFactory().getMappingMetamodel();
109+
final var metamodel = session.getFactory().getMappingMetamodel();
115110
metamodel.forEachEntityDescriptor( (entityDescriptor) -> {
116111
final String[] entitySpaces = (String[]) entityDescriptor.getQuerySpaces();
117112
if ( affectedEntity( tableSpaces, entitySpaces ) ) {
118-
Collections.addAll( spacesList, entitySpaces );
113+
addAll( spacesList, entitySpaces );
119114

120115
if ( entityDescriptor.canWriteToCache() ) {
121116
entityCleanups.add( new EntityCleanup( entityDescriptor.getCacheAccessStrategy(), session ) );
@@ -124,10 +119,10 @@ public BulkOperationCleanupAction(SharedSessionContractImplementor session, Set<
124119
naturalIdCleanups.add( new NaturalIdCleanup( entityDescriptor.getNaturalIdCacheAccessStrategy(), session ) );
125120
}
126121

127-
final Set<String> roles = metamodel.getCollectionRolesByEntityParticipant( entityDescriptor.getEntityName() );
122+
final var roles = metamodel.getCollectionRolesByEntityParticipant( entityDescriptor.getEntityName() );
128123
if ( roles != null ) {
129124
for ( String role : roles ) {
130-
final CollectionPersister collectionDescriptor = metamodel.getCollectionDescriptor( role );
125+
final var collectionDescriptor = metamodel.getCollectionDescriptor( role );
131126
if ( collectionDescriptor.hasCache() ) {
132127
collectionCleanups.add(
133128
new CollectionCleanup( collectionDescriptor.getCacheAccessStrategy(), session )
@@ -138,18 +133,17 @@ public BulkOperationCleanupAction(SharedSessionContractImplementor session, Set<
138133
}
139134
} );
140135

141-
this.affectedTableSpaces = spacesList.toArray( new String[ 0 ] );
136+
affectedTableSpaces = spacesList.toArray( new String[ 0 ] );
142137
}
143138

144139
public static void schedule(SharedSessionContractImplementor session, SqmDmlStatement<?> statement) {
145140
final List<EntityPersister> entityPersisters = new ArrayList<>( 1 );
146-
final MappingMetamodelImplementor metamodel = session.getFactory().getMappingMetamodel();
141+
final var metamodel = session.getFactory().getMappingMetamodel();
147142
if ( !( statement instanceof InsertSelectStatement ) ) {
148143
entityPersisters.add( metamodel.getEntityDescriptor( statement.getTarget().getEntityName() ) );
149144
}
150-
for ( SqmCteStatement<?> cteStatement : statement.getCteStatements() ) {
151-
final SqmQuery<?> cteDefinition = cteStatement.getCteDefinition();
152-
if ( cteDefinition instanceof SqmDmlStatement<?> dmlStatement ) {
145+
for ( var cteStatement : statement.getCteStatements() ) {
146+
if ( cteStatement.getCteDefinition() instanceof SqmDmlStatement<?> dmlStatement ) {
153147
entityPersisters.add(
154148
metamodel.getEntityDescriptor( dmlStatement.getTarget().getEntityName() )
155149
);
@@ -160,7 +154,7 @@ public static void schedule(SharedSessionContractImplementor session, SqmDmlStat
160154
}
161155

162156
public static void schedule(SharedSessionContractImplementor session, EntityPersister... affectedQueryables) {
163-
final BulkOperationCleanupAction action = new BulkOperationCleanupAction( session, affectedQueryables );
157+
final var action = new BulkOperationCleanupAction( session, affectedQueryables );
164158
if ( session.isEventSource() ) {
165159
session.asEventSource().getActionQueue().addAction( action );
166160
}
@@ -170,7 +164,7 @@ public static void schedule(SharedSessionContractImplementor session, EntityPers
170164
}
171165

172166
public static void schedule(SharedSessionContractImplementor session, Set<String> affectedQueryables) {
173-
final BulkOperationCleanupAction action = new BulkOperationCleanupAction( session, affectedQueryables );
167+
final var action = new BulkOperationCleanupAction( session, affectedQueryables );
174168
if ( session.isEventSource() ) {
175169
session.asEventSource().getActionQueue().addAction( action );
176170
}

hibernate-core/src/main/java/org/hibernate/action/internal/CollectionAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ protected final EventSource getSession() {
136136

137137
protected final void evict() throws CacheException {
138138
if ( persister.hasCache() ) {
139-
final CollectionDataAccess cache = persister.getCacheAccessStrategy();
139+
final var cache = persister.getCacheAccessStrategy();
140140
final Object ck = cache.generateCacheKey(
141141
key,
142142
persister,

hibernate-core/src/main/java/org/hibernate/action/internal/CollectionRecreateAction.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,12 @@
66

77
import org.hibernate.HibernateException;
88
import org.hibernate.collection.spi.PersistentCollection;
9-
import org.hibernate.engine.spi.SharedSessionContractImplementor;
10-
import org.hibernate.event.monitor.spi.EventMonitor;
119
import org.hibernate.event.spi.EventSource;
12-
import org.hibernate.event.monitor.spi.DiagnosticEvent;
1310
import org.hibernate.event.spi.PostCollectionRecreateEvent;
1411
import org.hibernate.event.spi.PostCollectionRecreateEventListener;
1512
import org.hibernate.event.spi.PreCollectionRecreateEvent;
1613
import org.hibernate.event.spi.PreCollectionRecreateEventListener;
1714
import org.hibernate.persister.collection.CollectionPersister;
18-
import org.hibernate.stat.spi.StatisticsImplementor;
1915

2016
/**
2117
* The action for recreating a collection
@@ -41,13 +37,13 @@ public CollectionRecreateAction(
4137
public void execute() throws HibernateException {
4238
// this method is called when a new non-null collection is persisted
4339
// or when an existing (non-null) collection is moved to a new owner
44-
final PersistentCollection<?> collection = getCollection();
40+
final var collection = getCollection();
4541
preRecreate();
46-
final SharedSessionContractImplementor session = getSession();
47-
final CollectionPersister persister = getPersister();
42+
final var session = getSession();
43+
final var persister = getPersister();
4844
final Object key = getKey();
49-
final EventMonitor eventMonitor = session.getEventMonitor();
50-
final DiagnosticEvent event = eventMonitor.beginCollectionRecreateEvent();
45+
final var eventMonitor = session.getEventMonitor();
46+
final var event = eventMonitor.beginCollectionRecreateEvent();
5147
boolean success = false;
5248
try {
5349
persister.recreate( collection, key, session );
@@ -61,7 +57,7 @@ public void execute() throws HibernateException {
6157
evict();
6258
postRecreate();
6359

64-
final StatisticsImplementor statistics = session.getFactory().getStatistics();
60+
final var statistics = session.getFactory().getStatistics();
6561
if ( statistics.isStatisticsEnabled() ) {
6662
statistics.recreateCollection( persister.getRole() );
6763
}

hibernate-core/src/main/java/org/hibernate/action/internal/CollectionRemoveAction.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,12 @@
77
import org.hibernate.AssertionFailure;
88
import org.hibernate.HibernateException;
99
import org.hibernate.collection.spi.PersistentCollection;
10-
import org.hibernate.engine.spi.SharedSessionContractImplementor;
11-
import org.hibernate.event.monitor.spi.EventMonitor;
1210
import org.hibernate.event.spi.EventSource;
13-
import org.hibernate.event.monitor.spi.DiagnosticEvent;
1411
import org.hibernate.event.spi.PostCollectionRemoveEvent;
1512
import org.hibernate.event.spi.PostCollectionRemoveEventListener;
1613
import org.hibernate.event.spi.PreCollectionRemoveEvent;
1714
import org.hibernate.event.spi.PreCollectionRemoveEventListener;
1815
import org.hibernate.persister.collection.CollectionPersister;
19-
import org.hibernate.stat.spi.StatisticsImplementor;
2016

2117
/**
2218
* The action for removing a collection
@@ -104,16 +100,16 @@ public CollectionRemoveAction(
104100
@Override
105101
public void execute() throws HibernateException {
106102
preRemove();
107-
final SharedSessionContractImplementor session = getSession();
103+
final var session = getSession();
108104
if ( !emptySnapshot ) {
109105
// an existing collection that was either nonempty or uninitialized
110106
// is replaced by null or a different collection
111107
// (if the collection is uninitialized, Hibernate has no way of
112108
// knowing if the collection is actually empty without querying the db)
113-
final CollectionPersister persister = getPersister();
109+
final var persister = getPersister();
114110
final Object key = getKey();
115-
final EventMonitor eventMonitor = session.getEventMonitor();
116-
final DiagnosticEvent event = eventMonitor.beginCollectionRemoveEvent();
111+
final var eventMonitor = session.getEventMonitor();
112+
final var event = eventMonitor.beginCollectionRemoveEvent();
117113
boolean success = false;
118114
try {
119115
persister.remove( key, session );
@@ -131,7 +127,7 @@ public void execute() throws HibernateException {
131127
evict();
132128
postRemove();
133129

134-
final StatisticsImplementor statistics = session.getFactory().getStatistics();
130+
final var statistics = session.getFactory().getStatistics();
135131
if ( statistics.isStatisticsEnabled() ) {
136132
statistics.removeCollection( getPersister().getRole() );
137133
}

0 commit comments

Comments
 (0)