Skip to content

Sort where queries by an association the query filters on - #16853

Open
matrei wants to merge 6 commits into
7.0.xfrom
fix/15589-where-query-association-sort-7.0.x
Open

matrei wants to merge 6 commits into
7.0.xfrom
fix/15589-where-query-association-sort-7.0.x

Conversation

@matrei

@matrei matrei commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Description

Fixes #15589. This is part 1 of the split of this pull request, as agreed here and in the dev@ thread Fixes that change results on patch and minor branches. Parts 2 to 13 are #17062 to #17073, stacked on this one, part 14 is #17074 on 9.0.x, and #17079 gives Hibernate 7 the rule of the requested join types. The previous head, 7323d92, is kept at preserve/16853-full.

Sorting a where query by a property of an association failed with a QueryException when the query also had criteria on that association:

Team.where {
    club.name == 'Manchester United'
}.list(sort: 'club.name')
// org.hibernate.QueryException: could not resolve property: club.name of: grails.gorm.tests.Team

The criteria join club under a generated alias, such as club_alias0, and AbstractHibernateQuery records the join under both the association path and the alias. order() found the join, but added the order with the association path, which Hibernate cannot resolve against the root entity. The sort only worked with an explicit alias defined in the where query, because then the sort property already started with the alias. The order is now added with the alias of the join, as club_alias0.name.

A sort by a path through more than one association, such as team.club.name, failed whether or not the where query joined the nested association, because order() split the path at its first dot only. order() now walks the path one association at a time. It reuses a join that the criteria registered under the full path, such as team.club, and joins any other association on the path for the sort, registering that join like any other. A sort through an explicit alias, such as t.club.name, works the same way. Because the sort join is registered instead of created anew for every order, a sort by two properties of the same association no longer fails with duplicate association path.

A sort by the identifier of an association that follows another one on the path, such as team.club.id, is sorted by the foreign key of the association before it, so it keeps the players whose team has no club, as a where query without criteria on team already did on 7.0.x. An association without such a foreign key, a hasOne or the inverse side of a one-to-one, is joined.

Sorting by an explicit alias gives the same order as before, and so does sorting by the identifier of an association, such as club.id, in a query with criteria on club, which now goes through the join instead of the foreign key.

The "Sorting with Associations" section of the where query docs said that this sort fails and that an explicit alias is required. It now shows the sort by the association path, says that the path can cross more than one association, that an association is joined with an inner join unless the query requests another join type, how to request a left join, and how the identifier of a nested association is sorted.

Results that change

  • What changes: A where query, a detached criteria or a dynamic finder that requests a left join of an association, with join('club', JoinType.LEFT) or fetch: [club: JoinType.LEFT], and sorts by a property of that association, returns the instances without the association, and the totalCount of a paged list counts them. Grails 7.0.17 left them out, because the sort joined the association again with an inner join, although the same query without the sort returned them. With three teams, one of them without a club, Team.where { name ==~ '%First%' }.join('club', JoinType.LEFT).list(max: 10, sort: 'club.name') returns 2 teams on 7.0.x, and 3 with this change and with Hibernate 7 on 8.0.x.
  • How to get the old result: Request an inner join with JoinType.INNER, or add a criterion such as club != null.
  • Setting: No setting. The old result contradicts what the call asks for, a left join, and Hibernate 7 already returns the new rows for the same call, so this is the wrong-result case of the rule in docs: add the branch policy for fixes that change a result #17061. list and criteria queries with the same fetch argument still leave the instances out, until Apply a join type that a criteria query requests #17069 and Apply a join type in the fetch argument of list and the example queries #17071.

Apart from this, only queries that failed before behave differently. A sort by an association for which the query requests no join type still joins it with an inner join, as on 7.0.x.

The upgrade guide has a note under Grails 7.0.18, and the upgrade skill has the same entry. As a fix that changes a result, this needs two reviewers.

Tests

