Skip to content

Commit e81bb98

Browse files
committed
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.
1 parent 83a6ebe commit e81bb98

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ public static String applySorting(String query, Sort sort, String alias) {
222222

223223
/**
224224
* Returns the order clause for the given {@link Order}. Will prefix the clause with the given alias if the referenced
225-
* property refers to a join alias.
225+
* property refers to a join alias, i.e. starts with {@code $alias.}.
226226
*
227227
* @param joinAliases the join aliases of the original query.
228228
* @param alias the alias for the root entity.
@@ -235,7 +235,7 @@ private static String getOrderClause(Set<String> joinAliases, String alias, Orde
235235
boolean qualifyReference = !property.contains("("); // ( indicates a function
236236

237237
for (String joinAlias : joinAliases) {
238-
if (property.startsWith(joinAlias)) {
238+
if (property.startsWith(joinAlias.concat("."))) {
239239
qualifyReference = false;
240240
break;
241241
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,18 @@ public void detectsAliasInQueryContainingLineBreaks() {
311311
assertThat(detectAlias("select \n u \n from \n User \nu"), is("u"));
312312
}
313313

314+
/**
315+
* @see DATAJPA-815
316+
*/
317+
@Test
318+
public void doesPrefixPropertyWith() {
319+
320+
String query = "from Cat c join Dog d";
321+
Sort sort = new Sort("dPropertyStartingWithJoinAlias");
322+
323+
assertThat(applySorting(query, sort, "c"), endsWith("order by c.dPropertyStartingWithJoinAlias asc"));
324+
}
325+
314326
private void assertCountQuery(String originalQuery, String countQuery) {
315327
assertThat(createCountQueryFor(originalQuery), is(countQuery));
316328
}

0 commit comments

Comments
 (0)