Skip to content

Sort by a path that starts with the alias of the queried class - #17073

Open
jdaugherty wants to merge 31 commits into
apache:7.0.xfrom
jdaugherty:fix/16853-13-sort-root-alias
Open

jdaugherty wants to merge 31 commits into
apache:7.0.xfrom
jdaugherty:fix/16853-13-sort-root-alias

Conversation

@jdaugherty

Copy link
Copy Markdown
Contributor

Description

Part 13 of the split of #16853, as agreed there and in the dev@ thread Fixes that change results on patch and minor branches. Relates to #15589.

Stacked on #17072. Until that merges, this pull request also shows the commits below it. The change to review is the last commit, e359719.

A sort that starts with the alias that a detached criteria gives the
queried class, as in
new DetachedCriteria(Team, 't').list(sort: 't.name'), was ignored: the
query returned its rows unsorted, without an error. Such a sort now
sorts by the rest of its path, in where queries, detached criteria,
list, the example queries and the sort argument of a criteria query.

Results that change

  • What changes: A sort that starts with the alias that a detached criteria gives the queried class, as in new DetachedCriteria(Book, 'b').list(sort: 'b.title'), sorts the results. It was ignored. The rows are the same.
  • How to get the old result: Remove the sort.
  • Setting: No setting. The call asks for the sort and it was dropped.

The upgrade guide has a note under Grails 7.0.18, and the upgrade skill has the same entry.

Verification

The association sort and join specs of grails-data-hibernate5-core pass on this branch, with grails.gorm.leftJoinAssociationSort off and on, and codeStyle passes for the touched modules. The full build was not run locally.

Contributor Checklist

Please review the following checklist before submitting your pull request. Pull requests that do not meet these requirements may be closed without review.

Issue and Scope

  • This PR is linked to an existing issue that has been acknowledged or approved by the project team. If no approved issue exists, please give background on why this change is necessary. Tickets are preferred for release change log history.
  • This PR addresses the complete scope of the linked issue. Partial implementations or unfinished work should not be submitted for review.
  • This PR contains a single, focused change. Unrelated changes should be submitted as separate pull requests.
  • This PR targets the correct branch for the type of change:
    • Patch release branches (e.g., 7.0.x): Bug fixes only. No new features or API changes.
    • Minor release branches (e.g., 7.1.x): New features are welcome, but breaking existing APIs must be avoided.
    • Major release branches (e.g., 8.0.x): Reserved for major changes. Breaking API changes are permitted.

Code Quality

  • I have added or updated tests that cover the changes introduced in this PR. All code contributions are expected to include appropriate test coverage.
  • I have verified that all existing tests pass by running ./gradlew build --rerun-tasks.
  • My code follows the project's code style guidelines. I have run ./gradlew codeStyle and resolved any violations. See Code Style for details.
  • This PR does not include mass reformatting, style-only changes, or large-scale refactoring unless it was explicitly approved in the linked issue. Unsolicited reformatting will not be accepted.
  • If generative AI tooling was used in preparing this contribution, a quality model was used to ensure contributions are consistent with the project's quality standards.

Licensing and Attribution

Documentation

  • If this PR introduces user-facing changes, I have included or updated the relevant documentation.
  • If this PR adds a new feature, I have updated the What's New section of the Grails Guide.
  • If this PR introduces breaking changes or changes that require user action during an upgrade, I have updated the Upgrade Notes for the corresponding version in the Grails Guide.
  • The PR description clearly explains what was changed and why.

First-time contributors: Please read our Contributing Guide before submitting.
Pull requests that appear to be auto-generated, incomplete, or unrelated to an approved issue may be
closed to help maintainers focus on reviewed and planned work. We appreciate your understanding.