WhereQueryWithAssociationSortSpec asserted the QueryException. It now checks:

  • a sort by club.name with criteria on club, ascending and descending, with a sort map and with chained where calls, and by club.id, by an explicit alias, and by club.name without criteria on club;
  • a sort by two properties of club when the query has no criteria on it;
  • a sort of Player by team.club.name with criteria on team.club, on team only and on neither, and by t.club.name through an explicit alias of team;
  • that a sort by club.name leaves out a team without a club, and keeps it when fetch requests a left join;
  • that a sort by team.club.id keeps a player whose team has no club, also through an explicit alias and with criteria on team, and that a sort by the identifier of a nested hasOne works.

Team.club is nullable in the fixture for these cases. 13 of the 20 tests fail with the AbstractHibernateQuery of 7.0.x: 12 with a QueryException, and the one for the requested left join with the rows of 7.0.x.

CI passes with run ci.

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

Verified locally on 0676c67 with :grails-data-hibernate5-core:test. The spec passes, and probes beyond it pass as well: a paged list(max: 2) with totalCount, max plus offset descending, .order('club.name', 'desc') on the detached criteria, a disjunction that uses the association (club.name == 'Arsenal' || name == '...'), a multi-key sort map mixing club.name and name, an explicit alias in the where query combined with a sort by club.name, and ignoreCase: false. The paged count works because HibernateQuery.clone() copies the criterion entries and the subcriteria but not the orders, so the count query carries no order by.

The change is right: getOrCreateAlias registers the join under both the association path and the alias, so resolving the order through criteriaAndAlias.alias covers the generated alias, the explicit alias and the association id in one place, and the detachedCriteria branch closes a latent NPE the old code had for a subquery-backed query. I also confirmed that a sort by an alias the query does not define is silently ignored (the else branch finds no such property), as the docs now say.

One thing to point out: the sort resolves a single association level only. A nested path such as team.club.name still throws, whether or not the where query joins it, while the new docs sentence reads as if any association path works. Details inline on order(). The other inline note is a merge-up heads-up for the Hibernate 7 docs on 8.0.x.

Without an explicit alias, sorting on association properties may fail due to dynamically generated aliases.

For example the following query will fail:
To sort by a property of an associated entity, use the association path. This also works when the `where` query uses the same association:

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.

Merge-up note: on 8.0.x the Hibernate 7 copy of this guide (grails-data-hibernate7/docs/src/docs/asciidoc/querying/whereQueries.adoc, section "Query Aliases and Sorting") still shows the owner.lastName sort as failing because of a dynamic alias, while the Hibernate 7 WhereQueryWithAssociationSortSpec there already expects it to work. The merge-up carries only this file, so that section stays stale unless it is aligned in the merge-up or a follow-up.

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.

Thanks. That section was never updated for Hibernate 7. I'll align it in a separate PR against 8.0.x, after checking the Hibernate 7 behavior a bit beyond what its spec covers.

