From 17a72bbd0154ba20e058fbdf9ebae41426c35b19 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Wed, 21 Oct 2015 11:24:21 +0200 Subject: [PATCH] DATAJPA-815 - Fixed application of sorting in combination with join aliases. The application of sort expressions is guarded by the detection of join aliases to potentially prefix the sort expression with the default alias. In case a raw property reference to sort by started with a join alias the property name wasn't prefixed. We now explicitly check for a start with the alias followed by a dot. --- .../data/jpa/repository/query/QueryUtils.java | 4 ++-- .../jpa/repository/query/QueryUtilsUnitTests.java | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 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 afe1a0b34..c748caa5d 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 @@ -222,7 +222,7 @@ public abstract class QueryUtils { /** * Returns the order clause for the given {@link Order}. Will prefix the clause with the given alias if the referenced - * property refers to a join alias. + * property refers to a join alias, i.e. starts with {@code $alias.}. * * @param joinAliases the join aliases of the original query. * @param alias the alias for the root entity. @@ -235,7 +235,7 @@ public abstract class QueryUtils { boolean qualifyReference = !property.contains("("); // ( indicates a function for (String joinAlias : joinAliases) { - if (property.startsWith(joinAlias)) { + if (property.startsWith(joinAlias.concat("."))) { qualifyReference = false; break; } 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 7b0376d24..f74699e8a 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 @@ -311,6 +311,18 @@ public class QueryUtilsUnitTests { assertThat(detectAlias("select \n u \n from \n User \nu"), is("u")); } + /** + * @see DATAJPA-815 + */ + @Test + public void doesPrefixPropertyWith() { + + String query = "from Cat c join Dog d"; + Sort sort = new Sort("dPropertyStartingWithJoinAlias"); + + assertThat(applySorting(query, sort, "c"), endsWith("order by c.dPropertyStartingWithJoinAlias asc")); + } + private void assertCountQuery(String originalQuery, String countQuery) { assertThat(createCountQueryFor(originalQuery), is(countQuery)); }