diff --git a/pom.xml b/pom.xml
index 2c64717780..1539b78bae 100644
--- a/pom.xml
+++ b/pom.xml
@@ -5,7 +5,7 @@
org.springframework.data
spring-data-relational-parent
- 2.4.0-SNAPSHOT
+ 2.4.0-821-support-sort-null-handling-SNAPSHOT
pom
Spring Data Relational Parent
diff --git a/spring-data-jdbc-distribution/pom.xml b/spring-data-jdbc-distribution/pom.xml
index 0646c2846d..c55d9ccad0 100644
--- a/spring-data-jdbc-distribution/pom.xml
+++ b/spring-data-jdbc-distribution/pom.xml
@@ -14,7 +14,7 @@
org.springframework.data
spring-data-relational-parent
- 2.4.0-SNAPSHOT
+ 2.4.0-821-support-sort-null-handling-SNAPSHOT
../pom.xml
diff --git a/spring-data-jdbc/pom.xml b/spring-data-jdbc/pom.xml
index 11114a795e..f76dc28f46 100644
--- a/spring-data-jdbc/pom.xml
+++ b/spring-data-jdbc/pom.xml
@@ -6,7 +6,7 @@
4.0.0
spring-data-jdbc
- 2.4.0-SNAPSHOT
+ 2.4.0-821-support-sort-null-handling-SNAPSHOT
Spring Data JDBC
Spring Data module for JDBC repositories.
@@ -15,7 +15,7 @@
org.springframework.data
spring-data-relational-parent
- 2.4.0-SNAPSHOT
+ 2.4.0-821-support-sort-null-handling-SNAPSHOT
diff --git a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/SqlGenerator.java b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/SqlGenerator.java
index 80fa379a49..a95c9b1933 100644
--- a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/SqlGenerator.java
+++ b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/SqlGenerator.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2017-2021 the original author or authors.
+ * Copyright 2017-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -51,6 +51,7 @@
* @author Milan Milanov
* @author Myeonghyeon Lee
* @author Mikhail Polivakha
+ * @author Chirag Tailor
*/
class SqlGenerator {
@@ -714,7 +715,7 @@ private OrderByField orderToOrderByField(Sort.Order order) {
SqlIdentifier columnName = this.entity.getRequiredPersistentProperty(order.getProperty()).getColumnName();
Column column = Column.create(columnName, this.getTable());
- return OrderByField.from(column, order.getDirection());
+ return OrderByField.from(column, order.getDirection()).withNullHandling(order.getNullHandling());
}
/**
diff --git a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/JdbcAggregateTemplateIntegrationTests.java b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/JdbcAggregateTemplateIntegrationTests.java
index 4153ac6b3b..fa79aec3bd 100644
--- a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/JdbcAggregateTemplateIntegrationTests.java
+++ b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/JdbcAggregateTemplateIntegrationTests.java
@@ -257,6 +257,21 @@ void saveAndLoadManyEntitiesWithReferencedEntitySortedAndPaged() {
.containsExactly("Star");
}
+ @Test // GH-821
+ @EnabledOnFeature({SUPPORTS_QUOTED_IDS, SUPPORTS_NULL_HANDLING})
+ void saveAndLoadManyEntitiesWithReferencedEntitySortedWithNullHandling() {
+
+ template.save(createLegoSet(null));
+ template.save(createLegoSet("Star"));
+ template.save(createLegoSet("Frozen"));
+
+ Iterable reloadedLegoSets = template.findAll(LegoSet.class, Sort.by(new Sort.Order(Sort.Direction.ASC, "name", Sort.NullHandling.NULLS_LAST)));
+
+ assertThat(reloadedLegoSets) //
+ .extracting("name") //
+ .containsExactly("Frozen", "Star", null);
+ }
+
@Test // DATAJDBC-112
@EnabledOnFeature(SUPPORTS_QUOTED_IDS)
void saveAndLoadManyEntitiesByIdWithReferencedEntity() {
diff --git a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/convert/SqlGeneratorUnitTests.java b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/convert/SqlGeneratorUnitTests.java
index 363d9c9d1d..7ea24dfebe 100644
--- a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/convert/SqlGeneratorUnitTests.java
+++ b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/convert/SqlGeneratorUnitTests.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2017-2021 the original author or authors.
+ * Copyright 2017-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -25,7 +25,6 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
-
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.ReadOnlyProperty;
import org.springframework.data.annotation.Version;
@@ -64,6 +63,7 @@
* @author Milan Milanov
* @author Myeonghyeon Lee
* @author Mikhail Polivakha
+ * @author Chirag Tailor
*/
class SqlGeneratorUnitTests {
@@ -245,6 +245,26 @@ void findAllSortedByMultipleFields() {
"x_other ASC");
}
+ @Test // GH-821
+ void findAllSortedWithNullHandling_resolvesNullHandlingWhenDialectSupportsIt() {
+
+ SqlGenerator sqlGenerator = createSqlGenerator(DummyEntity.class, PostgresDialect.INSTANCE);
+
+ String sql = sqlGenerator.getFindAll(Sort.by(new Sort.Order(Sort.Direction.ASC, "name", Sort.NullHandling.NULLS_LAST)));
+
+ assertThat(sql).contains("ORDER BY \"dummy_entity\".\"x_name\" ASC NULLS LAST");
+ }
+
+ @Test // GH-821
+ void findAllSortedWithNullHandling_ignoresNullHandlingWhenDialectDoesNotSupportIt() {
+
+ SqlGenerator sqlGenerator = createSqlGenerator(DummyEntity.class, SqlServerDialect.INSTANCE);
+
+ String sql = sqlGenerator.getFindAll(Sort.by(new Sort.Order(Sort.Direction.ASC, "name", Sort.NullHandling.NULLS_LAST)));
+
+ assertThat(sql).endsWith("ORDER BY dummy_entity.x_name ASC");
+ }
+
@Test // DATAJDBC-101
void findAllPagedByUnpaged() {
diff --git a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/testing/TestDatabaseFeatures.java b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/testing/TestDatabaseFeatures.java
index 6e3e65a484..d252745034 100644
--- a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/testing/TestDatabaseFeatures.java
+++ b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/testing/TestDatabaseFeatures.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2020-2021 the original author or authors.
+ * Copyright 2020-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -29,6 +29,7 @@
* presence or absence of features in tests.
*
* @author Jens Schauder
+ * @author Chirag Tailor
*/
public class TestDatabaseFeatures {
@@ -83,6 +84,10 @@ private void supportsMultiDimensionalArrays() {
assumeThat(database).isNotIn(Database.H2, Database.Hsql);
}
+ private void supportsNullHandling() {
+ assumeThat(database).isNotIn(Database.MySql, Database.MariaDb, Database.SqlServer);
+ }
+
public void databaseIs(Database database) {
assumeThat(this.database).isEqualTo(database);
}
@@ -115,6 +120,7 @@ public enum Feature {
SUPPORTS_ARRAYS(TestDatabaseFeatures::supportsArrays), //
SUPPORTS_GENERATED_IDS_IN_REFERENCED_ENTITIES(TestDatabaseFeatures::supportsGeneratedIdsInReferencedEntities), //
SUPPORTS_NANOSECOND_PRECISION(TestDatabaseFeatures::supportsNanosecondPrecision), //
+ SUPPORTS_NULL_HANDLING(TestDatabaseFeatures::supportsNullHandling),
IS_POSTGRES(f -> f.databaseIs(Database.PostgreSql)), //
IS_HSQL(f -> f.databaseIs(Database.Hsql));
diff --git a/spring-data-relational/pom.xml b/spring-data-relational/pom.xml
index a6eb48c891..f862f08876 100644
--- a/spring-data-relational/pom.xml
+++ b/spring-data-relational/pom.xml
@@ -6,7 +6,7 @@
4.0.0
spring-data-relational
- 2.4.0-SNAPSHOT
+ 2.4.0-821-support-sort-null-handling-SNAPSHOT
Spring Data Relational
Spring Data Relational support
@@ -14,7 +14,7 @@
org.springframework.data
spring-data-relational-parent
- 2.4.0-SNAPSHOT
+ 2.4.0-821-support-sort-null-handling-SNAPSHOT
diff --git a/spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect/AbstractDialect.java b/spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect/AbstractDialect.java
index 6563df66a3..7043a9f734 100644
--- a/spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect/AbstractDialect.java
+++ b/spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect/AbstractDialect.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2019-2021 the original author or authors.
+ * Copyright 2019-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
import java.util.OptionalLong;
import java.util.function.Function;
+import org.springframework.data.domain.Sort;
import org.springframework.data.relational.core.sql.LockMode;
import org.springframework.data.relational.core.sql.LockOptions;
import org.springframework.data.relational.core.sql.Select;
@@ -28,6 +29,7 @@
*
* @author Mark Paluch
* @author Myeonghyeon Lee
+ * @author Chirag Tailor
* @since 1.1
*/
public abstract class AbstractDialect implements Dialect {
@@ -42,7 +44,7 @@ public SelectRenderContext getSelectContext() {
Function