if (createdAssociationPaths.containsKey(sortHead)) {
// sortHead may be either an explicit alias or an association path that
// was joined (with a generated alias) by a where query criterion.
// Hibernate can only resolve the property through the alias.

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 resolves one level only: property is split at the first dot, so a sort by a nested path such as team.club.name (Player -> Team -> Club) still throws a QueryException, whether or not the where query joins team.club. It threw before this change too, so it is not a regression, but the new docs sentence ("To sort by a property of an associated entity, use the association path") reads as if any depth works. Either extend this to nested paths, or state the one-level scope in the docs and leave nested paths to a follow-up.

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.

Right, it's one level only, as before. I've stated that in the docs now and would leave nested paths for a follow-up. Supporting them means walking the path, reusing the joins that the criteria register under the full path (such as team.club), and adding inner joins for the rest, which I'd rather not do on 7.0.x.

Explicit aliases don't help with nested paths either: the where DSL doesn't recognize def c = team.club, or def t = team; def c = t.club, as an alias, so a query that uses c doesn't compile.

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.

I'm actually going to fix this ...

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.

Pushed c0c5a80 to this branch. It reverts b494caa and makes order() walk the sort path one association at a time: a join the criteria registered under the full path, such as team.club, is reused, and each association not joined yet gets an inner join, so team.club.name sorts whether or not the where query joins it, and t.club.name works through an explicit alias of team. A sort by two properties of the same association no longer raises duplicate association path either, since the sort join is registered instead of created for every order. WhereQueryWithAssociationSortSpec covers the nested and duplicate cases (all six failed before the change), and the docs sentence now says the path can cross more than one association.

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.

@matrei I think it's worth doing this after looking at it. Take a look at the change and see what you think.

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.

Thanks, that looks good to me, and it brings Hibernate 5 in line with Hibernate 7, which already sorts by nested paths. I ran the grails-data-hibernate5-core tests on c0c5a80 locally, since CI didn't run them after I removed run ci, and they all pass.

Comparing a few queries before and after the change, the results are the same except for one: with fetch: [owner: JoinType.LEFT] on a nullable association, a sort by owner.name now keeps the rows without an owner, because the sort join uses the requested join type instead of an inner join. I think that's right, but since it changes results on 7.0.x, I'll mention it in the PR description.

@matrei matrei added run ci and removed run ci labels Nov 4, 2026
@matrei matrei closed this Nov 4, 2026
@matrei matrei reopened this Nov 4, 2026

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

I reviewed c0c5a80 and it looks good to me. Walking the path one association at a time and going through getOrCreateAlias reuses the joins the where query made. It also registers the sort's own join, which fixes the duplicate association path error for two sorts on the same association.

Two minor points, neither blocking:

  • No test covers the LEFT fetch join now being used for the sort.
  • The docs could say that a sort joins an association with an inner join unless fetch asks for another type, so a sort by owner.name leaves out pets without an owner. That was already the case before this change.

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

Sorting by an association path reuses a join the where query already made and orders by that join's alias. A path through more than one association works the same way. Not approving. A sort by a nested identifier, such as team.club.id, inner-joins the last association and drops rows whose association is null. Those rows were returned before, because the identifier was ordered through the foreign key.

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

Pushed 5e71443 for the feedback on c0c5a80.

@jamesfredley: a sort by a nested identifier keeps its rows again. team.club.id is sorted by the foreign key on team, as it was on 7.0.x, and club is joined only when there is no such foreign key. Details are in the thread.

@matrei: both points are in the same commit.

  • WhereQueryWithAssociationSortSpec now covers the join type of a sort join: an inner join by default, which leaves out a team without a club, and a left join with fetch: [club: JoinType.LEFT], which keeps it. Team.club is nullable in the fixture for that.
  • The docs say that an association is joined with an inner join unless the query requests another type, show the fetch example, and mention the nested identifier.

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

Follow-up to my review above: 2154153 and 1b70845 replace the approach of 5e71443.

A sort should not change which rows a query returns, so an association that is joined only for a sort is now joined with a left join. That keeps the rows for every association on a sort path, so the foreign-key ordering for a nested identifier from 5e71443 is removed again.

@matrei: this goes beyond the two points in your review and changes results on 7.0.x, so I have updated the PR description and added section 22 to the upgrade notes.

  • Where queries, detached criteria and dynamic finders (2154153): without a requested join type the sort join was an inner join, and a sort by club.name or club.id left out the teams without a club, from the list and from the totalCount of a paged list. They are kept now. A join type requested for the association, such as fetch: [club: JoinType.INNER], is used for the sort, and an association the query has criteria on is still joined by the criteria. calculateProjectionPropertyName in the same class already left-joins the associations it joins for a projection, for the same reason.
  • list, find and findAll with an example, findWhere, findAllWhere and a criteria query with pagination arguments (1b70845) had the same inner join in GrailsHibernateQueryUtils and GrailsHibernateUtil. Team.list(sort: 'club.name') left out the teams without a club, and findAllWhere with that sort did not find such a team at all. They use a left join now too.
  • The where query docs say that sorting does not change which rows a query returns. WhereQueryWithAssociationSortSpec and the new AssociationSortSpec cover it, and nine cases of each fail on c0c5a80.

@jamesfredley: the details for team.club.id are in the thread.

@jdaugherty

Copy link
Copy Markdown
Contributor

@jamesfredley @matrei @borinquenkid, a question that came up while testing this PR and that the PR does not change: what should the fetch argument of the static list() do when the association is null?

With four teams, one of which has no club (Team.club is nullable):

Query 7.0.x (Hibernate 5) 8.0.x (Hibernate 7)
Team.list(fetch: [club: 'join']) 3 teams, club not fetched 3 teams, club fetched
Team.where { ... }.list(fetch: [club: 'join']) 4 teams, fetched 4 teams, fetched
Team.findAllByNameLike('%First%', [fetch: [club: 'join']]) 4 teams, fetched 4 teams, fetched
Team.createCriteria().list(fetch: [club: 'join'], max: 10) { ... } 4 teams, fetched 4 teams, not fetched
Team.findAllWhere([name: 'MU First Team'], [fetch: [club: 'join']]) found, fetched found, not fetched

"Fetched" is Hibernate.isInitialized(team.club) after clearing the session. fetch: [club: 'eager'] behaves like 'join' in list().

So in list() the argument leaves out the team without a club, on both versions:

  • On 7.0.x, GrailsHibernateQueryUtils.populateArgumentsForCriteria turns it into queryRoot.join('club'). That is an inner join and not a fetch, so it only filters.
  • On 8.0.x, HqlListQueryBuilder appends join fetch e.club, an inner fetch join.

It also means that Team.list(sort: 'club.name', fetch: [club: 'join']) still leaves the team out after this PR, because of fetch and not because of the sort.

The options I see:

  1. A left fetch join in list(), which is what the where queries and dynamic finders do. It changes which rows list() returns, like the sort change in this PR, and on 7.0.x list() starts to fetch the association.
  2. A left join without the fetch on 7.0.x, so that the argument no longer filters and still does not fetch.
  3. Leave it as it is and document it.

What do you think should happen, and on which branch? The two rows where Hibernate 7 does not fetch look like a separate gap there.

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

A sort by a nested identifier such as team.club.id now left-joins that association and keeps the rows whose association is null. A paged criteria count can sort by a nested path, and each entry of a sort map is kept. Not approving. A sort stored on the query is applied before the list arguments, so an inner join requested there does not replace the left join the sort already created.

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

Pushed b482d67, 4646683 and e6343c4 for the review of c6b4d61.

@jamesfredley: an inner join requested for the association now wins over a sort stored on the query, so Team.where { name == 'Unattached First Team' }.sort('club.name').list(fetch: [club: JoinType.INNER]) leaves the team out. A sort no longer joins an association when it is added to the query. The join is made when the query is executed, with the join type the query requests by then. Details are in the thread.

@matrei: 4646683 and e6343c4 go beyond the review and change results on 7.0.x again, so they are in the PR description, in the fetching guide and in section 23 of the upgrade notes. A join type other than LEFT that a where query, a detached criteria or a dynamic finder requests is now applied whether or not the query has criteria or a sort on the association. Before, Team.where { ... }.list(fetch: [club: JoinType.INNER]) fetched the club with an outer join and returned the teams without a club. Only a sort or a criterion on club made the inner join take effect, so a sort still changed the rows of such a query.

e6343c4 does the same for a criteria query. The criteria builder has accepted join('club', JoinType.INNER) since the join type was added to BuildableCriteria, and never used it: the method set a fetch mode like the one-argument join. A JoinType in the fetch argument of the builder's list matched no fetch mode and did not fetch at all. Both now join and fetch with the requested type.

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

A requested inner join is applied when the query runs, and a nested identifier sort still keeps the rows whose association is null. Not approving. A subquery that both joins an association and filters through it fails with a duplicate association path, and a right join of a collection does not fetch that collection.

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

Not approved. A collection join that stands in for an inner join ignores a Hibernate filter, and a later join or select does not replace a requested inner join. A requested fetch of a collection is also dropped when another join traverses that collection. A subquery that joins an association and then filters through it no longer fails with a duplicate path. A right join of a collection now fetches it. Threads PRRT_kwDOAAfRJ86z71iD and PRRT_kwDOAAfRJ86z71iI resolved.

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

Pushed six commits for the review of c5b0edb. The head is 9c59ac4.

@jamesfredley: the three points are fixed, and the details of the first two are in the threads.

  • A join that is requested for a collection applies the filters of the session (6ce701e). The query is restricted with a subquery that joins the collection, instead of a check that the collection is not empty, so a shelf whose books all belong to another tenant is left out.
  • A later join or select replaces a requested join type, in a criteria query and on a where query, and so does the fetch argument of a criteria query (b9f26d4).
  • A fetch that is requested for a collection is kept when another requested join traverses the collection (f4ef5b1). A path through a collection, as in fetch: ['players.contract': JoinType.INNER], is now joined in the same kind of subquery and no longer in the query itself. That also makes count and the totalCount of a paged list count instances instead of joined rows, and a criteria query return each instance once.

The other three commits come from a second pass over the branch:

  • 71b4535: every join of a where query is made when the query is executed, with the join type that the query requests by then. A join type that the fetch argument of list requests is now used for the join that the criteria or a projection of the query make. It was read after they had joined the association, so Team.where { club.name == 'Chelsea' || name == 'Unattached First Team' }.list(fetch: [club: JoinType.LEFT]) left out the team without a club. The join type of a nested association is looked up by its path, criteria on a nested association can be used more than once, and a sort stored on the query can be combined with criteria that list adds through an alias.
  • 8ef8631: a sort by two properties of one association no longer fails in find and findAll with an example, findWhere and findAllWhere, and list joins the association once for it.
  • 9c59ac4: order in a criteria query can sort by a property of an association. It failed, and with a Query.Order it sorted by a property of the queried class.

25 cases of AssociationJoinTypeSpec, WhereQueryWithAssociationSortSpec and the new MultiTenancyRequestedJoinSpec fail on c5b0edb, and 13 cases of AssociationSortSpec fail on b9f26d4.

@matrei: 71b4535, f4ef5b1 and 9c59ac4 change results on 7.0.x in a few more places. Sections 22 and 23 of the upgrade notes cover them, and the PR description is updated.

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

Not approved. An order by a collection leaves that collection unfetched even when the query asks to fetch it. An order by an association also leaves a requested inner join as a left join, so a team with no club is returned. A later join or select now replaces a requested join type, and a collection restriction uses the session filters.

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

Pushed eight commits for the review of 9c59ac4. The head is cc58a3b.

@jamesfredley: both points are fixed in 4988d60, and the details are in the threads. order in a criteria query joined the association of its property at once, before the rest of the criteria and the fetch argument of list were known. The criteria builder now adds its orders once the criteria are built, after the join types that they request. An order therefore uses the join type that is requested for its association, and a collection that the criteria ask to fetch is fetched. This holds whether the request comes before or after the order.

97bd0e5 makes the same change for find and findAll with an example, findWhere and findAllWhere, where a sort by a property of a collection did not fetch the collection when the fetch argument asked for it.

The other six commits come from another pass over the branch:

  • 5a94a73: order('club.id') in a criteria query makes no join again and reads the foreign key. Since 9c59ac4 it joined club, and a query with projections { distinct('club.id') } and that order failed.
  • 7e847ed: inside the criteria of an association, order(Query.Order.asc('name')) sorts by the property of that association, as order('name') does. It sorted by the property of the queried class.
  • 75b5b4d: a where query that has criteria on a collection and requests JoinType.LEFT for it no longer fetches the collection through the join of the criteria. That join only has the elements that the criteria match, so Team.where { players { name == 'A' } }.list(fetch: [players: JoinType.LEFT]) returned the team with one player in the collection. The collection is now loaded when it is read.
  • 60450f9: the subquery of a requested join has a short alias. The alias was made from the association path, and Hibernate uses it as it is in the SQL.
  • e8f7036: two corrections in the fetching guide and the upgrade notes.
  • cc58a3b: updateAll and deleteAll apply a requested join type. Team.where { ... }.join('club', JoinType.INNER) listed and counted the teams with a club, but updateAll and deleteAll on it also updated or deleted the teams without one. An update and a delete cannot join, so each requested join is applied to them as a subquery restriction.

28 cases of AssociationJoinTypeSpec and AssociationSortSpec fail on 9c59ac4, and nine cases for updateAll and deleteAll fail on e8f7036.

@matrei: 97bd0e5, 7e847ed, 75b5b4d and cc58a3b change results on 7.0.x. Sections 22 and 23 of the upgrade notes have an entry for each, and the PR description is updated.

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

One more commit since the review above. The head is 1ab83ab.

updateAll and deleteAll apply a requested join type with a subquery (cc58a3b). For a path that leads back to the queried class, such as the parent of a category, that subquery selects from the table that the update or the delete modifies. MySQL, and MariaDB before version 10.3, do not allow that, so the statement failed there with an error of the database. With 1ab83ab, updateAll and deleteAll throw an UnsupportedOperationException on these databases. It names the association and says to restrict the query with criteria instead, as in Category.where { parent != null }. Other databases run the statement.

RequestedJoinBulkOperationDialectSpec covers this with the MySQL and MariaDB dialects, and four of its nine cases fail on cc58a3b. The fetching guide and section 23 of the upgrade notes describe it.

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

Not approved. A listener that adds an association restriction to findAll is ignored, and the query fails to resolve the alias. Sorting an example query by an association still returns a row whose association is null when an inner join was requested. A criteria list that asks to fetch a collection it sorts by now fetches it.

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

Pushed two commits for the review of 1ab83ab. The head is 0c81f00.

@jamesfredley: both points are fixed in 688440e, and the details are in the threads. A listener's criteria on an association now get their joins after the PreQueryEvent, and find and findAll with an example, findWhere, findAllWhere and list apply a JoinType in their fetch argument, before a sort by the association, which uses that join.

A second review of the branch, with each finding run against 7.0.x, found seven more regressions. The same commit fixes them:

  • deleteAll with an inner join of a collection that has a join table deleted no instance and removed the join rows of the instances it should have deleted. Hibernate deletes the join rows first, with the same restriction. The identifiers are now selected first.
  • updateAll and deleteAll matched the side of a one-to-one that the other class has the foreign key of, as Nose.face with static belongsTo = [face: Face], by comparing the two primary keys, and changed other instances than list returns.
  • A sort by a collection that has a join table and multi-tenant elements produced SQL that no database accepts: Hibernate combines the condition that keeps the collection from being fetched with the tenant filter of the join table. The condition is now part of the where clause.
  • A sort by players.name in a query that fetched reserves, a collection of the same class, filled reserves with the players. The sort now fetches its collection as well when the query fetches another one.
  • join('club', JoinType.LEFT) followed by fetch: [club: 'join'] lost the left join for the criteria on club, as on 7.0.x it did not.
  • A paged criteria query sorted through an alias, as sort: 't.club.name', lost its sort.
  • A nested sort path was resolved through an alias of the criteria that has the name of the first association on the path.

0c81f00 fixes older issues that the review found in what this branch changes or documents: order('contract.id') on a hasOne inner-joined, a paged criteria query with a map of sort properties failed, a projection into an embedded component of an association failed, a right join of a collection that criteria join returned null rows, a join type for a collection of a basic type was ignored, and the associations before a collection on a requested path were not fetched. It also corrects two statements in the docs.

50 of the new cases fail on 1ab83ab. The whole test suites of grails-data-hibernate5-core and grails-datamapping-core pass, and so does codeStyle for both.

@matrei: both commits change results on 7.0.x in a few more places, and sections 22 and 23 of the upgrade notes describe each of them. grails-datamapping-core gains getSupersededJoinTypes() on the detached criteria, so that a join type that a later fetch mode replaced still reaches the query. The PR description is updated.

matrei and others added 4 commits November 19, 2026 09:30
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.
@jdaugherty
jdaugherty force-pushed the fix/15589-where-query-association-sort-7.0.x branch from 7323d92 to e210655 Compare November 19, 2026 16:03
jdaugherty added a commit to jdaugherty/grails-core that referenced this pull request Nov 19, 2026
… 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.
jdaugherty added a commit to jdaugherty/grails-core that referenced this pull request Nov 19, 2026
A sort whose path starts with a name that is neither a property of the
queried class nor an alias of the query was ignored: the query returned
its rows in the same order for asc and desc, without an error. Any
other invalid sort property throws IllegalArgumentException: Invalid
sort property, and such a sort now does the same, in where queries,
detached criteria, dynamic finders, list, the example queries and the
sort argument of a criteria query.

Part 14 of the split of apache#16853.
jdaugherty added a commit to jdaugherty/grails-core that referenced this pull request Nov 19, 2026
A sort whose path starts with a name that is neither a property of the
queried class nor an alias of the query was ignored: the query returned
its rows in the same order for asc and desc, without an error. Any
other invalid sort property throws IllegalArgumentException: Invalid
sort property, and such a sort now does the same, in where queries,
detached criteria, dynamic finders, list, the example queries and the
sort argument of a criteria query.

Part 14 of the split of apache#16853.
@jdaugherty

jdaugherty commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

@matrei the split we agreed on here and on dev@ is done.

This PR is reset to part 1 (e210655, your commits rebased onto the current 7.0.x). The previous head, 7323d92, is kept at preserve/16853-full on my fork.

Parts 2 to 13 are a stack of 12 pull requests against 7.0.x, one commit each, each on top of the one before it. Until the ones below it merge, each also shows their commits; the commit to review is named in its description. Part 14 is a pull request of its own against 9.0.x. All of them are ready for review.

Part Pull request What it does Where it goes Setting
1 this PR Sort a where query by an association it filters on, nested paths, a nested id through the foreign key 7.0.x now no
2 #17062 list sorts by every entry of a sort map, and joins an association once 7.0.x now no
3 #17063 totalCount of a paged detached criteria keeps the root alias 7.0.x now no
4 #17064 count() on a query that has a sort no longer throws; it counts the rows that list returns 7.0.x now no
5 #17065 A sort by an association keeps the rows without it, behind grails.gorm.leftJoinAssociationSort; adds the settings page to the guide 7.0.x, behind the setting adds it
6 #17066 The joins of a where query are made when the query is executed, so a join type that is requested late is used 7.0.x sort joins only
7 #17067 A requested join type is applied without criteria or a sort 7.0.x, once Hibernate 7 has the same rule on 8.0.x no
8 #17068 updateAll and deleteAll apply the requested join type (the other half of 7) with 7 no
9 #17069 A criteria query applies a requested join type with 7 no
10 #17070 order('author.name') in a criteria query works 7.0.x left join only with the setting
11 #17071 list and the example queries apply a join type in fetch with 7 no
12 #17072 Sort by and join a collection without changing what a query fetches 7.0.x, behind the setting yes, all of the sort changes
13 #17073 A sort can start with the alias of the queried class 7.0.x no
14 #17074 A sort by an alias that the query does not define throws 9.0.x: it makes a working call throw no

What differs from the split I described above:

  • The sort left join is behind a setting, grails.gorm.leftJoinAssociationSort, off by default, as the thread settled. It is one of the settings shared by the datastores, so Keep the rows without an association when sorting by it with Hibernate 7 #16863 can read the same key with Hibernate 7. Every sort join is covered: list, the example queries, dynamic finders, where queries, detached criteria, the sort argument and order of a criteria query, and the collection sorts of part 12.
  • Part 4 is new. count() on a where query with a sort threw on H2, also for a sort by a plain property. It now counts the rows that list returns: 4 of the 5 teams by default, and 5 with the setting.
  • The specs run with the setting off and on. The three sort specs each have a subclass that turns it on, and a feature whose result depends on it says so. Every part passes on its own in both modes, so the parts no longer have to release together.
  • Part 9 has one more fix. A criteria query with a sort argument and a fetch join type for the same association failed with duplicate association path until part 10. The sort now uses the join that the criteria already have.
  • The last part is split in two. Part 13 makes a sort through the alias of the queried class work. Part 14 is the new exception for an undefined alias, which is the kind of change the rule sends to the next major, so it is one commit on 9.0.x that does not depend on the other parts.
  • Each part that changes a result has its note under Grails 7.0.18 in the upgrade guide and the same entry in the upgrade skill.

The Hibernate 7 side of the setting is in #16863, which is merged with the current 8.0.x and reads the same key.

The requested join type parts (7, 8, 9 and 11) cover Hibernate 5 only. The same rule and spec for Hibernate 7 is #17079 on 8.0.x, stacked on #16863, and it goes in before these merge into 7.0.x.

Not done yet:

Related issues and pull requests:

Updated after the pull requests were opened: all parts are ready for review, part 14 moved to 9.0.x, the Hibernate 7 pull request for the join types is #17079, and the related issues are listed above.

@jdaugherty

Copy link
Copy Markdown
Contributor

One more note on the split: the requested join type parts (#17067, #17068, #17069 and #17071) cover Hibernate 5 only. 8.0.x will need a separate pull request that gives Hibernate 7 the same rule and spec, and as agreed on dev@ that one goes in before these merge into 7.0.x, so the merge-up does not leave the two modules returning different rows for the same query.

The sort setting already has its Hibernate 7 side in #16863, which now reads the same grails.gorm.leftJoinAssociationSort key.

jdaugherty added a commit to jdaugherty/grails-core that referenced this pull request Nov 19, 2026
With GORM for Hibernate 5, a sort whose path starts with a name that is
neither a property of the queried class nor an alias of the query was
ignored: the query returned its rows in the same order for asc and desc,
without an error. Any other invalid sort property throws
IllegalArgumentException: Invalid sort property, and such a sort now
does the same, in where queries, detached criteria, dynamic finders,
list, the example queries and the sort argument of a criteria query.

A sort that starts with an alias that the query defines, or with the
alias of the queried class, is left as it is.

Part 14 of the split of apache#16853. It makes a call that runs today throw,
so it goes to the next major and not to 7.0.x.
@matrei

matrei commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

@jdaugherty thanks for the split, and for keeping 7323d92. I checked the reset: its five commits are the ones at 5e71443 (git range-diff shows each as equal), it merges cleanly with the current 7.0.x, and CI passes.

On 11-17 I said that after the reset I'd make a join that only the sort needs an inner join again, and take the fetch: [owner: JoinType.LEFT] example out of the where query guide. I'd rather keep the change as it is. With three teams that match, one of them without a club:

7.0.x This PR (e210655) Hibernate 7, 8.0.x (0518f48)
Team.where { name ==~ '%First%' }.join('club', JoinType.LEFT).list(max: 10, sort: 'club.name') 2 rows, totalCount 2 3, 3 3, 3
The same with list(max: 10, sort: 'club.name', fetch: [club: JoinType.LEFT]) 2, 2 3, 3 3, 3
new DetachedCriteria(Team).like('name', '%First%').join('club', JoinType.LEFT).list(max: 10, sort: 'club.name') 2, 2 3, 3 3, 3
Team.where { name ==~ '%First%' }.join('club', JoinType.LEFT).list(max: 10), without the sort 3, 3 3, 3 3, 3

On 7.0.x the sort overrides the join type that the call asks for: the query requests a left join, gets an inner one, and loses a row that it returns without the sort. That's the wrong-result case of the rule in #17061, so the fix goes to 7.0.x. It doesn't need a setting, since Hibernate 7 already returns the new rows for the same call. The stack agrees: at #17073 with grails.gorm.leftJoinAssociationSort off, these queries return 3 rows, and a sort without a requested join type returns 2, as on 7.0.x. This also answers James's comment on #17063, where the total goes from 2 to 3 along with the list.

Item 1 therefore changes a result, not only queries that threw, and needs what the rule asks for. I'll add:

  • a "Results that change" section in the description:
    • What changes: a where query or a detached criteria that requests a left join of an association, with join or with fetch, and sorts by a property of it, returns the rows without the association. Grails 7.0.17 left them out, because the sort joined the association with an inner join.
    • How to get the old result: request an inner join (JoinType.INNER), or add club != null to the criteria.
    • Setting: none, for the reasons above.
  • a note under Grails 7.0.18 and the same entry in the upgrade skill.

It also needs a second reviewer.

The fetch: [owner: JoinType.LEFT] example stays in the guide, as the paragraph before it already describes the new rule. list and criteria queries with the same fetch still leave the row out until parts 9 and 11, which wait for #17079, so the note names where queries and detached criteria only.

A where query, a detached criteria or a dynamic finder that requests a
left join of an association and sorts by a property of it now returns
the instances without the association, and the totalCount of a paged
list counts them. Grails 7.0.17 left them out, because the sort joined
the association again with an inner join. Add a note under Grails 7.0.18
to the upgrade guide, and the same entry to the upgrade skill.
@testlens-app

testlens-app Bot commented Nov 20, 2026

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: 7bad57e
▶️ Tests: 54223 executed
⚪️ Checks: 57/57 completed


Learn more about TestLens at testlens.app/docs.

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

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants