Skip to content

Commit ebd60f5

Browse files
committed
GH-2884 - Introduce composite property sort feature.
1 parent f7645e8 commit ebd60f5

File tree

4 files changed

+48
-13
lines changed

4 files changed

+48
-13
lines changed

src/main/java/org/springframework/data/neo4j/repository/query/CypherAdapterUtils.java

+14-12
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package org.springframework.data.neo4j.repository.query;
1717

1818
import static org.neo4j.cypherdsl.core.Cypher.property;
19-
import static org.neo4j.cypherdsl.core.Cypher.raw;
2019

2120
import java.util.Collection;
2221
import java.util.HashMap;
@@ -35,7 +34,6 @@
3534
import org.neo4j.cypherdsl.core.SortItem;
3635
import org.neo4j.cypherdsl.core.StatementBuilder;
3736
import org.neo4j.cypherdsl.core.SymbolicName;
38-
import org.neo4j.cypherdsl.core.internal.SchemaNames;
3937
import org.neo4j.driver.Value;
4038
import org.springframework.data.domain.KeysetScrollPosition;
4139
import org.springframework.data.domain.Pageable;
@@ -66,21 +64,25 @@ public static Function<Sort.Order, SortItem> sortAdapterFor(NodeDescription<?> n
6664
return order -> {
6765

6866
String domainProperty = order.getProperty();
69-
String rawDomainProperty = order.getProperty();
70-
boolean propertyIsQualified = domainProperty.contains(".");
67+
boolean propertyIsQualifiedOrComposite = domainProperty.contains(".");
7168
SymbolicName root;
72-
if (!propertyIsQualified) {
69+
if (!propertyIsQualifiedOrComposite) {
7370
root = Constants.NAME_OF_TYPED_ROOT_NODE.apply(nodeDescription);
7471
} else {
75-
int indexOfSeparator = domainProperty.indexOf(".");
76-
root = Cypher.name(domainProperty.substring(0, indexOfSeparator));
77-
domainProperty = domainProperty.substring(indexOfSeparator + 1);
72+
// need to check first if this is really a qualified name or the "qualifier" is a composite property
73+
if (nodeDescription.getGraphProperty(domainProperty.split("\\.")[0]).isEmpty()) {
74+
int indexOfSeparator = domainProperty.indexOf(".");
75+
root = Cypher.name(domainProperty.substring(0, indexOfSeparator));
76+
domainProperty = domainProperty.substring(indexOfSeparator + 1);
77+
} else {
78+
root = Constants.NAME_OF_TYPED_ROOT_NODE.apply(nodeDescription);
79+
}
7880
}
7981

8082
var optionalGraphProperty = nodeDescription.getGraphProperty(domainProperty);
8183
// try to resolve if this is a composite property
8284
if (optionalGraphProperty.isEmpty()) {
83-
var domainPropertyPrefix = rawDomainProperty.split("\\.")[0];
85+
var domainPropertyPrefix = domainProperty.split("\\.")[0];
8486
optionalGraphProperty = nodeDescription.getGraphProperty(domainPropertyPrefix);
8587
}
8688
if (optionalGraphProperty.isEmpty()) {
@@ -91,10 +93,10 @@ public static Function<Sort.Order, SortItem> sortAdapterFor(NodeDescription<?> n
9193
if (graphProperty.isInternalIdProperty()) {
9294
// Not using the id expression here, as the root will be referring to the constructed map being returned.
9395
expression = property(root, Constants.NAME_OF_INTERNAL_ID);
94-
} else if (graphProperty.isComposite() && !rawDomainProperty.contains(".")) {
95-
throw new IllegalStateException(String.format("Cannot order by composite property: '%s'. Only ordering by its nested fields is allowed.", rawDomainProperty));
96+
} else if (graphProperty.isComposite() && !domainProperty.contains(".")) {
97+
throw new IllegalStateException(String.format("Cannot order by composite property: '%s'. Only ordering by its nested fields is allowed.", domainProperty));
9698
} else if (graphProperty.isComposite()) {
97-
expression = property(root, rawDomainProperty);
99+
expression = property(root, domainProperty);
98100
} else {
99101
expression = property(root, graphProperty.getPropertyName());
100102
if (order.isIgnoreCase()) {

src/test/java/org/springframework/data/neo4j/integration/issues/IssuesIT.java

+16
Original file line numberDiff line numberDiff line change
@@ -281,10 +281,12 @@ protected void prepareIndividual(
281281
CityModel aachen = new CityModel();
282282
aachen.setName("Aachen");
283283
aachen.setExoticProperty("Cars");
284+
aachen.setCompositeProperty(Map.of("language", "German"));
284285

285286
CityModel utrecht = new CityModel();
286287
utrecht.setName("Utrecht");
287288
utrecht.setExoticProperty("Bikes");
289+
utrecht.setCompositeProperty(Map.of("language", "Dutch"));
288290

289291
cityModelRepository.saveAll(List.of(aachen, utrecht));
290292
}
@@ -736,6 +738,20 @@ public void testCityModelProjectionPersistence(
736738
assertThat(reloaded.getCityEmployees()).hasSize(1);
737739
}
738740

741+
@Test
742+
@Tag("GH-2884")
743+
void sortByCompositeProperty(@Autowired CityModelRepository repository) {
744+
Sort sort = Sort.by(Sort.Order.asc("compositeProperty.language"));
745+
List<CityModel> models = repository.findAll(sort);
746+
747+
assertThat(models).extracting("name").containsExactlyInAnyOrder("Utrecht", "Aachen");
748+
749+
Sort sortDesc = Sort.by(Sort.Order.desc("compositeProperty.language"));
750+
models = repository.findAll(sortDesc);
751+
752+
assertThat(models).extracting("name").containsExactlyInAnyOrder("Aachen", "Utrecht");
753+
}
754+
739755
@Test
740756
@Tag("GH-2493")
741757
void saveOneShouldWork(@Autowired Driver driver, @Autowired BookmarkCapture bookmarkCapture,

src/test/java/org/springframework/data/neo4j/integration/issues/gh2474/CityModel.java

+13
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.neo4j.integration.issues.gh2474;
1717

18+
import org.springframework.data.neo4j.core.schema.CompositeProperty;
1819
import org.springframework.data.neo4j.core.schema.GeneratedValue;
1920
import org.springframework.data.neo4j.core.schema.Id;
2021
import org.springframework.data.neo4j.core.schema.Node;
@@ -23,6 +24,7 @@
2324

2425
import java.util.ArrayList;
2526
import java.util.List;
27+
import java.util.Map;
2628
import java.util.UUID;
2729

2830
/**
@@ -48,6 +50,9 @@ public class CityModel {
4850
@Property("exotic.property")
4951
private String exoticProperty;
5052

53+
@CompositeProperty
54+
private Map<String, String> compositeProperty;
55+
5156
public CityModel() {
5257
}
5358

@@ -99,6 +104,14 @@ public void setExoticProperty(String exoticProperty) {
99104
this.exoticProperty = exoticProperty;
100105
}
101106

107+
public Map<String, String> getCompositeProperty() {
108+
return compositeProperty;
109+
}
110+
111+
public void setCompositeProperty(Map<String, String> compositeProperty) {
112+
this.compositeProperty = compositeProperty;
113+
}
114+
102115
public boolean equals(final Object o) {
103116
if (o == this) {
104117
return true;

src/test/java/org/springframework/data/neo4j/repository/query/CypherAdapterUtilsTest.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ void sortByCompositePropertyField() {
6969
var mappingContext = new Neo4jMappingContext();
7070
var entity = mappingContext.getPersistentEntity(ScrollingEntity.class);
7171

72-
System.out.println(CypherAdapterUtils.sortAdapterFor(entity).apply(Sort.Order.asc("basicComposite.blubb")));
72+
var sortItem = CypherAdapterUtils.sortAdapterFor(entity).apply(Sort.Order.asc("basicComposite.blubb"));
73+
var node = Cypher.anyNode("scrollingEntity");
74+
var statement = Cypher.match(node).returning(node).orderBy(sortItem).build();
75+
assertThat(Renderer.getDefaultRenderer().render(statement))
76+
.isEqualTo("MATCH (scrollingEntity) RETURN scrollingEntity ORDER BY scrollingEntity.`basicComposite.blubb`");
7377
}
7478

7579
@Test

0 commit comments

Comments
 (0)