Skip to content

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

Merged
jdaugherty merged 25 commits into
apache:7.0.xfrom
jdaugherty:fix/16853-13-sort-root-alias
Dec 3, 2026
Merged

jdaugherty merged 25 commits into
apache:7.0.xfrom
jdaugherty:fix/16853-13-sort-root-alias

Conversation

@jdaugherty

@jdaugherty jdaugherty commented Nov 19, 2026 •

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, which is on 7.0.x since 892840b. This pull request now targets 7.0.x.

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

  • Root-alias-prefixed sorts now apply their remaining path. For association paths, the new inner join can exclude roots without the association from list, count and totalCount when the compatibility setting is off. Enable leftJoinAssociationSort or request LEFT to retain them while sorting; remove the sort for the former unsorted rows.
  • An invalid remaining property now fails as the same unqualified invalid property does. Undefined aliases remain outside this change and are addressed on 9.0.x by Reject a sort by an alias that the query does not define #17074.
  • A join type requested through the root alias, with join, select or the fetch argument, as in new DetachedCriteria(Team, 't').join('t.club', JoinType.INNER), or join('club.club', JoinType.INNER) for the alias club, was ignored. It is now applied as for the path without the alias: list, count, the totalCount of a page, updateAll and deleteAll leave out the teams without a club, and JoinType.RIGHT through the alias gives what it gives without the alias. Remove the request to keep the former rows. Request each association once, with or without the alias.
  • this, the alias that Hibernate gives the queried class of a query that declares none, is stripped as a root alias in every API, the static list included: Team.list(sort: 'this.name', order: 'desc') sorts by name, sort: 'this.club.name' leaves out the teams without a club with the setting off, also from totalCount, and join('this.club', JoinType.INNER) in a where query or a detached criteria is applied.
  • A valid association path keeps its old meaning when its first segment is also the root alias. The root alias is stripped once only when the full association path does not resolve, preserving the doubled-alias fix without changing a working association sort. Nested joins are qualified through their parent alias, including paths through collections and multiple associations.

The Hibernate 7 root-alias sort and detached-criteria restriction counterpart is tracked by #17167, including the finding on #17063. It must be coordinated on 8.0.x before release across both modules, separately from requested-join handling in #17079. The Grails 9 merge-up must preserve declared root aliases alongside the undefined-alias rejection in #17074.

The pages of a static list whose collection fetch is deferred belong to #17072, whose totalCount now counts the rows that such a list pages through; pages of distinct instances are tracked by #17185.

Verification

At 883db76, after merging #17072's head, UndefinedAliasSortSpec, RootAliasSortLegacySpec, RequestedJoinBulkOperationDialectSpec and the association sort, join type, fetch and pagination specs pass in both setting modes (2,498 cases, zero failures, 140 skipped), and codeStyle of the Hibernate 5 core module passes. At 33970a6, before the merge and this round, the full Hibernate 5 core suite reported 2,968 passed, 168 skipped and zero failures, with root codeStyle, RAT, dependency-version validation and the guide build.

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.

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.

@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.

Allow an unfetched sorted bag beside a fetched set of another element type. Keep same-element legacy fetch handling, and protect a static-list fetched bag with either compatibility setting. Make utility-only fetch checks private and document secondary collection loading costs.
Resolve an existing association path before stripping a colliding root alias, while retaining the doubled-alias fix. Exercise both compatibility modes and document missing-association rows, counts and invalid remaining properties.

@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 that starts with the alias of the queried class still throws.

@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 again at eba8ef9. Points 1, 2 and 4 of my first review are fixed, and point 3 is below:

  1. "Results that change", the note and the skill entry say that a sort that starts with the alias and goes on to an association leaves out the instances without it unless the setting is enabled, and that its count and totalCount do too. They also name the invalid property after the alias, my note 5.
  2. An alias that is also the name of an association sorts by the association again. new DetachedCriteria(Team, 'club').list(sort: 'club.name') returns the 5 teams by the name of their club, as on 7.0.x and with Hibernate 7. James's club.club.name still sorts.
  3. The pull request merges cleanly with 7.0.x and has run ci. The MongoDB functional tests failed in OpenSessionInViewSpec, which waited 10 seconds for the session of an asynchronous request. That spec came with the session fix on 7.0.x (d8c2068), tests no Hibernate code, and passed on every other part of the stack, so a rerun of that job should pass.

