38
38
import java .util .Map ;
39
39
import java .util .Set ;
40
40
import java .util .concurrent .ConcurrentHashMap ;
41
+ import java .util .concurrent .atomic .AtomicReference ;
41
42
import java .util .function .BiFunction ;
42
43
import java .util .function .Function ;
43
44
import java .util .stream .Collectors ;
@@ -892,10 +893,14 @@ private <T> Mono<T> processNestedRelations(Neo4jPersistentEntity<?> sourceEntity
892
893
.flatMap (newRelatedObject -> {
893
894
Neo4jPersistentEntity <?> targetEntity = neo4jMappingContext .getRequiredPersistentEntity (relatedObjectBeforeCallbacksApplied .getClass ());
894
895
895
- Mono <Tuple2 <Long [], Long [] >> queryOrSave ;
896
+ Mono <Tuple2 <AtomicReference < Long >, AtomicReference < Entity > >> queryOrSave ;
896
897
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 <>()));
899
904
} else {
900
905
queryOrSave = saveRelatedNode (newRelatedObject , targetEntity , includeProperty , currentPropertyPath )
901
906
.doOnNext (entity -> {
@@ -905,17 +910,11 @@ private <T> Mono<T> processNestedRelations(Neo4jPersistentEntity<?> sourceEntity
905
910
stateMachine .markValueAsProcessedAs (value , entity .id ());
906
911
}
907
912
})
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 )));
916
914
}
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 ();
919
918
// if an internal id is used this must be set to link this entity in the next iteration
920
919
PersistentPropertyAccessor <?> targetPropertyAccessor = targetEntity .getPropertyAccessor (newRelatedObject );
921
920
if (targetEntity .isUsingInternalIds ()) {
@@ -927,8 +926,8 @@ private <T> Mono<T> processNestedRelations(Neo4jPersistentEntity<?> sourceEntity
927
926
targetPropertyAccessor .setProperty (requiredIdProperty , relatedInternalId );
928
927
}
929
928
}
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 );
932
931
}
933
932
stateMachine .markValueAsProcessedAs (relatedObjectBeforeCallbacksApplied , targetPropertyAccessor .getBean ());
934
933
stateMachine .markRelationshipAsProcessed (relatedInternalId , relationshipDescription .getRelationshipObverse ());
0 commit comments