4141import java .util .Map ;
4242import java .util .Set ;
4343import java .util .concurrent .ConcurrentHashMap ;
44+ import java .util .concurrent .atomic .AtomicReference ;
4445import java .util .function .BiFunction ;
4546import java .util .function .Function ;
4647import java .util .stream .Collectors ;
@@ -866,10 +867,14 @@ private <T> Mono<T> processNestedRelations(Neo4jPersistentEntity<?> sourceEntity
866867 .flatMap (newRelatedObject -> {
867868 Neo4jPersistentEntity <?> targetEntity = neo4jMappingContext .getPersistentEntity (relatedObjectBeforeCallbacksApplied .getClass ());
868869
869- Mono <Tuple2 <Long [], Long [] >> queryOrSave ;
870+ Mono <Tuple2 <AtomicReference < Long >, AtomicReference < Entity > >> queryOrSave ;
870871 if (stateMachine .hasProcessedValue (relatedValueToStore )) {
871- queryOrSave = Mono .just (new Long [] {stateMachine .getInternalId (relatedValueToStore )})
872- .map (id -> Tuples .of (id , new Long [1 ]));
872+ AtomicReference <Long > relatedInternalId = new AtomicReference <>();
873+ Long possibleValue = stateMachine .getInternalId (relatedValueToStore );
874+ if (possibleValue != null ) {
875+ relatedInternalId .set (possibleValue );
876+ }
877+ queryOrSave = Mono .just (Tuples .of (relatedInternalId , new AtomicReference <>()));
873878 } else {
874879 queryOrSave = saveRelatedNode (newRelatedObject , targetEntity , includeProperty , currentPropertyPath )
875880 .doOnNext (entity -> {
@@ -879,17 +884,11 @@ private <T> Mono<T> processNestedRelations(Neo4jPersistentEntity<?> sourceEntity
879884 stateMachine .markValueAsProcessedAs (value , entity .id ());
880885 }
881886 })
882- .map (entity -> {
883- Long version = targetEntity .hasVersionProperty () ?
884- entity .get (targetEntity .getVersionProperty ().getPropertyName ()).asLong () :
885- null ;
886- return Tuples .of (
887- new Long [] { entity .id () },
888- new Long [] { version });
889- });
887+ .map (entity -> Tuples .of (new AtomicReference <>(entity .id ()), new AtomicReference <>(entity )));
890888 }
891- return queryOrSave .flatMap (idAndVersion -> {
892- Long relatedInternalId = idAndVersion .getT1 ()[0 ];
889+ return queryOrSave .flatMap (idAndEntity -> {
890+ Long relatedInternalId = idAndEntity .getT1 ().get ();
891+ Entity savedEntity = idAndEntity .getT2 ().get ();
893892 // if an internal id is used this must be set to link this entity in the next iteration
894893 PersistentPropertyAccessor <?> targetPropertyAccessor = targetEntity .getPropertyAccessor (newRelatedObject );
895894 if (targetEntity .isUsingInternalIds ()) {
@@ -901,8 +900,8 @@ private <T> Mono<T> processNestedRelations(Neo4jPersistentEntity<?> sourceEntity
901900 targetPropertyAccessor .setProperty (requiredIdProperty , relatedInternalId );
902901 }
903902 }
904- if (targetEntity . hasVersionProperty () && idAndVersion . getT2 ()[ 0 ] != null ) {
905- targetPropertyAccessor . setProperty (targetEntity . getVersionProperty (), idAndVersion . getT2 ()[ 0 ] );
903+ if (savedEntity != null ) {
904+ TemplateSupport . updateVersionPropertyIfPossible (targetEntity , targetPropertyAccessor , savedEntity );
906905 }
907906 stateMachine .markValueAsProcessedAs (relatedObjectBeforeCallbacksApplied , targetPropertyAccessor .getBean ());
908907 stateMachine .markRelationshipAsProcessed (relatedInternalId , relationshipDescription .getRelationshipObverse ());
0 commit comments