Apart from these, my probes from the first review give the same results here as at 16bd465, with the setting off and on. The tests of grails-data-hibernate5-core pass here (1720 tests, 168 skipped), and the guide builds.

Needs a fix

1. James's new comment holds: a path that resolves as an association still loses its first segment when it goes through a collection or through more than one association. The description says that the root alias is stripped only "when the full association path does not resolve". That holds for club.name, but not for these:

7.0.x and this head Hibernate 7, on 8.0.x and at #17079
new DetachedCriteria(Team, 'players').list(sort: 'players.team.name'), also sort('players.team.name') and max: 3 could not resolve property: team of: RjTeam sorts by the team of each player
new DetachedCriteria(Player, 'team').list(sort: 'team.club.name') could not resolve property: club of: RjPlayer sorts by the club of each player's team

Both calls fail on 7.0.x too, so making them work needs no note. But they fail because the alias is stripped from a path that resolves from the queried class. Resolving the whole path, through collections and nested associations, before stripping the alias would make them sort, as Hibernate 7 does.

2. Nothing tracks the Hibernate 7 counterpart. The description says that it's a follow-up to coordinate before the release, but there's no issue or pull request for it. With Hibernate 7, every sort of the first table of my first review still fails: on 8.0.x mostly with a NullPointerException, and at #17079 (1cba001) with IllegalArgumentException: Invalid sort property. Criteria through the root alias of a detached criteria fail with Hibernate 7 as well, as I noted on #17063. An issue for both, named in the description, would keep them from being missed when the stack is merged up.

Smaller points

  • The description still names e359719 as the change to review. 35242f9 and eba8ef9 are part of it too.
  • The imports of AssociationJoinTypeSpec and WhereQueryWithAssociationSortSpec are reordered again here.

As a fix that changes a result, this needs a second approval under #17061.

Merge the reviewed parent fixes and defer collection fetches that would multiply bag elements. Keep requested left joins, complete fetched collections, sort-map joins and distinct static-list results in both compatibility modes.
Merge the reviewed collection-fetch fixes and qualify nested joins through their parent alias. Preserve valid association paths that share the root alias name, with paged, stored and map sorts covered in both compatibility modes.

@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 a nested path still drops a team with no players when the left join is named on that path.

