Skip to content

Commit 28a6e5c

Browse files
refactor: Avoid recreation of imperative nested statemachine in some comes.
Fixes #2882
1 parent 6d18d0b commit 28a6e5c

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/main/java/org/springframework/data/neo4j/core/Neo4jTemplate.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -575,12 +575,14 @@ class Tuple3<T> {
575575
.collect(Collectors.toMap(m -> (Value) m.getKey(), m -> (String) m.getValue()));
576576

577577
// Save related
578+
var stateMachine = new NestedRelationshipProcessingStateMachine(neo4jMappingContext, null, null);
578579
return entitiesToBeSaved.stream().map(t -> {
579580
PersistentPropertyAccessor<T> propertyAccessor = entityMetaData.getPropertyAccessor(t.modifiedInstance);
580581
Neo4jPersistentProperty idProperty = entityMetaData.getRequiredIdProperty();
581582
Object id = convertIdValues(idProperty, propertyAccessor.getProperty(idProperty));
582583
String internalId = idToInternalIdMapping.get(id);
583-
return this.<T>processRelations(entityMetaData, propertyAccessor, t.wasNew, new NestedRelationshipProcessingStateMachine(neo4jMappingContext, t.originalInstance, internalId), TemplateSupport.computeIncludePropertyPredicate(pps, entityMetaData));
584+
stateMachine.registerInitialObject(t.originalInstance, internalId);
585+
return this.<T>processRelations(entityMetaData, propertyAccessor, t.wasNew, stateMachine, TemplateSupport.computeIncludePropertyPredicate(pps, entityMetaData));
584586
}).collect(Collectors.toList());
585587
}
586588

src/main/java/org/springframework/data/neo4j/core/mapping/NestedRelationshipProcessingStateMachine.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,15 @@ public NestedRelationshipProcessingStateMachine(final Neo4jMappingContext mappin
7373
this.mappingContext = mappingContext;
7474
}
7575

76-
public NestedRelationshipProcessingStateMachine(final Neo4jMappingContext mappingContext, Object initialObject, Object elementId) {
76+
public NestedRelationshipProcessingStateMachine(final Neo4jMappingContext mappingContext, @Nullable Object initialObject, @Nullable Object elementId) {
7777
this(mappingContext);
7878

79+
if (initialObject != null && elementId != null) {
80+
registerInitialObject(initialObject, elementId);
81+
}
82+
}
83+
84+
public void registerInitialObject(Object initialObject, Object elementId) {
7985
Assert.notNull(initialObject, "Initial object must not be null");
8086
Assert.notNull(elementId, "The initial objects element ID must not be null");
8187

0 commit comments

Comments
 (0)