refactor: Avoid recreation of imperative nested statemachine in some comes.

Fixes #2882
This commit is contained in:
Michael Simons
2024-03-18 14:42:46 +01:00
parent cc8312e9d2
commit bf890a696d
2 changed files with 10 additions and 2 deletions

View File

@@ -529,12 +529,14 @@ public final class Neo4jTemplate implements
.collect(Collectors.toMap(m -> (Value) m.getKey(), m -> (String) m.getValue()));
// Save related
var stateMachine = new NestedRelationshipProcessingStateMachine(neo4jMappingContext, null, null);
return entitiesToBeSaved.stream().map(t -> {
PersistentPropertyAccessor<T> propertyAccessor = entityMetaData.getPropertyAccessor(t.modifiedInstance);
Neo4jPersistentProperty idProperty = entityMetaData.getRequiredIdProperty();
Object id = convertIdValues(idProperty, propertyAccessor.getProperty(idProperty));
String internalId = idToInternalIdMapping.get(id);
return this.<T>processRelations(entityMetaData, propertyAccessor, t.wasNew, new NestedRelationshipProcessingStateMachine(neo4jMappingContext, t.originalInstance, internalId), TemplateSupport.computeIncludePropertyPredicate(pps, entityMetaData));
stateMachine.registerInitialObject(t.originalInstance, internalId);
return this.<T>processRelations(entityMetaData, propertyAccessor, t.wasNew, stateMachine, TemplateSupport.computeIncludePropertyPredicate(pps, entityMetaData));
}).collect(Collectors.toList());
}

View File

@@ -73,9 +73,15 @@ public final class NestedRelationshipProcessingStateMachine {
this.mappingContext = mappingContext;
}
public NestedRelationshipProcessingStateMachine(final Neo4jMappingContext mappingContext, Object initialObject, Object elementId) {
public NestedRelationshipProcessingStateMachine(final Neo4jMappingContext mappingContext, @Nullable Object initialObject, @Nullable Object elementId) {
this(mappingContext);
if (initialObject != null && elementId != null) {
registerInitialObject(initialObject, elementId);
}
}
public void registerInitialObject(Object initialObject, Object elementId) {
Assert.notNull(initialObject, "Initial object must not be null");
Assert.notNull(elementId, "The initial objects element ID must not be null");