matrei and others added 17 commits November 19, 2026 09:30
Sorting a where query by a property of an association, such as
sort: 'club.name', failed with a QueryException when the query also
had criteria on that association. The criteria join the association
under a generated alias, and the order was added with the association
path, which Hibernate cannot resolve. The order now uses the alias of
the join.
A sort by a path through more than one association, such as
owner.employer.name, is still not supported.
A sort by a path that crosses more than one association, such as
team.club.name, failed with a QueryException whether or not the where
query joined the nested association, because order() split the path at
its first dot only. The sort property is now qualified with the alias of
the last association on its path: an association the query has already
joined is reused, and one it has not joined yet is joined for the sort
and registered like any other join. A sort by two properties of the same
association no longer fails with "duplicate association path" either,
since the sort join is registered instead of created anew for every
order.
Since order() walks the whole sort path, a sort by the identifier of an
association that follows another one on the path, such as team.club.id,
joined that last association too. The join is an inner join unless fetch
requests another type, so the sort left out the rows where the
association is null. Before, only the first association was joined and
Hibernate read club.id from the foreign key of team, which kept them.

The identifier is sorted by the foreign key again when the entity before
the association holds it. An association without such a foreign key, a
hasOne or the inverse side of a one-to-one, is still joined, and so is
the first association of a path, as it always has been.

The spec also covers the join type of a sort join: an inner join by
default, and a left join when fetch requests one. Team.club is nullable
in the fixture for these cases. The docs describe the inner join, how to
request a left join, and the identifier of a nested association.
list with a map of sort properties, such as
Team.list(sort: ['club.name': 'asc', name: 'desc']), sorted by the last
entry only, because each entry replaced the orders of the query. The
same held for a default sort that the mapping declares for more than
one property. Every entry is now applied, in the order of the map.

An association is joined once for all the sort properties that use it.
list joined it once for each, and find and findAll with an example,
findWhere and findAllWhere failed with "duplicate association path".

Part 2 of the split of apache#16853.
A paged query of a detached criteria that has an alias for the queried
class, and criteria that use it, failed when totalCount was read:

    new DetachedCriteria(Team, 't').eq('t.name', 'Arsenal').list(max: 2).totalCount

The count runs on a copy of the criteria, and the copy was created
without the alias, so Hibernate could not resolve t.name. The copy now
has the alias of the original.

Part 3 of the split of apache#16853.
count on a where query or a detached criteria that has a sort, such as
Team.where { ... }.sort('name').count(), kept the order by in the query
that counts the rows, which H2 rejects with "must be in the GROUP BY
list". A query that only counts its rows now has no orders.

The join that a sort by a property of an association makes is kept, so
the count is that of the rows which list returns for the same query.

Part 4 of the split of apache#16853.
… by it

A sort by a property of an association joins the association with an
inner join, so it leaves out the instances whose association is null:
Team.list(sort: 'club.name') does not return the teams without a club,
and count and the totalCount of a paged list do not count them.

The new grails.gorm.leftJoinAssociationSort setting makes an association
that is joined only for a sort a left join, in where queries, detached
criteria, dynamic finders, list, find and findAll with an example,
findWhere, findAllWhere and a criteria query that is passed pagination
arguments. It is disabled by default, so the results of existing
applications do not change in a patch release. Grails 8.1 enables it by
default.

With the setting, a sort by a nested identifier, such as team.club.id,
goes through the left join. Without it, it is still read from the
foreign key.

A sort by a property of a collection keeps its inner join: Hibernate
fetches a collection through a left join, which is handled separately.

Part 5 of the split of apache#16853.
Team.where { ... }.join('club', JoinType.INNER) lists and counts the
teams with a club, but deleteAll and updateAll on it ignored the join
type and deleted or updated the teams without a club as well. They now
modify the instances that list returns.

An update or a delete cannot join, so each requested join becomes a
restriction: a subquery that selects from the entity of the first
association on the path and joins the rest of it. deleteAll selects the
identifiers first when the path starts with a collection that has a
join table, because Hibernate deletes the join rows before the
instances with the same restriction. A one-to-one whose foreign key the
other class has is matched through the property that refers back.

