Skip to content

Commit aab50ea

Browse files
committed
GH-2458 - Preserve ordered related node collection.
Closes #2458
1 parent da580e0 commit aab50ea

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
@@ -674,7 +674,7 @@ private Collection<Node> extractMatchingNodes(Collection<Node> allNodesInResult,
674674
Predicate<Node> onlyWithMatchingLabels = n -> n.hasLabel(targetLabel);
675675
return allNodesInResult.stream()
676676
.filter(onlyWithMatchingLabels)
677-
.collect(Collectors.toSet());
677+
.collect(Collectors.toList());
678678
}
679679

680680
private Collection<Node> extractNodes(MapAccessor allValues) {
@@ -701,7 +701,7 @@ private Collection<Relationship> extractMatchingRelationships(Collection<Relatio
701701
Predicate<Relationship> onlyWithMatchingType = r -> r.type().equals(typeOfRelationship) || relationshipDescription.isDynamic();
702702
return relationshipsFromResult.stream()
703703
.filter(onlyWithMatchingType.and(relationshipPredicate))
704-
.collect(Collectors.toSet());
704+
.collect(Collectors.toList());
705705
}
706706

707707
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)