Skip to content

Commit 27f6866

Browse files
gregturnschauder
authored andcommitted
Properly handle ignoreCase for aliased fields.
When writing a select with a field alias, properly handle Sort.Order's ignoreCase. Closes #2280 Original pull request #2399
1 parent bca2adc commit 27f6866

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@
3838
import javax.persistence.criteria.Join;
3939
import javax.persistence.criteria.JoinType;
4040
import javax.persistence.metamodel.Attribute;
41+
import javax.persistence.metamodel.Attribute.PersistentAttributeType;
4142
import javax.persistence.metamodel.Bindable;
4243
import javax.persistence.metamodel.ManagedType;
4344
import javax.persistence.metamodel.PluralAttribute;
4445
import javax.persistence.metamodel.SingularAttribute;
45-
import javax.persistence.metamodel.Attribute.PersistentAttributeType;
4646

4747
import org.springframework.core.annotation.AnnotationUtils;
4848
import org.springframework.dao.InvalidDataAccessApiUsageException;
@@ -73,6 +73,7 @@
7373
* @author Mohammad Hewedy
7474
* @author Andriy Redko
7575
* @author Peter Großmann
76+
* @author Greg Turnquist
7677
*/
7778
public abstract class QueryUtils {
7879

@@ -291,7 +292,9 @@ private static String getOrderClause(Set<String> joinAliases, Set<String> select
291292
checkSortExpression(order);
292293

293294
if (selectionAlias.contains(property)) {
294-
return String.format("%s %s", property, toJpaDirection(order));
295+
return String.format("%s %s", //
296+
order.isIgnoreCase() ? String.format("lower(%s)", property) : property, //
297+
toJpaDirection(order));
295298
}
296299

297300
boolean qualifyReference = !property.contains("("); // ( indicates a function
@@ -465,6 +468,7 @@ public static String createCountQueryFor(String originalQuery) {
465468
* @param originalQuery must not be {@literal null}.
466469
* @param countProjection may be {@literal null}.
467470
* @return a query String to be used a count query for pagination. Guaranteed to be not {@literal null}.
471+
* @return a query String to be used a count query for pagination. Guaranteed to be not {@literal null}.
468472
* @since 1.6
469473
* @deprecated use {@link DeclaredQuery#deriveCountQuery(String, String)} instead.
470474
*/

src/test/java/org/springframework/data/jpa/repository/query/QueryUtilsUnitTests.java

+13
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
* @author Florian Lüdiger
4040
* @author Grégoire Druant
4141
* @author Mohammad Hewedy
42+
* @author Greg Turnquist
4243
*/
4344
class QueryUtilsUnitTests {
4445

@@ -444,6 +445,18 @@ void appliesSortCorrectlyForFieldAliases() {
444445
assertThat(fullQuery).endsWith("order by authorName asc");
445446
}
446447

448+
@Test // GH-2280
449+
void appliesOrderingCorrectlyForFieldAliasWithIgnoreCase() {
450+
451+
String query = "SELECT customer.id as id, customer.name as name FROM CustomerEntity customer";
452+
Sort sort = Sort.by(Order.by("name").ignoreCase());
453+
454+
String fullQuery = applySorting(query, sort);
455+
456+
assertThat(fullQuery).isEqualTo(
457+
"SELECT customer.id as id, customer.name as name FROM CustomerEntity customer order by lower(name) asc");
458+
}
459+
447460
@Test // DATAJPA-1061
448461
void appliesSortCorrectlyForFunctionAliases() {
449462

0 commit comments

Comments
 (0)