66
77import java .util .ArrayList ;
88import java .util .Collection ;
9- import java .util .Iterator ;
109import java .util .List ;
1110
1211import org .hibernate .HibernateException ;
1312import org .hibernate .annotations .OnDeleteAction ;
1413import org .hibernate .collection .spi .PersistentCollection ;
1514import org .hibernate .engine .spi .CascadeStyle ;
1615import org .hibernate .engine .spi .CascadingAction ;
17- import org .hibernate .engine .spi .CollectionEntry ;
1816import org .hibernate .engine .spi .EntityEntry ;
19- import org .hibernate .engine .spi .PersistenceContext ;
2017import org .hibernate .engine .spi .Status ;
2118import org .hibernate .event .spi .DeleteContext ;
2219import 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 ();
0 commit comments