Skip to content

Commit f5ae77f

Browse files
committed
GH-2458 - Preserve ordered related node collection.
Closes #2458
1 parent 487a615 commit f5ae77f

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ private Collection<Node> extractMatchingNodes(Collection<Node> allNodesInResult,
646646
Predicate<Node> onlyWithMatchingLabels = n -> n.hasLabel(targetLabel);
647647
return allNodesInResult.stream()
648648
.filter(onlyWithMatchingLabels)
649-
.collect(Collectors.toSet());
649+
.collect(Collectors.toList());
650650
}
651651

652652
private Collection<Node> extractNodes(MapAccessor allValues) {
@@ -671,7 +671,7 @@ private Collection<Relationship> extractMatchingRelationships(Collection<Relatio
671671
Predicate<Relationship> onlyWithMatchingType = r -> r.type().equals(typeOfRelationship) || relationshipDescription.isDynamic();
672672
return relationshipsFromResult.stream()
673673
.filter(onlyWithMatchingType.and(relationshipPredicate))
674-
.collect(Collectors.toSet());
674+
.collect(Collectors.toList());
675675
}
676676

677677
private Collection<Relationship> extractRelationships(MapAccessor allValues) {

src/test/java/org/springframework/data/neo4j/integration/movies/imperative/AdvancedMappingIT.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,12 @@ interface MovieRepository extends Neo4jRepository<Movie, String> {
149149
MovieWithSequelProjection findProjectionByTitleAndDescription(String title, String description);
150150

151151
MovieWithSequelEntity findByTitleAndDescription(String title, String description);
152+
153+
@Query("MATCH (m:Movie{title:'The Matrix'})<-[a:ACTED_IN]-(p:Person) WITH a,p,m order by p.name return m, collect(a), collect(p)")
154+
Movie findMatrixWithSortedAscActors();
155+
156+
@Query("MATCH (m:Movie{title:'The Matrix'})<-[a:ACTED_IN]-(p:Person) WITH a,p,m order by p.name DESC return m, collect(a), collect(p)")
157+
Movie findMatrixWithSortedDescActors();
152158
}
153159

154160
@Test // GH-1906
@@ -457,6 +463,19 @@ void projectDirectCycleEntityReference(@Autowired MovieRepository movieRepositor
457463
assertThat(secondSequel.getActors()).isNotEmpty();
458464
}
459465

466+
@Test // GH-2458
467+
void findPreservesOrderFromResultAscInRelationshipList(@Autowired MovieRepository repository) {
468+
assertThat(repository.findMatrixWithSortedAscActors().getActors()).extracting("person").extracting("name")
469+
.containsExactly("Carrie-Anne Moss", "Emil Eifrem", "Gloria Foster", "Hugo Weaving", "Keanu Reeves",
470+
"Laurence Fishburne");
471+
}
472+
@Test // GH-2458
473+
void findPreservesOrderFromResultDescInRelationshipList(@Autowired MovieRepository repository) {
474+
assertThat(repository.findMatrixWithSortedDescActors().getActors()).extracting("person").extracting("name")
475+
.containsExactly("Laurence Fishburne", "Keanu Reeves", "Hugo Weaving", "Gloria Foster", "Emil Eifrem",
476+
"Carrie-Anne Moss");
477+
}
478+
460479
@Configuration
461480
@EnableTransactionManagement
462481
@EnableNeo4jRepositories(considerNestedRepositories = true)

0 commit comments

Comments
 (0)