GH-2928 - Handle deeply nested relationships in fluent query correctly.

Closes #2928
This commit is contained in:
Gerrit Meier
2024-07-24 15:36:51 +02:00
parent edbda8a1e1
commit bef8acce53
3 changed files with 26 additions and 2 deletions

View File

@@ -15,7 +15,6 @@
*/
package org.springframework.data.neo4j.repository.query;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
@@ -126,7 +125,7 @@ abstract class FluentQuerySupport<R> {
Set<String> allPaths = new HashSet<>();
for (String property : projectingProperties) {
if (property.contains(".")) {
allPaths.addAll(Arrays.stream(property.split("\\.")).toList());
allPaths.add(property.substring(0, property.lastIndexOf(".")));
}
allPaths.add(property);
}

View File

@@ -2793,6 +2793,7 @@ class RepositoryIT {
CREATE (f2) -[:ARRIVES] ->(cdg)
CREATE (f3) -[:DEPARTS] ->(lax)
CREATE (f3) -[:ARRIVES] ->(lhr)
CREATE (f1) -[:NEXT_FLIGHT] ->(f2) -[:NEXT_FLIGHT] ->(f3)
""");
}
@@ -2882,6 +2883,22 @@ class RepositoryIT {
});
}
@Test
void findAllByExampleFluentCyclicRelationships(@Autowired FlightRepository repository) {
Example<Flight> example = Example.of(new Flight("FL 001", null, null),
ExampleMatcher.matchingAll().withIgnoreNullValues());
List<Flight> flights = repository.findBy(example,
q -> q.project("name", "nextFlight.name", "nextFlight.nextFlight.name").all());
assertThat(flights)
.hasSize(1)
.first().satisfies(p -> {
assertThat(p.getName()).isEqualTo("FL 001");
assertThat(p.getNextFlight().getName()).isEqualTo("FL 002");
assertThat(p.getNextFlight().getNextFlight().getName()).isEqualTo("FL 003");
});
}
@Test // GH-2343
void findAllByExampleFluentAs(@Autowired PersonRepository repository) {

View File

@@ -57,4 +57,12 @@ public class Flight {
public Airport getArrival() {
return arrival;
}
public Flight getNextFlight() {
return this.nextFlight;
}
public void setNextFlight(Flight nextFlight) {
this.nextFlight = nextFlight;
}
}