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.
This commit is contained in:
Oliver Gierke
2015-10-21 11:24:21 +02:00
parent 4cc0469708
commit 17a72bbd01
2 changed files with 14 additions and 2 deletions

View File

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

View File

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