MySQL, and MariaDB before 10.3, do not allow an update or a delete to
select from the table that it modifies. For a join that leads back to
the queried class, these dialects get an UnsupportedOperationException
that says to restrict the query with criteria instead.

Part 8 of the split of apache#16853.
The criteria builder accepted a join type and never used it.
join('club', JoinType.INNER) has had the body of the one-argument join
since it was added, so it set a fetch mode, and a JoinType in the fetch
argument of list matched no fetch mode, so the association was not
fetched at all.

Both now join and fetch the association with the requested type, with
the rules of a where query: a path through a collection restricts the
query with a subquery, and the associations on the path are fetched.
The join types are applied once the criteria are built, so an
association that the criteria have a block for, or join through an
alias, keeps that join. A fetch mode requested afterwards, with join,
select, fetchMode or the fetch argument, replaces the join type.

The sort argument of the list of a criteria query uses a join that the
criteria already have for the association, such as the one of a join
type that the fetch argument requests for it. It joined the association
a second time, which failed with "duplicate association path".

Part 9 of the split of apache#16853.
order('club.name') in a criteria query failed with "could not resolve
property", and with a Query.Order it sorted by a property of the
queried class. A criteria query that is passed pagination arguments
and a sort by a path through more than one association failed when
totalCount was read, or with "duplicate association path" when the
criteria used the first association, and a map of sort properties
failed with a ClassCastException.

order now resolves an association path, also inside the criteria of an
association, and joins with a left join an association that the
criteria do not join. The orders are added once the criteria are
built, so an order uses the join and the join type that the criteria
or the fetch argument request, whichever comes first. An order by the
identifier of an association reads the foreign key, as before. The
sort argument reuses a join that the criteria have for a path, also
one made through an alias, and creates any other from the root
criteria, which is how the count recreates it.

The sort argument can also start with an alias that the criteria give
an association, as sort: 'c.name' with createAlias('club', 'c'), also
as an entry of a map of sort properties and followed by a further
association. Such a sort was dropped, which review 5868034911 of
apache#16853 reported for the map.

The join that an order makes is a left join only with the
grails.gorm.leftJoinAssociationSort setting. Without it the association
is joined with an inner join, as it is for the other sorts.

Part 10 of the split of apache#16853.
A where query, a detached criteria and a dynamic finder joined an
association as soon as criteria, a projection or a sort needed it. The
rest of the query can still change after that: criteria passed to list
are added later, and the fetch argument of list, which can request a
join type, is read last. The result depended on the order in which the
query was put together:

- a join type in the fetch argument of list was not used for the join
  that the criteria or a projection had already made;
- a sort stored on the query with sort() was joined before a join type
  requested in list was known, and criteria passed to list could not
  join the same association through an alias;
- a join type was looked up by the name of the association, so a type
  requested for club was used for team.club, and one requested for
  team.club was not;
- criteria on a nested association used twice failed with
  "duplicate association path".

Criteria, projections and orders only need the alias of a join. A join
is now registered under its path from the root entity and its alias,
and made when the query is executed, or when a subquery is handed to
the query that embeds it, with the join type that the query requests
for that path. A listener of the query events that adds criteria on an
association to find and findAll with an example, findWhere or
findAllWhere gets its joins made after the event.

Part 6 of the split of apache#16853.
A where query, a detached criteria or a dynamic finder can request a
join type for an association, with fetch: [club: JoinType.INNER] or
join('club', JoinType.INNER). The type was only used when criteria or
a sort joined the association. Otherwise the association was fetched
with the outer join of its fetch mode, so
Team.where { ... }.list(fetch: [club: JoinType.INNER]) returned the
teams without a club, and the same query sorted by club.name did not.

A requested join type other than LEFT is now applied when the query is
executed, to list, get, find, count, the totalCount of a paged list, a
subquery and a query with a projection:

