diff --git a/src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java b/src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java index 84a8b70e0e..213e21f03d 100644 --- a/src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java +++ b/src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java @@ -38,11 +38,11 @@ import javax.persistence.criteria.Join; import javax.persistence.criteria.JoinType; import javax.persistence.metamodel.Attribute; +import javax.persistence.metamodel.Attribute.PersistentAttributeType; import javax.persistence.metamodel.Bindable; import javax.persistence.metamodel.ManagedType; import javax.persistence.metamodel.PluralAttribute; import javax.persistence.metamodel.SingularAttribute; -import javax.persistence.metamodel.Attribute.PersistentAttributeType; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.dao.InvalidDataAccessApiUsageException; @@ -73,6 +73,7 @@ * @author Mohammad Hewedy * @author Andriy Redko * @author Peter Großmann + * @author Greg Turnquist */ public abstract class QueryUtils { @@ -291,7 +292,9 @@ private static String getOrderClause(Set joinAliases, Set select checkSortExpression(order); if (selectionAlias.contains(property)) { - return String.format("%s %s", property, toJpaDirection(order)); + return String.format("%s %s", // + order.isIgnoreCase() ? String.format("lower(%s)", property) : property, // + toJpaDirection(order)); } boolean qualifyReference = !property.contains("("); // ( indicates a function @@ -465,6 +468,7 @@ public static String createCountQueryFor(String originalQuery) { * @param originalQuery must not be {@literal null}. * @param countProjection may be {@literal null}. * @return a query String to be used a count query for pagination. Guaranteed to be not {@literal null}. + * @return a query String to be used a count query for pagination. Guaranteed to be not {@literal null}. * @since 1.6 * @deprecated use {@link DeclaredQuery#deriveCountQuery(String, String)} instead. */ diff --git a/src/test/java/org/springframework/data/jpa/repository/query/QueryUtilsUnitTests.java b/src/test/java/org/springframework/data/jpa/repository/query/QueryUtilsUnitTests.java index 9040cfabf5..00794830ae 100644 --- a/src/test/java/org/springframework/data/jpa/repository/query/QueryUtilsUnitTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/query/QueryUtilsUnitTests.java @@ -39,6 +39,7 @@ * @author Florian Lüdiger * @author Grégoire Druant * @author Mohammad Hewedy + * @author Greg Turnquist */ class QueryUtilsUnitTests { @@ -444,6 +445,18 @@ void appliesSortCorrectlyForFieldAliases() { assertThat(fullQuery).endsWith("order by authorName asc"); } + @Test // GH-2280 + void appliesOrderingCorrectlyForFieldAliasWithIgnoreCase() { + + String query = "SELECT customer.id as id, customer.name as name FROM CustomerEntity customer"; + Sort sort = Sort.by(Order.by("name").ignoreCase()); + + String fullQuery = applySorting(query, sort); + + assertThat(fullQuery).isEqualTo( + "SELECT customer.id as id, customer.name as name FROM CustomerEntity customer order by lower(name) asc"); + } + @Test // DATAJPA-1061 void appliesSortCorrectlyForFunctionAliases() {