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)); }