|
29 | 29 | import jakarta.persistence.criteria.From;
|
30 | 30 | import jakarta.persistence.criteria.Join;
|
31 | 31 | import jakarta.persistence.criteria.JoinType;
|
| 32 | +import jakarta.persistence.criteria.Nulls; |
32 | 33 | import jakarta.persistence.criteria.Path;
|
33 | 34 | import jakarta.persistence.metamodel.Attribute;
|
34 | 35 | import jakarta.persistence.metamodel.Attribute.PersistentAttributeType;
|
@@ -292,9 +293,8 @@ public static String applySorting(String query, Sort sort, @Nullable String alia
|
292 | 293 | Set<String> selectionAliases = getFunctionAliases(query);
|
293 | 294 | selectionAliases.addAll(getFieldAliases(query));
|
294 | 295 |
|
295 |
| - String orderClauses = sort.stream() |
296 |
| - .map(order -> getOrderClause(joinAliases, selectionAliases, alias, order)) |
297 |
| - .collect(Collectors.joining(", ")); |
| 296 | + String orderClauses = sort.stream().map(order -> getOrderClause(joinAliases, selectionAliases, alias, order)) |
| 297 | + .collect(Collectors.joining(", ")); |
298 | 298 |
|
299 | 299 | builder.append(orderClauses);
|
300 | 300 |
|
@@ -753,18 +753,25 @@ private static jakarta.persistence.criteria.Order toJpaOrder(Order order, From<?
|
753 | 753 | PropertyPath property = PropertyPath.from(order.getProperty(), from.getJavaType());
|
754 | 754 | Expression<?> expression = toExpressionRecursively(from, property);
|
755 | 755 |
|
756 |
| - if (order.getNullHandling() != Sort.NullHandling.NATIVE) { |
757 |
| - throw new UnsupportedOperationException("Applying Null Precedence using Criteria Queries is not yet supported."); |
758 |
| - } |
| 756 | + Nulls nulls = toNulls(order.getNullHandling()); |
759 | 757 |
|
760 | 758 | if (order.isIgnoreCase() && String.class.equals(expression.getJavaType())) {
|
761 | 759 | Expression<String> upper = cb.lower((Expression<String>) expression);
|
762 |
| - return order.isAscending() ? cb.asc(upper) : cb.desc(upper); |
| 760 | + return order.isAscending() ? cb.asc(upper, nulls) : cb.desc(upper, nulls); |
763 | 761 | } else {
|
764 |
| - return order.isAscending() ? cb.asc(expression) : cb.desc(expression); |
| 762 | + return order.isAscending() ? cb.asc(expression, nulls) : cb.desc(expression, nulls); |
765 | 763 | }
|
766 | 764 | }
|
767 | 765 |
|
| 766 | + private static Nulls toNulls(Sort.NullHandling nullHandling) { |
| 767 | + |
| 768 | + return switch (nullHandling) { |
| 769 | + case NULLS_LAST -> Nulls.LAST; |
| 770 | + case NULLS_FIRST -> Nulls.FIRST; |
| 771 | + case NATIVE -> Nulls.NONE; |
| 772 | + }; |
| 773 | + } |
| 774 | + |
768 | 775 | static <T> Expression<T> toExpressionRecursively(From<?, ?> from, PropertyPath property) {
|
769 | 776 | return toExpressionRecursively(from, property, false);
|
770 | 777 | }
|
|
0 commit comments