Skip to content

Commit aeb8d68

Browse files
alim-nmp911de
authored andcommitted
Support NullHandling in QueryUtils.
Closes #3811 Signed-off-by: Alim <[email protected]>
1 parent 912e414 commit aeb8d68

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
* @author Pranav HS
8888
* @author Eduard Dudar
8989
* @author Yanming Zhou
90+
* @author Alim Naizabek
9091
*/
9192
public abstract class QueryUtils {
9293

@@ -430,7 +431,13 @@ static Set<String> getFunctionAliases(String query) {
430431
}
431432

432433
private static String toJpaDirection(Order order) {
433-
return order.getDirection().name().toLowerCase(Locale.US);
434+
String direction = order.getDirection().name().toLowerCase(Locale.US);
435+
if (order.getNullHandling() == Sort.NullHandling.NULLS_FIRST) {
436+
direction += " nulls first";
437+
} else if (order.getNullHandling() == Sort.NullHandling.NULLS_LAST) {
438+
direction += " nulls last";
439+
}
440+
return direction;
434441
}
435442

436443
/**

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/query/DefaultQueryEnhancerUnitTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,14 @@ void shouldApplySorting() {
4848

4949
assertThat(sql).isEqualTo("SELECT e FROM Employee e order by e.foo asc, e.bar asc");
5050
}
51+
52+
@Test
53+
void shouldApplySortingWithNullHandling() {
54+
55+
QueryEnhancer enhancer = createQueryEnhancer(DeclaredQuery.of("SELECT e FROM Employee e", true));
56+
57+
String sql = enhancer.applySorting(Sort.by(Sort.Order.asc("foo").nullsFirst(), Sort.Order.asc("bar").nullsLast()));
58+
59+
assertThat(sql).isEqualTo("SELECT e FROM Employee e order by e.foo asc nulls first, e.bar asc nulls last");
60+
}
5161
}

0 commit comments

Comments
 (0)