From 9ce31b9da83aaeae91d3f3257a6d3e98fabbd8e8 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Sun, 4 Sep 2016 18:47:22 +0200 Subject: [PATCH] DATAJPA-960 - Fixed alias detection for queries not containing an alias. We're now more lenient against manually defined queries that do not contain aliases when adding order by clauses to them. The alias detection now doesn't accidentally pick up "where" anymore in case no primary alias is declared and the code applying the order by clause only qualifies the expressions created if there actually is an alias in the first place. --- .../data/jpa/repository/query/QueryUtils.java | 8 ++++---- .../data/jpa/repository/query/QueryUtilsUnitTests.java | 9 +++++++++ 2 files changed, 13 insertions(+), 4 deletions(-) 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 f67a2ba39..67b9883d8 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 @@ -107,7 +107,7 @@ public abstract class QueryUtils { builder.append(IDENTIFIER_GROUP); // Entity name, can be qualified (any builder.append("(?:\\sas)*"); // exclude possible "as" keyword builder.append("(?:\\s)+"); // at least one space separating - builder.append("(\\w*)"); // the actual alias + builder.append("(?!(?:where))(\\w*)"); // the actual alias ALIAS_MATCH = compile(builder.toString(), CASE_INSENSITIVE); @@ -182,8 +182,7 @@ public abstract class QueryUtils { * @return */ public static String applySorting(String query, Sort sort) { - - return applySorting(query, sort, DEFAULT_ALIAS); + return applySorting(query, sort, detectAlias(query)); } /** @@ -242,7 +241,8 @@ public abstract class QueryUtils { } } - String reference = qualifyReference ? String.format("%s.%s", alias, property) : property; + String reference = qualifyReference && StringUtils.hasText(alias) ? String.format("%s.%s", alias, property) + : property; String wrapped = order.isIgnoreCase() ? String.format("lower(%s)", reference) : reference; return String.format("%s %s", wrapped, toJpaDirection(order)); 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 f74699e8a..9d56d05f8 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 @@ -323,6 +323,15 @@ public class QueryUtilsUnitTests { assertThat(applySorting(query, sort, "c"), endsWith("order by c.dPropertyStartingWithJoinAlias asc")); } + /** + * @see DATAJPA-960 + */ + @Test + public void doesNotQualifySortIfNoAliasDetected() { + assertThat(applySorting("from mytable where ?1 is null", new Sort("firstname")), + endsWith("order by firstname asc")); + } + private void assertCountQuery(String originalQuery, String countQuery) { assertThat(createCountQueryFor(originalQuery), is(countQuery)); }