if (!joined && !leftJoin) {
// without the leftJoinAssociationSort setting, an association is joined for a sort as it is
// for criteria, with an inner join
join.usedByCriteria = true;

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.

A criteria for Team whose alias is players, sorted by players.team.name, and fetching players.team as a left join, should include a team with no players. It is left out. The sort joins players as an inner join. Keep the left join.

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.

Covered at 060fe905f225, with the lower requested-join fix from 4301e589351c carried through the parent. The exact players.team LEFT-fetch case now has 24 passing variants: colliding root alias and no-alias control, argument/stored/map sorts, paged/unpaged and both settings. The 12 setting-off variants failed before the corrected lower parent was included; no additional alias-runtime fix was needed.

Merge the corrected requested-join and example-query parent while retaining deferred bag loading and opt-in collection sorts. Keep explicit intermediate joins and pagination safeguards, and align inherited replacement-fetch controls with both compatibility modes.
Merge the corrected collection-sort parent while keeping parent-alias join qualification. Cover the nested LEFT collection path with a colliding root alias and a no-alias control, using argument, stored and map sorts with and without pagination in both compatibility modes.

@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 left join named with an alias is missed when that alias is also a property name.

* @return The path from the queried class
*/
public static String stripRootAlias(PersistentEntity entity, String rootAlias, String path) {
if (path == null || rootAlias == null || !path.startsWith(rootAlias + '.') || isPropertyPath(entity, path)) {

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.

A criteria for Book whose alias is author, sorted by author.publisher.name, and joining author.publisher as a left join, should include a book with no publisher. It is left out. The fetch path is not seen as a join. Keep the left join.

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.

This does not reproduce with a left join type. At 883db76, for a book whose author has no publisher, new DetachedCriteria(Book, 'author').join('author.publisher', JoinType.LEFT).list(sort: 'author.publisher.name') returns that book, first, with grails.gorm.leftJoinAssociationSort off and on. So do a stored sort('author.publisher.name'), a paged list (totalCount 3), count(), get(), a copy made by build {} or where {}, fetch: ['author.publisher': JoinType.LEFT] in place of join, and the paths through the alias, author.author.publisher and author.author.publisher.name. This line leaves author.publisher as it is because the path resolves from the book, so the join and the sort name the same association and the sort uses the left join. On 7.0.x each of these queries throws could not resolve property: publisher of: Book, or author_alias0 for the paths through the alias. c5ae0d7 adds the case to UndefinedAliasSortSpec and RootAliasSortLegacySpec: 'a LEFT #request of #joinPath keeps a book whose author has no publisher with root alias author and sort #sortPath using #sortForm and pagination #paged', with join and the fetch argument, both paths, a sort argument and a stored sort, paged and not.

The book is left out only when no join type is requested, with join('author.publisher') or fetch: ['author.publisher': 'join'], and the setting off. It is left out without the alias as well: new DetachedCriteria(Book).join('author.publisher').list(sort: 'author.publisher.name') returns the two books with a publisher, on 7.0.x as here. That is the 7.0.18 note "A sort by an association uses the left join that a query requests for it": a sort by an association for which the query requests no join type, or the fetch mode join, still joins it with an inner join. Request JoinType.LEFT to keep the book.

A detached criteria for books with the alias author, sorted by
author.publisher.name, keeps a book whose author has no publisher when
it requests a left join of author.publisher, with join or the fetch
argument. The path resolves from the book, so the alias is stripped
from neither the join nor the sort, and the sort uses the requested
left join. The same holds for author.author.publisher and
author.author.publisher.name, which name the same association through
the alias, for a stored sort and for a paged list, with
grails.gorm.leftJoinAssociationSort enabled or not. On 7.0.x these
queries throw.

@jdaugherty jdaugherty left a comment

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.

@jamesfredley, review 6094573523 is answered in its thread; c5ae0d7 adds the case to the specs.

* @return The path from the queried class
*/
public static String stripRootAlias(PersistentEntity entity, String rootAlias, String path) {
if (path == null || rootAlias == null || !path.startsWith(rootAlias + '.') || isPropertyPath(entity, path)) {

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.

This does not reproduce with a left join type. At 883db76, for a book whose author has no publisher, new DetachedCriteria(Book, 'author').join('author.publisher', JoinType.LEFT).list(sort: 'author.publisher.name') returns that book, first, with grails.gorm.leftJoinAssociationSort off and on. So do a stored sort('author.publisher.name'), a paged list (totalCount 3), count(), get(), a copy made by build {} or where {}, fetch: ['author.publisher': JoinType.LEFT] in place of join, and the paths through the alias, author.author.publisher and author.author.publisher.name. This line leaves author.publisher as it is because the path resolves from the book, so the join and the sort name the same association and the sort uses the left join. On 7.0.x each of these queries throws could not resolve property: publisher of: Book, or author_alias0 for the paths through the alias. c5ae0d7 adds the case to UndefinedAliasSortSpec and RootAliasSortLegacySpec: 'a LEFT #request of #joinPath keeps a book whose author has no publisher with root alias author and sort #sortPath using #sortForm and pagination #paged', with join and the fetch argument, both paths, a sort argument and a stored sort, paged and not.

The book is left out only when no join type is requested, with join('author.publisher') or fetch: ['author.publisher': 'join'], and the setting off. It is left out without the alias as well: new DetachedCriteria(Book).join('author.publisher').list(sort: 'author.publisher.name') returns the two books with a publisher, on 7.0.x as here. That is the 7.0.18 note "A sort by an association uses the left join that a query requests for it": a sort by an association for which the query requests no join type, or the fetch mode join, still joins it with an inner join. Request JoinType.LEFT to keep the book.

*/
private String normalizeFetchPath(String property) {
String rootAlias = criteria != null ? criteria.getAlias() : detachedCriteria != null ? detachedCriteria.getAlias() : null;
if (property != null && rootAlias != null && property.startsWith(rootAlias + '.') && !isAssociationSortPath(property)) {

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.

The same claim is answered with measurements in PRRT_kwDOAAfRJ868-CfM, on line 848 of GrailsHibernateUtil: with JoinType.LEFT the book is kept, with and without the alias, and the fetch mode join leaves it out without the alias too, as on 7.0.x.

@testlens-app

testlens-app Bot commented Nov 28, 2026

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: c5ae0d7
▶️ Tests: 62986 executed
⚪️ Checks: 58/58 completed


Learn more about TestLens at testlens.app/docs.

@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 page that fetches two collections can load one of them twice.

return true;
}
}
for (Fetch<?, ?> fetch : parent.getFetches()) {

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.

A list fetches two collections and sorts one of them. One collection has two rows. It is loaded with four. Count the fetched collections when deciding which one to load later.

@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.

Fifth round, at c5ae0d7. It merges #17072's head 02b0749, and it fixes the three points of my fourth review. c5ae0d7 adds the case of James's first comment of 11-28 to UndefinedAliasSortSpec. The grails-data-hibernate5-core tests pass (3241 tests, 163 skipped), the guide builds, the branch merges cleanly with 7.0.x at ffd848b, and CI passed.

Fixed

  1. updateAll and deleteAll read the join types by the same paths as list now.
    • new DetachedCriteria(Team, 't').join('t.club', JoinType.INNER).updateAll(name: 'X') updates the 5 teams that list and count() give. At 33970a6 it updated 7.
    • With build { isEmpty('players') }, deleteAll() deletes the 1 team that list() returns. At 33970a6 it deleted 2.
    • The update gives the same with the setting on.
  2. "Results that change", the note and the skill describe the join types requested through the root alias and through this. That includes count, the totalCount of a page, the bulk writes and JoinType.RIGHT.
  3. The static list strips this too. Team.list(sort: 'this.name', order: 'desc') sorts by name, as the other APIs do.

My probes of the earlier rounds give the same results as at 33970a6, apart from these rows. The other differences are the totals that #17072 counts now and the order of teams with equal sort values.

James's reviews of 11-28

The alias that is also the name of a property (00:21): this doesn't reproduce with a left join type, as jdaugherty replied. With books by an author with a publisher, by an author without one and by an author with another publisher:

new DetachedCriteria(Book, 'author')... 7.0.x (ffd848b) This head
.join('author.publisher', JoinType.LEFT).list(sort: 'author.publisher.name'), also with max: 10, with a stored sort, with fetch: ['author.publisher': JoinType.LEFT] in place of join, and through the alias as author.author.publisher, with the setting off and on throws could not resolve property: publisher, or author_alias0 through the alias the 3 books, the one without a publisher first
.join('author.publisher').list(sort: 'author.publisher.name'), setting off throws the 2 books with a publisher, as new DetachedCriteria(Book) gives on 7.0.x and here

A page that fetches two collections (06:52): this holds, but not for this part. When the static list fetches the bag members and the set trophies and sorts by one of them, the bag repeats its elements. Team.list(sort: 'trophies.title', fetch: [members: 'join', trophies: 'join']) returns Full with the members a, a, b and b.

  • #17072 (02b0749) gives the same, and so does #17071 (3128803), also without the sort.
  • The where query and the criteria query with the sort load a and b since #17072.
  • The where query without the sort repeats the elements on 7.0.17 too.

The code is #17072's, so I'll raise it there.

Smaller point

#17167 still doesn't name the join types requested through the root alias. With Hibernate 7 at #17079 (08325a6):

  • new DetachedCriteria(Team, 't').join('t.club', JoinType.INNER) returns all 7 teams from list, count(), the totalCount of a page and updateAll. So do join('club.club', JoinType.INNER) with the alias club, fetch: ['t.club': JoinType.INNER], and join('this.club', JoinType.INNER) in a where query.
  • join('t.club', JoinType.RIGHT) returns the 7 teams. Without the alias, it returns the 5 teams with a club and a null.

So that's one more place where this part makes the modules differ, and it belongs in #17167 with the sorts.

What I ran

  • My probes of the first four rounds: at this head and on 7.0.x (ffd848b).
  • The rows of James's reviews:
  • The root-alias join rows with Hibernate 7: at #17079 (08325a6).
  • The grails-data-hibernate5-core tests and the guide: at this head.

As a fix that changes a result, this needs a second approval under #17061.

@jdaugherty
jdaugherty changed the base branch from 7.0.x to fix/16853-12-collection-sorts-and-joins November 30, 2026 15:49

@sbglasius sbglasius 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.

This is an AI assisted review

LGTM. Root-alias stripping is applied consistently to sort, join/select/fetch and bulk update/delete, and a valid association path still wins over a same-named alias. Docs and upgrade notes cover the result changes.

One open item: James's thread on GrailsHibernateUtil.java:500 (two collections fetched, one loaded twice) has no reply yet. It looks like #17072's deferred-fetch code, so please answer or move it there before merging.

@jdaugherty
jdaugherty changed the base branch from fix/16853-12-collection-sorts-and-joins to 7.0.x December 3, 2026 12:05
@jdaugherty
jdaugherty merged commit 0b73627 into apache:7.0.x Dec 3, 2026
59 checks passed
@jdaugherty
jdaugherty deleted the fix/16853-13-sort-root-alias branch December 3, 2026 12:07
jdaugherty added a commit that referenced this pull request Dec 3, 2026
…etches

#17073 strips `this` from the sort of a static list through a recursive
call of addOrderPossiblyNested for the JPA criteria query, which #17072
had given the path of the association and the join types of the deferred
fetches. The two merged without a conflict, and grails-data-hibernate5-core
did not compile. The call passes both now.
jdaugherty added a commit that referenced this pull request Dec 3, 2026
Carries #17255 (a JPA entity with several @id properties compiles), #17265
(the OAuth2 plugin accepts an intranet authorization url and redirects once
on an invalid one), #17072 (a sort by a collection joins it without changing
what the query fetches) and #17073 (a sort can start with the alias of the
queried class), with 7fa74e0, which makes grails-data-hibernate5-core
compile again after #17072 and #17073 merged.

- upgrading60x.adoc keeps the 7.1.x side; the changes to the 7.0.18 notes
  are applied to upgrading70x.adoc, where 7.1.x keeps them.
jdaugherty added a commit that referenced this pull request Dec 3, 2026
Carries #17255 (a JPA entity with several @id properties compiles), #17265
(the OAuth2 plugin accepts an intranet authorization url and redirects once
on an invalid one), #17072 (a sort by a collection joins it without changing
what the query fetches) and #17073 (a sort can start with the alias of the
queried class), from 7.0.x, with 7fa74e0, which makes
grails-data-hibernate5-core compile again after #17072 and #17073 merged.
jdaugherty added a commit that referenced this pull request Dec 3, 2026
Carries #17255 (a JPA entity with several @id properties compiles), #17265
(the OAuth2 plugin accepts an intranet authorization url and redirects once
on an invalid one), #17072 (a sort by a collection joins it without changing
what the query fetches) and #17073 (a sort can start with the alias of the
queried class), from 7.0.x with 7fa74e0, and #17036 (runtime.groovy is
only read when application.groovy exists), from 7.2.x.

