diff --git a/spring-data-r2dbc/src/main/java/org/springframework/data/r2dbc/repository/query/R2dbcQueryCreator.java b/spring-data-r2dbc/src/main/java/org/springframework/data/r2dbc/repository/query/R2dbcQueryCreator.java index b3ac4da4ec..d2399cad62 100644 --- a/spring-data-r2dbc/src/main/java/org/springframework/data/r2dbc/repository/query/R2dbcQueryCreator.java +++ b/spring-data-r2dbc/src/main/java/org/springframework/data/r2dbc/repository/query/R2dbcQueryCreator.java @@ -136,7 +136,7 @@ private PreparedOperation select(@Nullable Criteria criteria, Sort sort, Stat } if (sort.isSorted()) { - selectSpec = selectSpec.withSort(getSort(sort)); + selectSpec = selectSpec.withSort(sort); } if (tree.isDistinct()) { @@ -186,15 +186,4 @@ private Expression[] getSelectProjection() { return expressions.toArray(new Expression[0]); } - private Sort getSort(Sort sort) { - - RelationalPersistentEntity tableEntity = entityMetadata.getTableEntity(); - - List orders = sort.get().map(order -> { - RelationalPersistentProperty property = tableEntity.getRequiredPersistentProperty(order.getProperty()); - return order.isAscending() ? Sort.Order.asc(property.getName()) : Sort.Order.desc(property.getName()); - }).collect(Collectors.toList()); - - return Sort.by(orders); - } } diff --git a/spring-data-r2dbc/src/test/java/org/springframework/data/r2dbc/repository/query/PartTreeR2dbcQueryUnitTests.java b/spring-data-r2dbc/src/test/java/org/springframework/data/r2dbc/repository/query/PartTreeR2dbcQueryUnitTests.java index 8f48868391..a92180a20e 100644 --- a/spring-data-r2dbc/src/test/java/org/springframework/data/r2dbc/repository/query/PartTreeR2dbcQueryUnitTests.java +++ b/spring-data-r2dbc/src/test/java/org/springframework/data/r2dbc/repository/query/PartTreeR2dbcQueryUnitTests.java @@ -38,9 +38,10 @@ import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoSettings; import org.mockito.quality.Strictness; - import org.springframework.beans.factory.annotation.Value; import org.springframework.data.annotation.Id; +import org.springframework.data.domain.Sort; +import org.springframework.data.domain.Sort.Direction; import org.springframework.data.projection.SpelAwareProxyProjectionFactory; import org.springframework.data.r2dbc.convert.R2dbcConverter; import org.springframework.data.r2dbc.core.DefaultReactiveDataAccessStrategy; @@ -53,6 +54,7 @@ import org.springframework.data.relational.core.mapping.RelationalMappingContext; import org.springframework.data.relational.core.mapping.Table; import org.springframework.data.relational.core.sql.LockMode; +import org.springframework.data.relational.domain.SqlSort; import org.springframework.data.relational.repository.Lock; import org.springframework.data.relational.repository.query.RelationalParametersParameterAccessor; import org.springframework.data.repository.Repository; @@ -599,6 +601,21 @@ void throwsExceptionWhenConditionKeywordIsUnsupported() throws Exception { .isThrownBy(() -> createQuery(r2dbcQuery, getAccessor(queryMethod, new Object[0]))); } + @Test // GH-1548 + void allowsSortingByNonDomainProperties() throws Exception { + + R2dbcQueryMethod queryMethod = getQueryMethod("findAllByFirstName", String.class, Sort.class); + PartTreeR2dbcQuery r2dbcQuery = new PartTreeR2dbcQuery(queryMethod, operations, r2dbcConverter, dataAccessStrategy); + + PreparedOperation preparedOperation = createQuery(queryMethod, r2dbcQuery, "foo", Sort.by("foobar")); + PreparedOperationAssert.assertThat(preparedOperation) // + .orderBy("users.foobar ASC"); + + preparedOperation = createQuery(queryMethod, r2dbcQuery, "foo", SqlSort.unsafe(Direction.ASC, "sum(foobar)")); + PreparedOperationAssert.assertThat(preparedOperation) // + .orderBy("sum(foobar) ASC"); + } + @Test // GH-282 void throwsExceptionWhenInvalidNumberOfParameterIsGiven() throws Exception { @@ -960,6 +977,8 @@ interface UserRepository extends Repository { Flux findAllByIdIsEmpty(); + Flux findAllByFirstName(String firstName, Sort sort); + Flux findTop3ByFirstName(String firstName); Mono findFirstByFirstName(String firstName);