Skip to content

Commit 6c4eef3

Browse files
committed
GH-2600 - Fix relproperties and simple relationship hydration.
Misses dynamic relationships right now.
1 parent 7799e59 commit 6c4eef3

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.function.Predicate;
3333
import java.util.function.Supplier;
3434
import java.util.stream.Collectors;
35+
import java.util.stream.Stream;
3536
import java.util.stream.StreamSupport;
3637

3738
import org.neo4j.driver.Value;
@@ -60,6 +61,7 @@
6061
import org.springframework.lang.NonNull;
6162
import org.springframework.lang.Nullable;
6263
import org.springframework.util.Assert;
64+
import org.springframework.util.CollectionUtils;
6365

6466
/**
6567
* @author Michael J. Simons
@@ -538,6 +540,42 @@ private AssociationHandler<Neo4jPersistentProperty> populateFrom(MapAccessor que
538540
boolean populatedScalarValue = !persistentProperty.isCollectionLike() && !persistentProperty.isMap()
539541
&& propertyValueNotNull;
540542

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+
541579
boolean propertyAlreadyPopulated = populatedCollection || populatedMap || populatedScalarValue;
542580

543581
// avoid unnecessary re-assignment of values

0 commit comments

Comments
 (0)