From f30a7a1cf64e07df2fe7933dc72ae165e376b666 Mon Sep 17 00:00:00 2001 From: Davide D'Alto Date: Thu, 11 Jul 2024 19:54:53 +0200 Subject: [PATCH] Replace loop in cascade --- .../reactive/engine/impl/Cascade.java | 44 ++++++++----------- 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/hibernate-reactive-core/src/main/java/org/hibernate/reactive/engine/impl/Cascade.java b/hibernate-reactive-core/src/main/java/org/hibernate/reactive/engine/impl/Cascade.java index 60f5971dc..7e113a009 100644 --- a/hibernate-reactive-core/src/main/java/org/hibernate/reactive/engine/impl/Cascade.java +++ b/hibernate-reactive-core/src/main/java/org/hibernate/reactive/engine/impl/Cascade.java @@ -623,40 +623,34 @@ private static CompletionStage cascadeCollectionElements( final T anything, final boolean isCascadeDeleteEnabled) throws HibernateException { final boolean reallyDoCascade = style.reallyDoCascade( action.delegate() ) && child != CollectionType.UNFETCHED_COLLECTION; - CompletionStage stage = voidFuture(); if ( reallyDoCascade ) { final boolean traceEnabled = LOG.isTraceEnabled(); - stage = stage.thenRun( () -> { - if ( traceEnabled ) { - LOG.tracev( "Done cascade {0} for collection: {1}", action, collectionType.getRole() ); - } - } ); - final Iterator itr = action.getCascadableChildrenIterator( eventSource, collectionType, child ); - while ( itr.hasNext() ) { - stage = stage.thenCompose( v -> cascadeProperty( - action, - cascadePoint, - eventSource, - componentPath, - parent, - itr.next(), - elemType, - style, - collectionType.getRole().substring( collectionType.getRole().lastIndexOf( '.' ) + 1 ), - anything, - isCascadeDeleteEnabled - ) ); + if ( traceEnabled ) { + LOG.tracev( "Done cascade {0} for collection: {1}", action, collectionType.getRole() ); } - stage = stage.thenRun( () -> { + + final Iterator itr = action.getCascadableChildrenIterator( eventSource, collectionType, child ); + return loop( itr, (value, integer) -> cascadeProperty( + action, + cascadePoint, + eventSource, + componentPath, + parent, + value, + elemType, + style, + collectionType.getRole().substring( collectionType.getRole().lastIndexOf( '.' ) + 1 ), + anything, + isCascadeDeleteEnabled + ) ).thenRun( () -> { if ( traceEnabled ) { LOG.tracev( "Done cascade {0} for collection: {1}", action, collectionType.getRole() ); } - } ); + } ).thenCompose( v -> doDeleteOrphans( action, eventSource, child, collectionType, style, elemType ) ); } - return stage - .thenCompose( v -> doDeleteOrphans( action, eventSource, child, collectionType, style, elemType ) ); + return doDeleteOrphans( action, eventSource, child, collectionType, style, elemType ); } private static CompletionStage doDeleteOrphans(