Skip to content

Sort list by every sort property, and join an association once - #17062

Open
jdaugherty wants to merge 6 commits into
apache:7.0.xfrom
jdaugherty:fix/16853-02-sort-every-property
Open

jdaugherty wants to merge 6 commits into
apache:7.0.xfrom
jdaugherty:fix/16853-02-sort-every-property

Conversation

@jdaugherty

Copy link
Copy Markdown
Contributor

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

  • What changes: The order of the results of list with a Map of 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.
  • How to get the old result: Pass one sort property.
  • Setting: No setting. The call asks for the sort and it was dropped; no rows change.

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

Verification

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

Contributor Checklist

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

Issue and Scope

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

Code Quality

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

Licensing and Attribution

Documentation

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

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

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

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

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

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

Part 2 of the split of apache#16853.

@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 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)) {

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

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 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 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 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 of players make. 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 max or offset, 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

  1. GrailsHibernateUtil.findJoinCriteria is a new public method on a patch line. Its only caller, here and at the top of the stack (#17073), is GrailsHibernateQueryUtils. As a private method there, with its three helpers, it adds no API that later lines have to keep.

  2. 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 map
    • list sorts by every property of the default sort of the mapping
    • #api sorted by two properties of an association joins the association once, for list and findAll by example
    • list sorted by two properties of a collection returns a team once for each of its players
    • findAll by example can be sorted by two paths through the same association
    • findAllWhere and findWhere can be sorted by two properties of an association
  3. 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 list by every entry of a sort map, and findAllWhere with two sorts through club works there. The merge-up brings Hibernate 5 to the same result. (Hibernate 7 rejects a sort by a property of a collection with a PathException, which is part 12's subject.)
  • The note sits under === Grails 7.0.18, without a number, and the skill has the same entry.

This branch has not been deployed

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

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

3 participants