3838import java .util .Map ;
3939import java .util .Set ;
4040import java .util .concurrent .ConcurrentHashMap ;
41+ import java .util .concurrent .atomic .AtomicReference ;
4142import java .util .function .BiFunction ;
4243import java .util .function .Function ;
4344import java .util .stream .Collectors ;
@@ -892,10 +893,14 @@ private <T> Mono<T> processNestedRelations(Neo4jPersistentEntity<?> sourceEntity
892893 .flatMap (newRelatedObject -> {
893894 Neo4jPersistentEntity <?> targetEntity = neo4jMappingContext .getRequiredPersistentEntity (relatedObjectBeforeCallbacksApplied .getClass ());
894895
895- Mono <Tuple2 <Long [], Long [] >> queryOrSave ;
896+ Mono <Tuple2 <AtomicReference < Long >, AtomicReference < Entity > >> queryOrSave ;
896897 if (stateMachine .hasProcessedValue (relatedValueToStore )) {
897- queryOrSave = Mono .just (new Long [] {stateMachine .getInternalId (relatedValueToStore )})
898- .map (id -> Tuples .of (id , new Long [1 ]));
898+ AtomicReference <Long > relatedInternalId = new AtomicReference <>();
899+ Long possibleValue = stateMachine .getInternalId (relatedValueToStore );
900+ if (possibleValue != null ) {
901+ relatedInternalId .set (possibleValue );
902+ }
903+ queryOrSave = Mono .just (Tuples .of (relatedInternalId , new AtomicReference <>()));
899904 } else {
900905 queryOrSave = saveRelatedNode (newRelatedObject , targetEntity , includeProperty , currentPropertyPath )
901906 .doOnNext (entity -> {
@@ -905,17 +910,11 @@ private <T> Mono<T> processNestedRelations(Neo4jPersistentEntity<?> sourceEntity
905910 stateMachine .markValueAsProcessedAs (value , entity .id ());
906911 }
907912 })
908- .map (entity -> {
909- Long version = targetEntity .hasVersionProperty () ?
910- entity .get (targetEntity .getVersionProperty ().getPropertyName ()).asLong () :
911- null ;
912- return Tuples .of (
913- new Long [] { entity .id () },
914- new Long [] { version });
915- });
913+ .map (entity -> Tuples .of (new AtomicReference <>(entity .id ()), new AtomicReference <>(entity )));
916914 }
917- return queryOrSave .flatMap (idAndVersion -> {
918- Long relatedInternalId = idAndVersion .getT1 ()[0 ];
915+ return queryOrSave .flatMap (idAndEntity -> {
916+ Long relatedInternalId = idAndEntity .getT1 ().get ();
917+ Entity savedEntity = idAndEntity .getT2 ().get ();
919918 // if an internal id is used this must be set to link this entity in the next iteration
920919 PersistentPropertyAccessor <?> targetPropertyAccessor = targetEntity .getPropertyAccessor (newRelatedObject );
921920 if (targetEntity .isUsingInternalIds ()) {
@@ -927,8 +926,8 @@ private <T> Mono<T> processNestedRelations(Neo4jPersistentEntity<?> sourceEntity
927926 targetPropertyAccessor .setProperty (requiredIdProperty , relatedInternalId );
928927 }
929928 }
930- if (targetEntity . hasVersionProperty () && idAndVersion . getT2 ()[ 0 ] != null ) {
931- targetPropertyAccessor . setProperty (targetEntity . getVersionProperty (), idAndVersion . getT2 ()[ 0 ] );
929+ if (savedEntity != null ) {
930+ TemplateSupport . updateVersionPropertyIfPossible (targetEntity , targetPropertyAccessor , savedEntity );
932931 }
933932 stateMachine .markValueAsProcessedAs (relatedObjectBeforeCallbacksApplied , targetPropertyAccessor .getBean ());
934933 stateMachine .markRelationshipAsProcessed (relatedInternalId , relationshipDescription .getRelationshipObverse ());
0 commit comments