- GrailsHibernateQueryUtils keeps 7.x's populateArgumentsForListQuery and
  addSortArguments, which defer the fetches a collection sort would
  multiply, with 8.0.x's validation: an invalid direction is rejected before
  the sort is read, and every sort property is validated.
- HibernateSession imports HashMap and LinkedHashMap.
- JpaEntityTransformSpec keeps 8.0.x's features and adds 7.x's.
- UndefinedAliasSortSpec and CollectionFetchPaginationSpec register their
  domain classes with registerDomainClasses, as the other specs on 8.0.x do.
- The changes to the 7.0.18 notes are also in the 8.0.1 section of
  upgrading80x.adoc and in the 8.0.1 part of the upgrade skill, under
  "GORM for Hibernate 5".

Ten rows of UndefinedAliasSortSpec fail on 8.0.x until #17308, in that spec
and in RootAliasSortLegacySpec, which runs it with the setting disabled: 20
failures. They sort through a root alias that has the name of a property,
as club.club.name for new DetachedCriteria(Team, 'club') and
author.author.publisher.name for the alias author, with the sort argument
or a sort map. The validation of the sort argument that 8.0.x adds does not
know the alias of the criteria and rejects such a key with "Invalid sort
property". #17308 makes the validation follow the alias.
jdaugherty added a commit that referenced this pull request Dec 3, 2026
Carries, from 7.x: #17255, #17265, #17072, #17073 and #17036, with
7fa74e0, which makes grails-data-hibernate5-core compile after #17072
and #17073; and from 8.0.x: #17295 (nimbus-jose-jwt 10.11), #17076 (with
Hibernate 7, deleteAll and updateAll reject max and offset), #16725 and
#17296 (the mongodb-developer skill and the scoped agent guides), #17024
(Spock conditions in if blocks and loops are asserted), #16852 and #17029
(setup-gradle 6.4.1 and setup-java 6.3.0), and #17308 (the check of the
sort argument reads a key through the alias of a detached criteria that
has the name of a property).

