Skip to content

Commit f1153be

Browse files
committed
typesafe logging in cascade processing
1 parent 699730c commit f1153be

File tree

5 files changed

+96
-62
lines changed

5 files changed

+96
-62
lines changed

hibernate-core/src/main/java/org/hibernate/engine/config/internal/ConfigurationServiceImpl.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ public void injectServices(ServiceRegistryImplementor serviceRegistry) {
8686
.classForName( candidate.toString() );
8787
}
8888
catch ( ClassLoadingException e ) {
89-
CORE_LOGGER.debugf( "Unable to locate %s implementation class %s", expected.getName(), candidate.toString() );
89+
CORE_LOGGER.debugf( "Unable to locate %s implementation class %s",
90+
expected.getName(), candidate.toString() );
9091
target = null;
9192
}
9293
}
@@ -95,10 +96,8 @@ public void injectServices(ServiceRegistryImplementor serviceRegistry) {
9596
return target.newInstance();
9697
}
9798
catch ( Exception e ) {
98-
CORE_LOGGER.debugf(
99-
"Unable to instantiate %s class %s", expected.getName(),
100-
target.getName()
101-
);
99+
CORE_LOGGER.debugf( "Unable to instantiate %s class %s",
100+
expected.getName(), target.getName() );
102101
}
103102
}
104103
return null;

hibernate-core/src/main/java/org/hibernate/engine/internal/Cascade.java

Lines changed: 41 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,14 @@
66

77
import java.util.ArrayList;
88
import java.util.Collection;
9-
import java.util.Iterator;
109
import java.util.List;
1110

1211
import org.hibernate.HibernateException;
1312
import org.hibernate.annotations.OnDeleteAction;
1413
import org.hibernate.collection.spi.PersistentCollection;
1514
import org.hibernate.engine.spi.CascadeStyle;
1615
import org.hibernate.engine.spi.CascadingAction;
17-
import org.hibernate.engine.spi.CollectionEntry;
1816
import org.hibernate.engine.spi.EntityEntry;
19-
import org.hibernate.engine.spi.PersistenceContext;
2017
import org.hibernate.engine.spi.Status;
2118
import org.hibernate.event.spi.DeleteContext;
2219
import org.hibernate.event.spi.EventSource;
@@ -82,7 +79,7 @@ public static <T> void cascade(
8279
if ( action.anythingToCascade( persister ) ) { // performance opt
8380
final boolean traceEnabled = CORE_LOGGER.isTraceEnabled();
8481
if ( traceEnabled ) {
85-
CORE_LOGGER.tracev( "Processing cascade {0} for: {1}", action, persister.getEntityName() );
82+
CORE_LOGGER.processingCascade( action, persister.getEntityName() );
8683
}
8784
final var bytecodeEnhancement = persister.getBytecodeEnhancementMetadata();
8885
final EntityEntry entry;
@@ -100,11 +97,11 @@ public static <T> void cascade(
10097

10198
final Type[] types = persister.getPropertyTypes();
10299
final String[] propertyNames = persister.getPropertyNames();
103-
final CascadeStyle[] cascadeStyles = persister.getPropertyCascadeStyles();
100+
final var cascadeStyles = persister.getPropertyCascadeStyles();
104101
final boolean hasUninitializedLazyProperties = bytecodeEnhancement.hasUnFetchedAttributes( parent );
105102

106103
for ( int i = 0; i < types.length; i++) {
107-
final CascadeStyle style = cascadeStyles[ i ];
104+
final var style = cascadeStyles[ i ];
108105
final String propertyName = propertyNames[ i ];
109106
final Type type = types[i];
110107
final boolean isUninitializedProperty =
@@ -192,7 +189,7 @@ && isLogicalOneToOne( type ) ) {
192189
}
193190

194191
if ( traceEnabled ) {
195-
CORE_LOGGER.tracev( "Done processing cascade {0} for: {1}", action, persister.getEntityName() );
192+
CORE_LOGGER.doneProcessingCascade( action, persister.getEntityName() );
196193
}
197194
}
198195
}
@@ -288,8 +285,8 @@ private static <T> void cascadeLogicalOneToOneOrphanRemoval(
288285
if ( style.hasOrphanDelete() && action.deleteOrphans() ) {
289286
// value is orphaned if loaded state for this property shows not null
290287
// because it is currently null.
291-
final PersistenceContext persistenceContext = eventSource.getPersistenceContextInternal();
292-
final EntityEntry entry = persistenceContext.getEntry( parent );
288+
final var persistenceContext = eventSource.getPersistenceContextInternal();
289+
final var entry = persistenceContext.getEntry( parent );
293290
if ( entry != null && entry.getStatus() != Status.SAVING ) {
294291
Object loadedValue;
295292
if ( componentPath == null ) {
@@ -324,13 +321,11 @@ private static <T> void cascadeLogicalOneToOneOrphanRemoval(
324321
// entity is managed (without first nulling and manually flushing).
325322
if ( child == null || loadedValue != null && child != loadedValue ) {
326323
EntityEntry valueEntry = persistenceContext.getEntry( loadedValue );
327-
328324
if ( valueEntry == null && isHibernateProxy( loadedValue ) ) {
329325
// un-proxy and re-associate for cascade operation
330326
// useful for @OneToOne defined as FetchType.LAZY
331327
loadedValue = persistenceContext.unproxyAndReassociate( loadedValue );
332328
valueEntry = persistenceContext.getEntry( loadedValue );
333-
334329
// HHH-11965
335330
// Should the unwrapped proxy value be equal via reference to the entity's property value
336331
// provided by the 'child' variable, we should not trigger the orphan removal of the
@@ -342,15 +337,12 @@ private static <T> void cascadeLogicalOneToOneOrphanRemoval(
342337
}
343338

344339
if ( valueEntry != null ) {
345-
final EntityPersister persister = valueEntry.getPersister();
340+
final var persister = valueEntry.getPersister();
346341
final String entityName = persister.getEntityName();
347342
if ( CORE_LOGGER.isTraceEnabled() ) {
348-
CORE_LOGGER.tracev(
349-
"Deleting orphaned entity instance: {0}",
350-
infoString( entityName, persister.getIdentifier( loadedValue, eventSource ) )
351-
);
343+
CORE_LOGGER.deletingOrphan(
344+
infoString( entityName, persister.getIdentifier( loadedValue, eventSource ) ) );
352345
}
353-
354346
if ( isForeignKeyToParent( type ) ) {
355347
// If FK direction is to-parent, we must remove the orphan *before* the queued update(s)
356348
// occur. Otherwise, replacing the association on a managed entity, without manually
@@ -487,10 +479,10 @@ private static <T> void cascadeCollection(
487479
final CascadeStyle style,
488480
final T anything,
489481
final CollectionType type) {
490-
final CollectionPersister persister =
482+
final var persister =
491483
eventSource.getFactory().getMappingMetamodel()
492484
.getCollectionDescriptor( type.getRole() );
493-
final Type elemType = persister.getElementType();
485+
final var elemType = persister.getElementType();
494486
//cascade to current collection elements
495487
if ( elemType instanceof EntityType || elemType instanceof AnyType || elemType instanceof ComponentType ) {
496488
cascadeCollectionElements(
@@ -530,15 +522,18 @@ private static <T> void cascadeToOne(
530522
final List<String> componentPath) {
531523
if ( style.reallyDoCascade( action ) ) {
532524
//not really necessary, but good for consistency...
533-
final PersistenceContext persistenceContext = eventSource.getPersistenceContextInternal();
525+
final var persistenceContext = eventSource.getPersistenceContextInternal();
534526
persistenceContext.addChildParent( child, parent );
527+
final String childEntityName =
528+
type instanceof EntityType entityType
529+
? entityType.getAssociatedEntityName()
530+
: null;
531+
CORE_LOGGER.cascading( action, childEntityName );
535532
try {
536533
action.cascade(
537534
eventSource,
538535
child,
539-
type instanceof EntityType entityType
540-
? entityType.getAssociatedEntityName()
541-
: null,
536+
childEntityName,
542537
parentEntityName,
543538
propertyName,
544539
componentPath,
@@ -572,14 +567,12 @@ private static <T> void cascadeCollectionElements(
572567

573568
final boolean reallyDoCascade = style.reallyDoCascade( action )
574569
&& child != CollectionType.UNFETCHED_COLLECTION;
575-
576570
if ( reallyDoCascade ) {
577571
final boolean traceEnabled = CORE_LOGGER.isTraceEnabled();
578572
if ( traceEnabled ) {
579-
CORE_LOGGER.tracev( "Cascade {0} for collection: {1}", action, collectionType.getRole() );
573+
CORE_LOGGER.cascadingCollection( action, collectionType.getRole() );
580574
}
581-
582-
final Iterator<?> iterator = action.getCascadableChildrenIterator( eventSource, collectionType, child );
575+
final var iterator = action.getCascadableChildrenIterator( eventSource, collectionType, child );
583576
while ( iterator.hasNext() ) {
584577
cascadeProperty(
585578
action,
@@ -597,14 +590,13 @@ private static <T> void cascadeCollectionElements(
597590
isCascadeDeleteEnabled
598591
);
599592
}
600-
601593
if ( traceEnabled ) {
602-
CORE_LOGGER.tracev( "Done cascade {0} for collection: {1}", action, collectionType.getRole() );
594+
CORE_LOGGER.doneCascadingCollection( action, collectionType.getRole() );
603595
}
604596
}
605597

606598
// a newly instantiated collection can't have orphans
607-
final PersistentCollection<?> persistentCollection =
599+
final var persistentCollection =
608600
child instanceof PersistentCollection<?> collection
609601
? collection
610602
: eventSource.getPersistenceContextInternal()
@@ -620,42 +612,46 @@ private static <T> void cascadeCollectionElements(
620612
if ( deleteOrphans ) {
621613
final boolean traceEnabled = CORE_LOGGER.isTraceEnabled();
622614
if ( traceEnabled ) {
623-
CORE_LOGGER.tracev( "Deleting orphans for collection: {0}", collectionType.getRole() );
615+
CORE_LOGGER.deletingOrphans( collectionType.getRole() );
624616
}
625617
// we can do the cast since orphan-delete does not apply to:
626618
// 1. newly instantiated collections
627619
// 2. arrays (we can't track orphans for detached arrays)
628620
final String elementEntityName = collectionType.getAssociatedEntityName( eventSource.getFactory() );
629621
deleteOrphans( eventSource, elementEntityName, persistentCollection );
630-
631622
if ( traceEnabled ) {
632-
CORE_LOGGER.tracev( "Done deleting orphans for collection: {0}", collectionType.getRole() );
623+
CORE_LOGGER.doneDeletingOrphans( collectionType.getRole() );
633624
}
634625
}
635626
}
636627

637628
/**
638629
* Delete any entities that were removed from the collection
639630
*/
640-
private static void deleteOrphans(EventSource eventSource, String entityName, PersistentCollection<?> pc) {
631+
private static void deleteOrphans(EventSource eventSource, String entityName, PersistentCollection<?> collection) {
641632
//TODO: suck this logic into the collection!
642-
final Collection<?> orphans;
643-
if ( pc.wasInitialized() ) {
644-
final CollectionEntry entry = eventSource.getPersistenceContextInternal().getCollectionEntry( pc );
645-
orphans = entry == null ? EMPTY_LIST : entry.getOrphans( entityName, pc );
646-
}
647-
else {
648-
orphans = pc.getQueuedOrphans( entityName );
649-
}
650-
651-
for ( Object orphan : orphans ) {
633+
for ( Object orphan : getOrphans( eventSource, entityName, collection ) ) {
652634
if ( orphan != null ) {
653-
CORE_LOGGER.tracev( "Deleting orphaned entity instance: {0}", entityName );
635+
CORE_LOGGER.deletingOrphanOfType( entityName );
654636
eventSource.delete( entityName, orphan, false, DeleteContext.create() );
655637
}
656638
}
657639
}
658640

641+
private static Collection<?> getOrphans(EventSource eventSource, String entityName, PersistentCollection<?> collection) {
642+
if ( collection.wasInitialized() ) {
643+
final var collectionEntry =
644+
eventSource.getPersistenceContextInternal()
645+
.getCollectionEntry( collection );
646+
return collectionEntry == null
647+
? EMPTY_LIST
648+
: collectionEntry.getOrphans( entityName, collection );
649+
}
650+
else {
651+
return collection.getQueuedOrphans( entityName );
652+
}
653+
}
654+
659655
private static <T> boolean cascadeDeleteEnabled(CascadingAction<T> action, CollectionPersister persister) {
660656
return action.directionAffectedByCascadeDelete() == ForeignKeyDirection.FROM_PARENT
661657
&& persister.isCascadeDeleteEnabled();

hibernate-core/src/main/java/org/hibernate/engine/spi/CascadingActions.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import static java.util.Collections.emptyIterator;
3333
import static org.hibernate.engine.internal.ForeignKeys.isTransient;
3434
import static org.hibernate.engine.internal.ManagedTypeHelper.isHibernateProxy;
35-
import static org.hibernate.internal.CoreMessageLogger.CORE_LOGGER;
3635
import static org.hibernate.internal.util.StringHelper.join;
3736

3837
/**
@@ -60,7 +59,6 @@ public void cascade(
6059
List<String> attributePath,
6160
DeleteContext context,
6261
boolean isCascadeDeleteEnabled) {
63-
CORE_LOGGER.tracev( "Cascading to delete: {0}", childEntityName );
6462
session.delete( childEntityName, child, isCascadeDeleteEnabled, context );
6563
}
6664

@@ -121,7 +119,6 @@ public void cascade(
121119
List<String> attributePath,
122120
LockOptions lockOptions,
123121
boolean isCascadeDeleteEnabled) {
124-
CORE_LOGGER.tracev( "Cascading to lock: {0}", childEntityName );
125122
session.lock( childEntityName, child, lockOptions );
126123
}
127124

@@ -161,7 +158,6 @@ public void cascade(
161158
RefreshContext context,
162159
boolean isCascadeDeleteEnabled)
163160
throws HibernateException {
164-
CORE_LOGGER.tracev( "Cascading to refresh: {0}", childEntityName );
165161
session.refresh( childEntityName, child, context );
166162
}
167163

@@ -200,7 +196,6 @@ public void cascade(
200196
Void nothing,
201197
boolean isCascadeDeleteEnabled)
202198
throws HibernateException {
203-
CORE_LOGGER.tracev( "Cascading to evict: {0}", childEntityName );
204199
session.evict( child );
205200
}
206201

@@ -244,7 +239,6 @@ public void cascade(
244239
MergeContext context,
245240
boolean isCascadeDeleteEnabled)
246241
throws HibernateException {
247-
CORE_LOGGER.tracev( "Cascading to merge: {0}", childEntityName );
248242
session.merge( childEntityName, child, context );
249243
}
250244

@@ -284,7 +278,6 @@ public void cascade(
284278
PersistContext context,
285279
boolean isCascadeDeleteEnabled)
286280
throws HibernateException {
287-
CORE_LOGGER.tracev( "Cascading to persist: {0}", childEntityName );
288281
session.persist( childEntityName, child, context );
289282
}
290283

@@ -335,7 +328,6 @@ public void cascade(
335328
PersistContext context,
336329
boolean isCascadeDeleteEnabled)
337330
throws HibernateException {
338-
CORE_LOGGER.tracev( "Cascading to persist on flush: {0}", childEntityName );
339331
session.persistOnFlush( childEntityName, child, context );
340332
}
341333

@@ -540,7 +532,6 @@ public void cascade(
540532
ReplicationMode mode,
541533
boolean isCascadeDeleteEnabled)
542534
throws HibernateException {
543-
CORE_LOGGER.tracev( "Cascading to replicate: {0}", childEntityName );
544535
session.replicate( childEntityName, child, mode );
545536
}
546537

hibernate-core/src/main/java/org/hibernate/engine/spi/CollectionEntry.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,8 @@ public void preFlush(PersistentCollection<?> collection) {
184184
dirty( collection );
185185

186186
if ( CORE_LOGGER.isTraceEnabled() && collection.isDirty() && loadedPersister != null ) {
187-
CORE_LOGGER.trace( "Collection dirty: "
188-
+ collectionInfoString( loadedPersister.getRole(), getLoadedKey() ) );
187+
CORE_LOGGER.collectionDirty(
188+
collectionInfoString( loadedPersister.getRole(), getLoadedKey() ) );
189189
}
190190

191191
setReached( false );
@@ -265,7 +265,7 @@ public void afterAction(PersistentCollection<?> collection) {
265265
* @param storedSnapshot the new stored snapshot
266266
*/
267267
public void resetStoredSnapshot(PersistentCollection<?> collection, Serializable storedSnapshot) {
268-
CORE_LOGGER.tracef("Reset storedSnapshot to %s for %s", storedSnapshot, this);
268+
CORE_LOGGER.resetStoredSnapshot( storedSnapshot, this );
269269

270270
if ( !fromMerge ) {
271271
snapshot = storedSnapshot;

0 commit comments

Comments
 (0)