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:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user