- AGENTS.md keeps 8.1.x's description of grails-forge and takes 8.0.x's
  links to the agent guides of build-logic and grails-gradle.
jdaugherty added a commit that referenced this pull request Dec 3, 2026
Carries #17255, #17265, #17072, #17073 and #17036 from 7.x, with
7fa74e0, and #17295, #17076, #16725, #17296, #17024, #16852, #17029
and #17308 from 8.0.x.

- The sort of Hibernate 5 is the 7.0.x code with the rejection of a sort by
  an alias that the query does not define, as the merge-up section of
  #17074 asks, and keeps the declared root alias and this, as #17073 asks
  of the merge into 9.0.x. AbstractHibernateQuery strips the root alias
  once, as on 7.0.x, then checks the rest from the queried class or from a
  declared alias and throws "Invalid sort property" for anything else.
  Both addOrderPossiblyNested methods of GrailsHibernateQueryUtils handle
  the root alias and this first and then reject a name that is not a
  property. GrailsHibernateUtil keeps 9.0.x's rejection.
- UndefinedAliasSortSpec has 9.0.x's features for an undefined alias and
  7.x's for the root alias, so a sort through an alias with the name of a
  property sorts, with #17308's check, and an undefined alias is rejected.
- dependencies.gradle keeps 9.0.x's pins, with nimbus-jose-jwt 10.11.
- The upgrade skill keeps 9.0.x's side, the Grails 9 skill, which has no
  8.0.1 part.
