Skip to content

Commit 8f9b998

Browse files
committed
minor code cleanups to Actions
1 parent a11ebde commit 8f9b998

13 files changed

+342
-330
lines changed

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

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public NonNullableTransientDependencies findNonNullableTransientEntities() {
113113
* @see #makeEntityManaged()
114114
*/
115115
protected final void nullifyTransientReferencesIfNotAlready() {
116-
if ( ! areTransientReferencesNullified ) {
116+
if ( !areTransientReferencesNullified ) {
117117
new ForeignKeys.Nullifier( getInstance(), false, isEarlyInsert(), getSession(), getPersister() )
118118
.nullifyTransientReferences( getState() );
119119
new Nullability( getSession() ).checkNullability( getState(), getPersister(), false );
@@ -210,28 +210,25 @@ protected void handleNaturalIdPreSaveNotifications() {
210210
*/
211211
public void handleNaturalIdPostSaveNotifications(Object generatedId) {
212212
final NaturalIdMapping naturalIdMapping = getPersister().getNaturalIdMapping();
213-
if ( naturalIdMapping == null ) {
214-
return;
215-
}
216-
217-
final Object naturalIdValues = naturalIdMapping.extractNaturalIdFromEntityState( state, getSession() );
218-
219-
if ( isEarlyInsert() ) {
220-
// with early insert, we still need to add a local (transactional) natural id cross-reference
221-
getSession().getPersistenceContextInternal().getNaturalIdResolutions().manageLocalResolution(
213+
if ( naturalIdMapping != null ) {
214+
final Object naturalIdValues = naturalIdMapping.extractNaturalIdFromEntityState( state, getSession() );
215+
if ( isEarlyInsert() ) {
216+
// with early insert, we still need to add a local (transactional) natural id cross-reference
217+
getSession().getPersistenceContextInternal().getNaturalIdResolutions().manageLocalResolution(
218+
generatedId,
219+
naturalIdValues,
220+
getPersister(),
221+
CachedNaturalIdValueSource.INSERT
222+
);
223+
}
224+
// after save, we need to manage the shared cache entries
225+
getSession().getPersistenceContextInternal().getNaturalIdResolutions().manageSharedResolution(
222226
generatedId,
223227
naturalIdValues,
228+
null,
224229
getPersister(),
225230
CachedNaturalIdValueSource.INSERT
226231
);
227232
}
228-
// after save, we need to manage the shared cache entries
229-
getSession().getPersistenceContextInternal().getNaturalIdResolutions().manageSharedResolution(
230-
generatedId,
231-
naturalIdValues,
232-
null,
233-
getPersister(),
234-
CachedNaturalIdValueSource.INSERT
235-
);
236233
}
237234
}

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

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
* @author Steve Ebersole
4747
*/
4848
public class BulkOperationCleanupAction implements Executable, Serializable {
49+
4950
private final Serializable[] affectedTableSpaces;
5051

5152
private final Set<EntityCleanup> entityCleanups = new HashSet<>();
@@ -63,7 +64,6 @@ public class BulkOperationCleanupAction implements Executable, Serializable {
6364
* @param affectedQueryables The affected entity persisters.
6465
*/
6566
public BulkOperationCleanupAction(SharedSessionContractImplementor session, EntityPersister... affectedQueryables) {
66-
final SessionFactoryImplementor factory = session.getFactory();
6767
final LinkedHashSet<String> spacesList = new LinkedHashSet<>();
6868
for ( EntityPersister persister : affectedQueryables ) {
6969
Collections.addAll( spacesList, (String[]) persister.getQuerySpaces() );
@@ -81,17 +81,14 @@ public BulkOperationCleanupAction(SharedSessionContractImplementor session, Enti
8181
);
8282
}
8383

84-
final MappingMetamodelImplementor mappingMetamodel = factory.getRuntimeMetamodels().getMappingMetamodel();
84+
final MappingMetamodelImplementor mappingMetamodel = session.getFactory().getRuntimeMetamodels().getMappingMetamodel();
8585
final Set<String> roles = mappingMetamodel.getCollectionRolesByEntityParticipant( persister.getEntityName() );
8686
if ( roles != null ) {
8787
for ( String role : roles ) {
8888
final CollectionPersister collectionPersister = mappingMetamodel.getCollectionDescriptor( role );
8989
if ( collectionPersister.hasCache() ) {
9090
collectionCleanups.add(
91-
new CollectionCleanup(
92-
collectionPersister.getCacheAccessStrategy(),
93-
session
94-
)
91+
new CollectionCleanup( collectionPersister.getCacheAccessStrategy(), session )
9592
);
9693
}
9794
}
@@ -103,11 +100,12 @@ public BulkOperationCleanupAction(SharedSessionContractImplementor session, Enti
103100

104101
/**
105102
* Constructs an action to cleanup "affected cache regions" based on a
106-
* set of affected table spaces. This differs from {@link #BulkOperationCleanupAction(SharedSessionContractImplementor, EntityPersister[])}
107-
* in that here we have the affected <strong>table names</strong>. From those
108-
* we deduce the entity persisters which are affected based on the defined
109-
* {@link EntityPersister#getQuerySpaces() table spaces}; and from there, we
110-
* determine the affected collection regions based on any collections
103+
* set of affected table spaces. This differs from
104+
* {@link #BulkOperationCleanupAction(SharedSessionContractImplementor, EntityPersister[])}
105+
* in that here we have the affected <strong>table names</strong>. From
106+
* those we deduce the entity persisters which are affected based on the
107+
* defined {@link EntityPersister#getQuerySpaces() table spaces}. Finally,
108+
* we determine the affected collection regions based on any collections
111109
* in which those entity persisters participate as elements/keys/etc.
112110
*
113111
* @param session The session to which this request is tied.
@@ -116,8 +114,7 @@ public BulkOperationCleanupAction(SharedSessionContractImplementor session, Enti
116114
public BulkOperationCleanupAction(SharedSessionContractImplementor session, Set<String> tableSpaces) {
117115
final LinkedHashSet<String> spacesList = new LinkedHashSet<>( tableSpaces );
118116

119-
final SessionFactoryImplementor factory = session.getFactory();
120-
final MappingMetamodelImplementor metamodel = factory.getRuntimeMetamodels().getMappingMetamodel();
117+
final MappingMetamodelImplementor metamodel = session.getFactory().getRuntimeMetamodels().getMappingMetamodel();
121118
metamodel.forEachEntityDescriptor( (entityDescriptor) -> {
122119
final String[] entitySpaces = (String[]) entityDescriptor.getQuerySpaces();
123120
if ( affectedEntity( tableSpaces, entitySpaces ) ) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* @author Gavin King
2929
*/
3030
public abstract class CollectionAction implements Executable, Serializable, Comparable<CollectionAction> {
31+
3132
private transient CollectionPersister persister;
3233
private transient SharedSessionContractImplementor session;
3334
private final PersistentCollection<?> collection;

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ public void execute() throws HibernateException {
4141
// this method is called when a new non-null collection is persisted
4242
// or when an existing (non-null) collection is moved to a new owner
4343
final PersistentCollection<?> collection = getCollection();
44-
4544
preRecreate();
4645
final SharedSessionContractImplementor session = getSession();
4746
getPersister().recreate( collection, getKey(), session);
@@ -56,19 +55,19 @@ public void execute() throws HibernateException {
5655
}
5756

5857
private void preRecreate() {
59-
getFastSessionServices()
60-
.eventListenerGroup_PRE_COLLECTION_RECREATE
61-
.fireLazyEventOnEachListener( this::newPreCollectionRecreateEvent, PreCollectionRecreateEventListener::onPreRecreateCollection );
58+
getFastSessionServices().eventListenerGroup_PRE_COLLECTION_RECREATE
59+
.fireLazyEventOnEachListener( this::newPreCollectionRecreateEvent,
60+
PreCollectionRecreateEventListener::onPreRecreateCollection );
6261
}
6362

6463
private PreCollectionRecreateEvent newPreCollectionRecreateEvent() {
6564
return new PreCollectionRecreateEvent( getPersister(), getCollection(), eventSource() );
6665
}
6766

6867
private void postRecreate() {
69-
getFastSessionServices()
70-
.eventListenerGroup_POST_COLLECTION_RECREATE
71-
.fireLazyEventOnEachListener( this::newPostCollectionRecreateEvent, PostCollectionRecreateEventListener::onPostRecreateCollection );
68+
getFastSessionServices().eventListenerGroup_POST_COLLECTION_RECREATE
69+
.fireLazyEventOnEachListener( this::newPostCollectionRecreateEvent,
70+
PostCollectionRecreateEventListener::onPostRecreateCollection );
7271
}
7372

7473
private PostCollectionRecreateEvent newPostCollectionRecreateEvent() {

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* The action for removing a collection
2222
*/
2323
public final class CollectionRemoveAction extends CollectionAction {
24+
2425
private final Object affectedOwner;
2526
private final boolean emptySnapshot;
2627

@@ -101,22 +102,18 @@ public CollectionRemoveAction(
101102
@Override
102103
public void execute() throws HibernateException {
103104
preRemove();
104-
105105
final SharedSessionContractImplementor session = getSession();
106-
107106
if ( !emptySnapshot ) {
108107
// an existing collection that was either nonempty or uninitialized
109108
// is replaced by null or a different collection
110109
// (if the collection is uninitialized, Hibernate has no way of
111110
// knowing if the collection is actually empty without querying the db)
112111
getPersister().remove( getKey(), session );
113112
}
114-
115113
final PersistentCollection<?> collection = getCollection();
116114
if ( collection != null ) {
117115
session.getPersistenceContextInternal().getCollectionEntry( collection ).afterAction( collection );
118116
}
119-
120117
evict();
121118
postRemove();
122119

@@ -127,9 +124,9 @@ public void execute() throws HibernateException {
127124
}
128125

129126
private void preRemove() {
130-
getFastSessionServices()
131-
.eventListenerGroup_PRE_COLLECTION_REMOVE
132-
.fireLazyEventOnEachListener( this::newPreCollectionRemoveEvent, PreCollectionRemoveEventListener::onPreRemoveCollection );
127+
getFastSessionServices().eventListenerGroup_PRE_COLLECTION_REMOVE
128+
.fireLazyEventOnEachListener( this::newPreCollectionRemoveEvent,
129+
PreCollectionRemoveEventListener::onPreRemoveCollection );
133130
}
134131

135132
private PreCollectionRemoveEvent newPreCollectionRemoveEvent() {
@@ -142,9 +139,9 @@ private PreCollectionRemoveEvent newPreCollectionRemoveEvent() {
142139
}
143140

144141
private void postRemove() {
145-
getFastSessionServices()
146-
.eventListenerGroup_POST_COLLECTION_REMOVE
147-
.fireLazyEventOnEachListener( this::newPostCollectionRemoveEvent, PostCollectionRemoveEventListener::onPostRemoveCollection );
142+
getFastSessionServices().eventListenerGroup_POST_COLLECTION_REMOVE
143+
.fireLazyEventOnEachListener( this::newPostCollectionRemoveEvent,
144+
PostCollectionRemoveEventListener::onPostRemoveCollection );
148145
}
149146

150147
private PostCollectionRemoveEvent newPostCollectionRemoveEvent() {

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* The action for updating a collection
2323
*/
2424
public final class CollectionUpdateAction extends CollectionAction {
25+
2526
private final boolean emptySnapshot;
2627

2728
/**
@@ -68,9 +69,8 @@ else if ( !affectedByFilters && collection.empty() ) {
6869
}
6970
else if ( collection.needsRecreate( persister ) ) {
7071
if ( affectedByFilters ) {
71-
throw new HibernateException(
72-
"cannot recreate collection while filter is enabled: " +
73-
MessageHelper.collectionInfoString( persister, collection, id, session )
72+
throw new HibernateException( "cannot recreate collection while filter is enabled: "
73+
+ MessageHelper.collectionInfoString( persister, collection, id, session )
7474
);
7575
}
7676
if ( !emptySnapshot ) {
@@ -95,9 +95,9 @@ else if ( collection.needsRecreate( persister ) ) {
9595
}
9696

9797
private void preUpdate() {
98-
getFastSessionServices()
99-
.eventListenerGroup_PRE_COLLECTION_UPDATE
100-
.fireLazyEventOnEachListener( this::newPreCollectionUpdateEvent, PreCollectionUpdateEventListener::onPreUpdateCollection );
98+
getFastSessionServices().eventListenerGroup_PRE_COLLECTION_UPDATE
99+
.fireLazyEventOnEachListener( this::newPreCollectionUpdateEvent,
100+
PreCollectionUpdateEventListener::onPreUpdateCollection );
101101
}
102102

103103
private PreCollectionUpdateEvent newPreCollectionUpdateEvent() {
@@ -109,9 +109,9 @@ private PreCollectionUpdateEvent newPreCollectionUpdateEvent() {
109109
}
110110

111111
private void postUpdate() {
112-
getFastSessionServices()
113-
.eventListenerGroup_POST_COLLECTION_UPDATE
114-
.fireLazyEventOnEachListener( this::newPostCollectionUpdateEvent, PostCollectionUpdateEventListener::onPostUpdateCollection );
112+
getFastSessionServices().eventListenerGroup_POST_COLLECTION_UPDATE
113+
.fireLazyEventOnEachListener( this::newPostCollectionUpdateEvent,
114+
PostCollectionUpdateEventListener::onPostUpdateCollection );
115115
}
116116

117117
private PostCollectionUpdateEvent newPostCollectionUpdateEvent() {

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

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,7 @@ public final Object getId() {
106106
}
107107

108108
public final DelayedPostInsertIdentifier getDelayedId() {
109-
return id instanceof DelayedPostInsertIdentifier
110-
? (DelayedPostInsertIdentifier) id
111-
: null;
109+
return id instanceof DelayedPostInsertIdentifier ? (DelayedPostInsertIdentifier) id : null;
112110
}
113111

114112
/**
@@ -157,13 +155,9 @@ public String toString() {
157155
public int compareTo(EntityAction action) {
158156
//sort first by entity name
159157
final int roleComparison = entityName.compareTo( action.entityName );
160-
if ( roleComparison != 0 ) {
161-
return roleComparison;
162-
}
163-
else {
164-
//then by id
165-
return persister.getIdentifierType().compare( id, action.id );
166-
}
158+
return roleComparison != 0 ? roleComparison
159+
//then by id
160+
: persister.getIdentifierType().compare( id, action.id );
167161
}
168162

169163
/**
@@ -180,7 +174,7 @@ public void afterDeserialize(SharedSessionContractImplementor session) {
180174
// guard against NullPointerException
181175
if ( session != null ) {
182176
this.session = session;
183-
this.persister = session.getFactory().getRuntimeMetamodels().getMappingMetamodel().getEntityDescriptor( entityName );
177+
this.persister = session.getFactory().getMappingMetamodel().getEntityDescriptor( entityName );
184178
this.instance = session.getPersistenceContext().getEntity( session.generateEntityKey( id, persister ) );
185179
}
186180
}

0 commit comments

Comments
 (0)