|
19 | 19 | import static org.neo4j.cypherdsl.core.Cypher.asterisk;
|
20 | 20 | import static org.neo4j.cypherdsl.core.Cypher.parameter;
|
21 | 21 |
|
| 22 | +import java.util.AbstractMap; |
22 | 23 | import java.util.ArrayList;
|
23 | 24 | import java.util.Collection;
|
24 | 25 | import java.util.Collections;
|
|
42 | 43 | import org.neo4j.cypherdsl.core.Node;
|
43 | 44 | import org.neo4j.cypherdsl.core.Statement;
|
44 | 45 | import org.neo4j.cypherdsl.core.renderer.Renderer;
|
| 46 | +import org.neo4j.driver.Value; |
45 | 47 | import org.neo4j.driver.exceptions.NoSuchRecordException;
|
46 | 48 | import org.neo4j.driver.summary.ResultSummary;
|
47 |
| -import org.neo4j.driver.summary.SummaryCounters; |
48 | 49 | import org.neo4j.driver.types.Entity;
|
49 | 50 | import org.neo4j.driver.types.MapAccessor;
|
50 | 51 | import org.neo4j.driver.types.TypeSystem;
|
@@ -466,20 +467,22 @@ class Tuple3<T> {
|
466 | 467 | Function<T, Map<String, Object>> binderFunction = neo4jMappingContext.getRequiredBinderFunctionFor(domainClass);
|
467 | 468 | List<Map<String, Object>> entityList = entitiesToBeSaved.stream().map(h -> h.modifiedInstance).map(binderFunction)
|
468 | 469 | .collect(Collectors.toList());
|
469 |
| - ResultSummary resultSummary = neo4jClient |
| 470 | + Map<Value, Long> idToInternalIdMapping = neo4jClient |
470 | 471 | .query(() -> renderer.render(cypherGenerator.prepareSaveOfMultipleInstancesOf(entityMetaData)))
|
471 |
| - .bind(entityList).to(Constants.NAME_OF_ENTITY_LIST_PARAM).run(); |
472 |
| - |
473 |
| - SummaryCounters counters = resultSummary.counters(); |
474 |
| - log.debug(() -> String.format( |
475 |
| - "Created %d and deleted %d nodes, created %d and deleted %d relationships and set %d properties.", |
476 |
| - counters.nodesCreated(), counters.nodesDeleted(), counters.relationshipsCreated(), |
477 |
| - counters.relationshipsDeleted(), counters.propertiesSet())); |
| 472 | + .bind(entityList).to(Constants.NAME_OF_ENTITY_LIST_PARAM) |
| 473 | + .fetchAs(Map.Entry.class) |
| 474 | + .mappedBy((t, r) -> new AbstractMap.SimpleEntry<>(r.get(Constants.NAME_OF_ID), r.get(Constants.NAME_OF_INTERNAL_ID).asLong())) |
| 475 | + .all() |
| 476 | + .stream() |
| 477 | + .collect(Collectors.toMap(m -> (Value) m.getKey(), m -> (Long) m.getValue())); |
478 | 478 |
|
479 | 479 | // Save related
|
480 | 480 | return entitiesToBeSaved.stream().map(t -> {
|
481 | 481 | PersistentPropertyAccessor<T> propertyAccessor = entityMetaData.getPropertyAccessor(t.modifiedInstance);
|
482 |
| - return processRelations(entityMetaData, t.originalInstance, propertyAccessor, t.wasNew, TemplateSupport.computeIncludePropertyPredicate(includedProperties, entityMetaData)); |
| 482 | + Neo4jPersistentProperty idProperty = entityMetaData.getRequiredIdProperty(); |
| 483 | + Object id = convertIdValues(idProperty, propertyAccessor.getProperty(idProperty)); |
| 484 | + Long internalId = idToInternalIdMapping.get(id); |
| 485 | + return processRelations(entityMetaData, t.originalInstance, internalId, propertyAccessor, t.wasNew, TemplateSupport.computeIncludePropertyPredicate(includedProperties, entityMetaData)); |
483 | 486 | }).collect(Collectors.toList());
|
484 | 487 | }
|
485 | 488 |
|
@@ -632,22 +635,13 @@ private <T> ExecutableQuery<T> createExecutableQuery(Class<T> domainType, @Nulla
|
632 | 635 | * @param isParentObjectNew A flag if the parent was new
|
633 | 636 | * @param includeProperty A predicate telling to include a relationship property or not
|
634 | 637 | */
|
635 |
| - private <T> void processRelations(Neo4jPersistentEntity<?> neo4jPersistentEntity, T originalInstance, Long internalId, |
| 638 | + private <T> T processRelations(Neo4jPersistentEntity<?> neo4jPersistentEntity, T originalInstance, Long internalId, |
636 | 639 | PersistentPropertyAccessor<?> parentPropertyAccessor,
|
637 | 640 | boolean isParentObjectNew, PropertyFilter includeProperty) {
|
638 | 641 |
|
639 |
| - PropertyFilter.RelaxedPropertyPath startingPropertyPath = PropertyFilter.RelaxedPropertyPath.withRootType(neo4jPersistentEntity.getUnderlyingClass()); |
640 |
| - processNestedRelations(neo4jPersistentEntity, parentPropertyAccessor, isParentObjectNew, |
641 |
| - new NestedRelationshipProcessingStateMachine(originalInstance, internalId), includeProperty, startingPropertyPath); |
642 |
| - } |
643 |
| - |
644 |
| - private <T> T processRelations(Neo4jPersistentEntity<?> neo4jPersistentEntity, T originalInstance, |
645 |
| - PersistentPropertyAccessor<?> parentPropertyAccessor, |
646 |
| - boolean isParentObjectNew, PropertyFilter includeProperty) { |
647 |
| - |
648 | 642 | PropertyFilter.RelaxedPropertyPath startingPropertyPath = PropertyFilter.RelaxedPropertyPath.withRootType(neo4jPersistentEntity.getUnderlyingClass());
|
649 | 643 | return processNestedRelations(neo4jPersistentEntity, parentPropertyAccessor, isParentObjectNew,
|
650 |
| - new NestedRelationshipProcessingStateMachine(originalInstance), includeProperty, startingPropertyPath); |
| 644 | + new NestedRelationshipProcessingStateMachine(originalInstance, internalId), includeProperty, startingPropertyPath); |
651 | 645 | }
|
652 | 646 |
|
653 | 647 | private <T> T processNestedRelations(Neo4jPersistentEntity<?> sourceEntity, PersistentPropertyAccessor<?> propertyAccessor,
|
|
0 commit comments