Repository navigation
Sort list by every sort property, and join an association once - #17062
jdaugherty wants to merge 6 commits into
Conversation
Sorting a where query by a property of an association, such as sort: 'club.name', failed with a QueryException when the query also had criteria on that association. The criteria join the association under a generated alias, and the order was added with the association path, which Hibernate cannot resolve. The order now uses the alias of the join.
A sort by a path through more than one association, such as owner.employer.name, is still not supported.
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.
list with a map of sort properties, such as Team.list(sort: ['club.name': 'asc', name: 'desc']), sorted by the last entry only, because each entry replaced the orders of the query. The same held for a default sort that the mapping declares for more than one property. Every entry is now applied, in the order of the map. An association is joined once for all the sort properties that use it. list joined it once for each, and find and findAll with an example, findWhere and findAllWhere failed with "duplicate association path". Part 2 of the split of apache#16853.
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A sort by an association's identifier drops a row whose association is empty.
| private boolean isForeignKeyIdentifier(PersistentEntity owner, Association association, String property) { | ||
| PersistentEntity associatedEntity = association.getAssociatedEntity(); | ||
| PersistentProperty identity = associatedEntity != null ? associatedEntity.getIdentity() : null; | ||
| if (session == null || identity == null || !identity.getName().equals(property)) { |
There was a problem hiding this comment.
A player whose team has no club should stay when the list sorts by team.club.id. The player is dropped, because the sort joins club instead of using the foreign key. Sort by the foreign key, and keep the player.
There was a problem hiding this comment.
This part only changes the sort-map handling and join reuse in GrailsHibernateQueryUtils and GrailsHibernateUtil; AbstractHibernateQuery is unchanged from part 1. Part 1 already reads a nested association identifier through its foreign key for where queries and detached criteria, and those regression cases pass.
If the reproducer is Player.list(sort: "team.club.id"), that is the separate static-list path, which already joins the final association before this part. Please open a follow-up issue for that case rather than extending the sort-map PR. If this is a where query or detached criteria, could you post the public GORM call and fixture that fail? That would let us target the finding to the part that introduces it.
matrei
left a comment
There was a problem hiding this comment.
Reviewed at 279520e (base 7.0.x), the last commit; the five below it are #16853. I ran this pull request's seven specs at the head and with part 1's main sources (e210655). Part 1 changes only AbstractHibernateQuery, so for list those are 7.0.x's. 7 of the 91 tests fail there and pass here; the other 84 pass on both.
The change is right, and it belongs on 7.0.x without a setting: a sort entry that is dropped, and a row repeated for each pair of elements, both contradict the call. Two things before it can land.
Needs a fix
1. Rows change, and the description and the note say they don't.
Team.list(sort: ['players.name': 'asc', 'players.id': 'asc'])returns a team with three players 9 times on 7.0.x, once for each pair that the two joins ofplayersmake. This head returns it 3 times. That's the test "list sorted by two properties of a collection returns a team once for each of its players".- With
maxoroffset, the rows on a page change once every entry sorts. #17061 counts that as a result change too.
Please put both in "Results that change", in the 7.0.18 note and in the skill entry. "No setting" still holds.
2. Add run ci. On 7.0.x the tests run only on request, so no part of the stack has run its tests in CI: the test jobs of this pull request were skipped. The description says the full build wasn't run locally either. The same goes for parts 3 to 13.
Notes
-
GrailsHibernateUtil.findJoinCriteriais a new public method on a patch line. Its only caller, here and at the top of the stack (#17073), isGrailsHibernateQueryUtils. As a private method there, with its three helpers, it adds no API that later lines have to keep. -
The 84 tests that pass without this commit pin the behavior that the later parts build on. None of the 58 feature methods changes up to #17073, so they work as guards. A reviewer of this part still has to find the 7 that it fixes, so a line in the description naming them would help:
list sorted by a map sorts by every entry of the maplist sorts by every property of the default sort of the mapping#api sorted by two properties of an association joins the association once, forlistandfindAll by examplelist sorted by two properties of a collection returns a team once for each of its playersfindAll by example can be sorted by two paths through the same associationfindAllWhere and findWhere can be sorted by two properties of an association
-
On James's comment:
Player.list(sort: 'team.club.id')does drop a player whose team has no club, but this commit doesn't change that. With one such player and one in a team with a club:
sort: 'team.club.id' |
7.0.x | Part 1 | This head | #17073, setting off | #17073, setting on |
|---|---|---|---|---|---|
Player.list(sort:), createCriteria().list(sort:) { } |
dropped | dropped | dropped | dropped | kept |
createCriteria().list { order('team.club.id') } |
throws | throws | throws | dropped | kept |
findAllByNameLike('%', [sort:]), where { name ==~ '%' }.list(sort:), DetachedCriteria.sort() |
kept | kept | kept | kept | kept |
where { team.name ==~ '%' }.list(sort:) |
throws | kept | kept | kept | kept |
So with the setting off, list and criteria queries drop a row that a dynamic finder and a where query keep for the same sort, and they already did on 7.0.x. That's the case where another API returns something else for the same request, so I'd fix it on 7.0.x without the setting: sort list and criteria queries by the foreign key of a nested association id, as part 1 does for where queries. That belongs in a part of its own, though, not in this one.
Verified as correct
- Hibernate 7 on 8.0.x (0518f48) already sorts
listby every entry of a sort map, andfindAllWherewith two sorts throughclubworks there. The merge-up brings Hibernate 5 to the same result. (Hibernate 7 rejects a sort by a property of a collection with aPathException, which is part 12's subject.) - The note sits under
=== Grails 7.0.18, without a number, and the skill has the same entry.
Description
Part 2 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 #16853. Until that merges, this pull request also shows the commits below it. The change to review is the last commit, 279520e.
list with a map of sort properties, such as
Team.list(sort: ['club.name': 'asc', name: 'desc']), sorted by the last
entry only, because each entry replaced the orders of the query. The
same held for a default sort that the mapping declares for more than
one property. Every entry is now applied, in the order of the map.
An association is joined once for all the sort properties that use it.
list joined it once for each, and find and findAll with an example,
findWhere and findAllWhere failed with "duplicate association path".
Results that change
listwith aMapof sort properties, or with a default sort on more than one property: every entry is applied, not only the last. The rows are the same.The upgrade guide has a note under Grails 7.0.18, and the upgrade skill has the same entry.
Verification
The association sort and join specs of
grails-data-hibernate5-corepass on this branch, withgrails.gorm.leftJoinAssociationSortoff and on, andcodeStylepasses for the touched modules. The full build was not run locally.Contributor Checklist
Please review the following checklist before submitting your pull request. Pull requests that do not meet these requirements may be closed without review.
Issue and Scope
7.0.x): Bug fixes only. No new features or API changes.7.1.x): New features are welcome, but breaking existing APIs must be avoided.8.0.x): Reserved for major changes. Breaking API changes are permitted.Code Quality
./gradlew build --rerun-tasks../gradlew codeStyleand resolved any violations. See Code Style for details.Licensing and Attribution
Documentation