41
41
import java .util .Map ;
42
42
import java .util .Set ;
43
43
import java .util .concurrent .ConcurrentHashMap ;
44
+ import java .util .concurrent .atomic .AtomicReference ;
44
45
import java .util .function .BiFunction ;
45
46
import java .util .function .Function ;
46
47
import java .util .stream .Collectors ;
@@ -866,10 +867,14 @@ private <T> Mono<T> processNestedRelations(Neo4jPersistentEntity<?> sourceEntity
866
867
.flatMap (newRelatedObject -> {
867
868
Neo4jPersistentEntity <?> targetEntity = neo4jMappingContext .getPersistentEntity (relatedObjectBeforeCallbacksApplied .getClass ());
868
869
869
- Mono <Tuple2 <Long [], Long [] >> queryOrSave ;
870
+ Mono <Tuple2 <AtomicReference < Long >, AtomicReference < Entity > >> queryOrSave ;
870
871
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 <>()));
873
878
} else {
874
879
queryOrSave = saveRelatedNode (newRelatedObject , targetEntity , includeProperty , currentPropertyPath )
875
880
.doOnNext (entity -> {
@@ -879,17 +884,11 @@ private <T> Mono<T> processNestedRelations(Neo4jPersistentEntity<?> sourceEntity
879
884
stateMachine .markValueAsProcessedAs (value , entity .id ());
880
885
}
881
886
})
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 )));
890
888
}
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 ();
893
892
// if an internal id is used this must be set to link this entity in the next iteration
894
893
PersistentPropertyAccessor <?> targetPropertyAccessor = targetEntity .getPropertyAccessor (newRelatedObject );
895
894
if (targetEntity .isUsingInternalIds ()) {
@@ -901,8 +900,8 @@ private <T> Mono<T> processNestedRelations(Neo4jPersistentEntity<?> sourceEntity
901
900
targetPropertyAccessor .setProperty (requiredIdProperty , relatedInternalId );
902
901
}
903
902
}
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 );
906
905
}
907
906
stateMachine .markValueAsProcessedAs (relatedObjectBeforeCallbacksApplied , targetPropertyAccessor .getBean ());
908
907
stateMachine .markRelationshipAsProcessed (relatedInternalId , relationshipDescription .getRelationshipObverse ());
0 commit comments