|
32 | 32 | import java.util.function.Predicate;
|
33 | 33 | import java.util.function.Supplier;
|
34 | 34 | import java.util.stream.Collectors;
|
| 35 | +import java.util.stream.Stream; |
35 | 36 | import java.util.stream.StreamSupport;
|
36 | 37 |
|
37 | 38 | import org.neo4j.driver.Value;
|
|
60 | 61 | import org.springframework.lang.NonNull;
|
61 | 62 | import org.springframework.lang.Nullable;
|
62 | 63 | import org.springframework.util.Assert;
|
| 64 | +import org.springframework.util.CollectionUtils; |
63 | 65 |
|
64 | 66 | /**
|
65 | 67 | * @author Michael J. Simons
|
@@ -538,6 +540,42 @@ private AssociationHandler<Neo4jPersistentProperty> populateFrom(MapAccessor que
|
538 | 540 | boolean populatedScalarValue = !persistentProperty.isCollectionLike() && !persistentProperty.isMap()
|
539 | 541 | && propertyValueNotNull;
|
540 | 542 |
|
| 543 | + if (populatedCollection) { |
| 544 | + createInstanceOfRelationships(persistentProperty, queryResult, (RelationshipDescription) association, baseDescription, relationshipsFromResult, nodesFromResult) |
| 545 | + .ifPresent(value -> { |
| 546 | + Collection<?> providedCollection = (Collection<?>) value; |
| 547 | + Collection<?> existingValue = (Collection<?>) propertyValue; |
| 548 | + Collection<Object> newValue = CollectionFactory.createCollection(existingValue.getClass(), providedCollection.size() + existingValue.size()); |
| 549 | + |
| 550 | + RelationshipDescription dings = (RelationshipDescription) association; |
| 551 | + Map<Object, Object> mergedValues = new HashMap<>(); |
| 552 | + for (Object existingValueInCollection : existingValue) { |
| 553 | + if (dings.hasRelationshipProperties()) { |
| 554 | + Object existingIdPropertyValue = ((Neo4jPersistentEntity<?>) dings.getRelationshipPropertiesEntity()).getPropertyAccessor(existingValueInCollection).getProperty(((Neo4jPersistentEntity<?>) dings.getRelationshipPropertiesEntity()).getIdProperty()); |
| 555 | + mergedValues.put(existingIdPropertyValue, existingValueInCollection); |
| 556 | + } else if (!dings.isDynamic()){ // should not happen because this is all inside populatedCollection (but better safe than sorry) |
| 557 | + Object existingIdPropertyValue = ((Neo4jPersistentEntity<?>) dings.getTarget()).getPropertyAccessor(existingValueInCollection).getProperty(((Neo4jPersistentEntity<?>) dings.getTarget()).getIdProperty()); |
| 558 | + mergedValues.put(existingIdPropertyValue, existingValueInCollection); |
| 559 | + } |
| 560 | + } |
| 561 | + for (Object providedValueInCollection : providedCollection) { |
| 562 | + if (dings.hasRelationshipProperties()) { |
| 563 | + Object existingIdPropertyValue = ((Neo4jPersistentEntity<?>) dings.getRelationshipPropertiesEntity()).getPropertyAccessor(providedValueInCollection).getProperty(((Neo4jPersistentEntity<?>) dings.getRelationshipPropertiesEntity()).getIdProperty()); |
| 564 | + mergedValues.put(existingIdPropertyValue, providedValueInCollection); |
| 565 | + } else if (!dings.isDynamic()){ // should not happen because this is all inside populatedCollection (but better safe than sorry) |
| 566 | + Object existingIdPropertyValue = ((Neo4jPersistentEntity<?>) dings.getTarget()).getPropertyAccessor(providedValueInCollection).getProperty(((Neo4jPersistentEntity<?>) dings.getTarget()).getIdProperty()); |
| 567 | + mergedValues.put(existingIdPropertyValue, providedValueInCollection); |
| 568 | + } |
| 569 | + } |
| 570 | + |
| 571 | + |
| 572 | + newValue.addAll(mergedValues.values()); |
| 573 | + propertyAccessor.setProperty(persistentProperty, newValue); |
| 574 | + }); |
| 575 | + } |
| 576 | + |
| 577 | + // todo dynamic relationships with maps |
| 578 | + |
541 | 579 | boolean propertyAlreadyPopulated = populatedCollection || populatedMap || populatedScalarValue;
|
542 | 580 |
|
543 | 581 | // avoid unnecessary re-assignment of values
|
|
0 commit comments