- The 9.0.x job of benchmark.yml takes setup-java 6.3.0 and setup-gradle
  6.4.1, as the other jobs do.
- The count features of #17308's SortArgumentValidationSpecs follow
  9.0.x, where a count checks its sort as list does (#17074). With
  Hibernate 5, count(sort: 't.club.unknown') for the alias t is rejected,
  as club.unknown is; t.unknown still gives the count. With Hibernate 7,
  which does not sort through the alias of the queried class (#17167), a
  count rejects such a key, as list and get do.
jdaugherty added a commit to jdaugherty/grails-core that referenced this pull request Dec 3, 2026
Brings the merge of 7.2.x with the Hibernate 5 sort and join type changes of apache#17072 and apache#17073, apache#17308, which reads a sort key through the alias of a detached criteria and fixes the Hibernate 5 specs that the sort check rejected, and the test of nested association blocks of apache#16559. This branch changes nothing that they change, apart from the upgrade note and the skill, which merged without a conflict and keep the entries of both sides in their order. apache#17308 leaves a static list with a join type in its fetch argument as it was: it has no alias, and calls the overload without one.
jdaugherty added a commit that referenced this pull request Dec 3, 2026
Carries #17255, #17265, #17072, #17073 and #17036 from 7.x, with
7fa74e0, #17295, #17076, #16725, #17296, #17024, #16852, #17029 and
#17308 from 8.0.x, and 9.0.x's merge of them, which keeps the rejection
of a sort by an alias that the query does not define (#17074) beside the
sort through the root alias of 7.0.x.
jdaugherty added a commit that referenced this pull request Dec 3, 2026
Carries #17255, #17265, #17072, #17073 and #17036 from 7.x, with
7fa74e0, #17295, #17076, #16725, #17296, #17024, #16852, #17029 and
#17308 from 8.0.x, through 8.1.x, 9.0.x and 9.1.x.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants