@@ -793,35 +793,48 @@ private <T> Mono<T> processNestedRelations(Neo4jPersistentEntity<?> sourceEntity
793
793
Flux <RelationshipHandler > relationshipCreation = Flux .fromIterable (relatedValuesToStore ).concatMap (relatedValueToStore -> {
794
794
795
795
Object relatedObjectBeforeCallbacksApplied = relationshipContext .identifyAndExtractRelationshipTargetNode (relatedValueToStore );
796
- return Mono .deferContextual (ctx -> eventSupport
797
- .maybeCallBeforeBind (relatedObjectBeforeCallbacksApplied )
796
+ return Mono .deferContextual (ctx ->
797
+
798
+ (stateMachine .hasProcessedValue (relatedObjectBeforeCallbacksApplied )
799
+ ? Mono .just (stateMachine .getProcessedAs (relatedObjectBeforeCallbacksApplied ))
800
+ : eventSupport .maybeCallBeforeBind (relatedObjectBeforeCallbacksApplied ))
801
+
798
802
.flatMap (newRelatedObject -> {
799
803
Neo4jPersistentEntity <?> targetEntity = neo4jMappingContext .getPersistentEntity (relatedObjectBeforeCallbacksApplied .getClass ());
800
804
801
- Mono <Tuple2 <Long , Long >> queryOrSave ;
802
- long noVersion = Long .MIN_VALUE ;
805
+ Mono <Tuple2 <Long [], Long []>> queryOrSave ;
803
806
if (stateMachine .hasProcessedValue (relatedValueToStore )) {
804
- queryOrSave = Mono .just (stateMachine .getInternalId (relatedObjectBeforeCallbacksApplied ) )
805
- .map (id -> Tuples .of (id , noVersion ));
807
+ queryOrSave = Mono .just (new Long [] { stateMachine .getInternalId (relatedValueToStore )} )
808
+ .map (id -> Tuples .of (id , new Long [ 1 ] ));
806
809
} else {
807
810
queryOrSave = saveRelatedNode (newRelatedObject , targetEntity )
808
- .map (entity -> Tuples .of (entity .id (), targetEntity .hasVersionProperty () ?
809
- entity .get (targetEntity .getVersionProperty ().getPropertyName ())
810
- .asLong () :
811
- noVersion ));
811
+ .doOnNext (entity -> stateMachine .markValueAsProcessed (relatedValueToStore , entity .id ()))
812
+ .map (entity -> {
813
+ Long version = targetEntity .hasVersionProperty () ?
814
+ entity .get (targetEntity .getVersionProperty ().getPropertyName ()).asLong () :
815
+ null ;
816
+ return Tuples .of (
817
+ new Long [] { entity .id () },
818
+ new Long [] { version });
819
+ });
812
820
}
813
821
return queryOrSave .flatMap (idAndVersion -> {
814
- long relatedInternalId = idAndVersion .getT1 ();
815
- stateMachine .markValueAsProcessed (relatedValueToStore , relatedInternalId );
822
+ Long relatedInternalId = idAndVersion .getT1 ()[0 ];
816
823
// if an internal id is used this must be set to link this entity in the next iteration
817
824
PersistentPropertyAccessor <?> targetPropertyAccessor = targetEntity .getPropertyAccessor (newRelatedObject );
818
825
if (targetEntity .isUsingInternalIds ()) {
819
- targetPropertyAccessor .setProperty (targetEntity .getRequiredIdProperty (), relatedInternalId );
820
- stateMachine .markValueAsProcessedAs (newRelatedObject , targetPropertyAccessor .getBean ());
826
+ Neo4jPersistentProperty requiredIdProperty = targetEntity .getRequiredIdProperty ();
827
+ if (relatedInternalId == null
828
+ && targetPropertyAccessor .getProperty (requiredIdProperty ) != null ) {
829
+ relatedInternalId = (Long ) targetPropertyAccessor .getProperty (requiredIdProperty );
830
+ } else if (targetPropertyAccessor .getProperty (requiredIdProperty ) == null ) {
831
+ targetPropertyAccessor .setProperty (requiredIdProperty , relatedInternalId );
832
+ }
821
833
}
822
- if (targetEntity .hasVersionProperty () && idAndVersion .getT2 () != noVersion ) {
823
- targetPropertyAccessor .setProperty (targetEntity .getVersionProperty (), idAndVersion .getT2 ());
834
+ if (targetEntity .hasVersionProperty () && idAndVersion .getT2 ()[ 0 ] != null ) {
835
+ targetPropertyAccessor .setProperty (targetEntity .getVersionProperty (), idAndVersion .getT2 ()[ 0 ] );
824
836
}
837
+ stateMachine .markValueAsProcessedAs (relatedObjectBeforeCallbacksApplied , targetPropertyAccessor .getBean ());
825
838
826
839
Object idValue = idProperty != null
827
840
? relationshipContext
@@ -898,7 +911,7 @@ private <Y> Mono<Entity> saveRelatedNode(Object relatedNode, Neo4jPersistentEnti
898
911
899
912
return neo4jClient
900
913
.query (() -> renderer .render (cypherGenerator .prepareSaveOf (targetNodeDescription , dynamicLabels )))
901
- .bind (( Y ) entity ).with (neo4jMappingContext .getRequiredBinderFunctionFor (entityType ))
914
+ .bind (entity ).with (neo4jMappingContext .getRequiredBinderFunctionFor (entityType ))
902
915
.fetchAs (Entity .class )
903
916
.one ();
904
917
}).switchIfEmpty (Mono .defer (() -> {
0 commit comments