Repository navigation
Conversation
jdaugherty
left a comment
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
I'm actually going to fix this ...
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
@matrei I think it's worth doing this after looking at it. Take a look at the change and see what you think.
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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
LEFTfetch join now being used for the sort. - The docs could say that a sort joins an association with an inner join unless
fetchasks for another type, so a sort byowner.nameleaves out pets without an owner. That was already the case before this change.
jamesfredley
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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.
WhereQueryWithAssociationSortSpecnow 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 withfetch: [club: JoinType.LEFT], which keeps it.Team.clubis 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
fetchexample, and mention the nested identifier.
jdaugherty
left a comment
There was a problem hiding this comment.
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.nameorclub.idleft out the teams without a club, from the list and from thetotalCountof a paged list. They are kept now. A join type requested for the association, such asfetch: [club: JoinType.INNER], is used for the sort, and an association the query has criteria on is still joined by the criteria.calculateProjectionPropertyNamein the same class already left-joins the associations it joins for a projection, for the same reason. list,findandfindAllwith an example,findWhere,findAllWhereand a criteria query with pagination arguments (1b70845) had the same inner join inGrailsHibernateQueryUtilsandGrailsHibernateUtil.Team.list(sort: 'club.name')left out the teams without a club, andfindAllWherewith 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.
WhereQueryWithAssociationSortSpecand the newAssociationSortSpeccover it, and nine cases of each fail on c0c5a80.
@jamesfredley: the details for team.club.id are in the thread.
|
@jamesfredley @matrei @borinquenkid, a question that came up while testing this PR and that the PR does not change: what should the With four teams, one of which has no club (
"Fetched" is So in
It also means that The options I see:
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
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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
joinorselectreplaces a requested join type, in a criteria query and on a where query, and so does thefetchargument 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 makescountand thetotalCountof 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
fetchargument oflistrequests 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, soTeam.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 thatlistadds through an alias. - 8ef8631: a sort by two properties of one association no longer fails in
findandfindAllwith an example,findWhereandfindAllWhere, andlistjoins the association once for it. - 9c59ac4:
orderin a criteria query can sort by a property of an association. It failed, and with aQuery.Orderit 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
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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 joinedclub, and a query withprojections { 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, asorder('name')does. It sorted by the property of the queried class. - 75b5b4d: a where query that has criteria on a collection and requests
JoinType.LEFTfor it no longer fetches the collection through the join of the criteria. That join only has the elements that the criteria match, soTeam.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:
updateAllanddeleteAllapply a requested join type.Team.where { ... }.join('club', JoinType.INNER)listed and counted the teams with a club, butupdateAllanddeleteAllon 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
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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:
deleteAllwith 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.updateAllanddeleteAllmatched the side of a one-to-one that the other class has the foreign key of, asNose.facewithstatic belongsTo = [face: Face], by comparing the two primary keys, and changed other instances thanlistreturns.- 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.namein a query that fetchedreserves, a collection of the same class, filledreserveswith the players. The sort now fetches its collection as well when the query fetches another one. join('club', JoinType.LEFT)followed byfetch: [club: 'join']lost the left join for the criteria onclub, 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.
A sort by a path through more than one association, such as owner.employer.name, is still not supported.
This reverts commit b494caa.
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.
7323d92 to
e210655
Compare
… 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.
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.
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.
|
@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 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.
What differs from the split I described above:
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. |
|
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 |
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.
|
@jdaugherty thanks for the split, and for keeping 7323d92. I checked the reset: its five commits are the ones at 5e71443 ( 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
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 Item 1 therefore changes a result, not only queries that threw, and needs what the rule asks for. I'll add:
It also needs a second reviewer. The |
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.
✅ All tests passed ✅🏷️ Commit: 7bad57e Learn more about TestLens at testlens.app/docs. |
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
QueryExceptionwhen the query also had criteria on that association:The criteria join
clubunder a generated alias, such asclub_alias0, andAbstractHibernateQueryrecords 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, asclub_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, becauseorder()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 asteam.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 ast.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 withduplicate 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 onteamalready did on 7.0.x. An association without such a foreign key, ahasOneor 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 onclub, 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
join('club', JoinType.LEFT)orfetch: [club: JoinType.LEFT], and sorts by a property of that association, returns the instances without the association, and thetotalCountof 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.JoinType.INNER, or add a criterion such asclub != null.listand criteria queries with the samefetchargument 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
WhereQueryWithAssociationSortSpecasserted theQueryException. It now checks:club.namewith criteria onclub, ascending and descending, with a sort map and with chainedwherecalls, and byclub.id, by an explicit alias, and byclub.namewithout criteria onclub;clubwhen the query has no criteria on it;Playerbyteam.club.namewith criteria onteam.club, onteamonly and on neither, and byt.club.namethrough an explicit alias ofteam;club.nameleaves out a team without a club, and keeps it whenfetchrequests a left join;team.club.idkeeps a player whose team has no club, also through an explicit alias and with criteria onteam, and that a sort by the identifier of a nestedhasOneworks.Team.clubis nullable in the fixture for these cases. 13 of the 20 tests fail with theAbstractHibernateQueryof 7.0.x: 12 with aQueryException, and the one for the requested left join with the rows of 7.0.x.CI passes with
run ci.