- INNER and RIGHT on an association to a single instance join and
  fetch it; a right join is a right join, not an outer fetch;
- a path that ends in a collection or passes through one restricts the
  query with a correlated subquery, so that the collection stays
  fetched with all of its elements, each instance is counted once, and
  the filters of the session, such as the tenant filter, apply;
- every association on a requested path is joined and fetched;
- a collection of a basic type is handled like any other collection;
- a fetch mode requested afterwards replaces the join type, on the
  query and on the detached criteria, while the join that criteria on
  the association make keeps it.

Part 7 of the split of apache#16853.
list, findWhere, findAllWhere, and find and findAll with an example
ignored a JoinType in their fetch argument: they returned the instances
without the association, and list did not fetch it. They now apply it,
before a sort by the association, which uses that join. list fetches
with the requested type, returns each instance once when it fetches a
collection, and applies RIGHT like INNER, since Hibernate does not
support right joins in the criteria queries that list runs.

'join' and 'eager' in the fetch argument of list fetch the association
with a left join and return every instance, as a where query and a
dynamic finder do. list joined it with an inner join without fetching
it, so it left out the instances whose association is null or whose
collection is empty.

The totalCount of a paged list counts each instance once when the list
fetches a collection, and keeps a sort join that hangs under a fetch.

Part 11 of the split of apache#16853.
A sort that starts with the alias that a detached criteria gives the
queried class, as in
new DetachedCriteria(Team, 't').list(sort: 't.name'), was ignored: the
query returned its rows unsorted, without an error. Such a sort now
sorts by the rest of its path, in where queries, detached criteria,
list, the example queries and the sort argument of a criteria query.

Part 13 of the split of apache#16853.
A sort by a property of a collection joined the collection with an
inner join. It left out the instances whose collection is empty, and
took the place of a fetch that the query requested for the collection.
Criteria on a collection with a requested left join initialized the
collection with only the elements that the criteria matched.

- A sort joins a collection with a left join and does not fetch it
  through that join, unless the query asks for the collection to be
  fetched, with or without the sort.
- A left join of a collection that criteria use does not fetch the
  collection. When the query also fetches another collection, that one
  is loaded once the query has run, because Hibernate would read it
  from the columns of the join of the criteria.
- A sort by a collection next to a fetch of another collection repeats
  rows, and a bag cannot tell a repeated row from a repeated element,
  which is why Hibernate does not fetch two bags in one query. When
  either collection is a bag, the query throws an
  UnsupportedOperationException that names both, where it returned the
  bag with repeated elements. A query that projects values loads no
  collection and is not affected.

A fetch that a mapping declares counts as fetched only where Hibernate
joins it: not at or beyond hibernate.max_fetch_depth joins from the
queried class, where Hibernate loads it with a query of its own
(review 5868034911 of apache#16853).

This applies with the grails.gorm.leftJoinAssociationSort setting.
Without it a sort still joins a collection with an inner join, which
leaves out the instances whose collection is empty.

Part 12 of the split of apache#16853.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requested changes. A sort by the queried class's own alias throws when that alias is also the name of an association.

if (!createdAssociationPaths.containsKey(sortHead)) {
PersistentProperty persistentProperty = entity.getPropertyByName(sortHead);
if (persistentProperty == null) {
String rootAlias = criteria != null ? criteria.getAlias() : detachedCriteria.getAlias();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The query aliases the root as author and sorts by author.author.name. That should sort by the author's name. It throws, because author is treated as the association before it is treated as the root alias. Check the root alias first.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 35242f9. The root alias is recognized before looking up a property of the same name, and is stripped once only. The remaining path is then resolved as a property path, so club.club.name means the club association of the root named club rather than stripping club twice.

The new cases cover both an association name and a scalar property name used as the root alias, through the sort argument and a stored sort. All 13 cases of UndefinedAliasSortSpec pass, as does the module codeStyle check.

@matrei matrei left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed at 16bd465 (base 7.0.x): e359719, 35242f9 and the merge of #17072's head, which merges without a conflict. I probed sorts that start with the alias of the queried class, in the sort argument of a detached criteria, in sort(), as a map, paged, with count, by a property of the class, of an association and of a nested association, and with an alias that is also the name of an association:

A sort by a property of the class sorts now, and keeps the rows:

7.0.x and #17072 This head
new DetachedCriteria(Team, 't').list(sort: 't.name', order: 'desc'), also sort('t.name', 'desc'), a map, max: 2 with totalCount, Team.where { … }.list(sort: 'this.name'), findAllByNameLike(…, [sort: 'this.name']), findAll with an example, and the sort argument of a criteria query unsorted sorted, with the same rows

Needs a fix

1. The rows change for a sort through an association, with the setting off. The description says "The rows are the same". A sort that starts with the alias and goes on to an association was ignored. It now joins the association with an inner join, as the same sort without the alias does:

Setting off 7.0.x and #17072 This head Setting on
new DetachedCriteria(Team, 't').list(sort: 't.club.name'), also 't.club.id' 7 teams, unsorted 5 7
the same with max: 2: totalCount 7 5 7
new DetachedCriteria(Team, 't').sort('t.club.name').count() 7 5 7
new DetachedCriteria(Player, 'p').list(sort: 'p.team.club.name') 7 players 6 7

This is the same as point 1 on #17070. Without the alias, new DetachedCriteria(Team).list(sort: 'club.id') returns the 5 teams on 7.0.x as well, so this needs no setting. But "Results that change", the note and the skill entry should say that such a sort leaves out the instances without the association unless the setting is enabled. The guide's new example, new DetachedCriteria(Pet, 'p').list(sort: 'p.owner.lastName'), is such a sort.

2. An alias that is also the name of an association now sorts by another property. 35242f9 resolves the root alias before an association of the same name, for James's comment. That changes a sort that worked on 7.0.x:

new DetachedCriteria(Team, 'club') 7.0.x and #17072 This head Hibernate 7 on 8.0.x and #17079
list(sort: 'club.name'), also sort('club.name').list() 5 teams, by the name of the club 7, by the name of the team 5, by the name of the club

On 7.0.x and with Hibernate 7, the association wins. Resolving the path from the association first, and stripping the alias only when the rest of the path doesn't resolve from there, would keep this sort as it was. James's author.author.name would still sort, since author of the author doesn't resolve. Otherwise "Results that change" and the note have to name this change.

3. No Hibernate 7 counterpart. With Hibernate 7, every sort of the first table fails: on 8.0.x mostly with a NullPointerException, and at #17079 with IllegalArgumentException: Invalid sort property. After the merge-up, Hibernate 5 sorts and Hibernate 7 throws. I noted that on #17063 already. Hibernate 7 needs this before it merges, as for the other parts, in #17079 or in a pull request of its own.

4. Add run ci, and resolve the conflicts with 7.0.x, as on the other parts. Locally, the tests of grails-data-hibernate5-core pass at this head (1636 tests, 159 skipped), which matches the 1,636 cases that jdaugherty reported for the whole stack on #17071.

Notes

  1. new DetachedCriteria(Team, 't').list(sort: 't.unknown') was ignored, and now fails with could not resolve property: unknown, as sort: 'unknown' does on 7.0.x. That's consistent, but it's a call that worked and now fails, so "Results that change" should name it. A sort by an alias that the query doesn't define, such as x.name, is still ignored.

  2. On James's comment, new DetachedCriteria(Team, 'club').list(sort: 'club.club.name') sorts by the name of the club here. At #17072 it failed with could not resolve property: club_alias0, and on 7.0.x it sorted by the name of the team.

  3. The description names e359719 as the change to review. 35242f9 is on top of it, without a message body.

  4. This is the last part of the stack on 7.0.x, so the module tests here run the whole stack.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants