Skip to content

Keep the root alias of a detached criteria in the count of a paged list - #17063

Merged
jdaugherty merged 8 commits into
apache:7.0.xfrom
jdaugherty:fix/16853-03-paged-count-root-alias
Nov 25, 2026
Merged

jdaugherty merged 8 commits into
apache:7.0.xfrom
jdaugherty:fix/16853-03-paged-count-root-alias

Conversation

@jdaugherty

@jdaugherty jdaugherty commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Description

Part 3 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 #17062. Until that merges, this pull request also shows the commits below it. Review the changes introduced by this part, including its review fixes, relative to #17062.

A paged query of a detached criteria that has an alias for the queried
class, and criteria that use it, failed when totalCount was read:

new DetachedCriteria(Team, 't').eq('t.name', 'Arsenal').list(max: 2).totalCount

The count runs on a copy of the criteria, and the copy was created
without the alias, so Hibernate could not resolve t.name. The copy now
has the alias of the original.

This fixes a paged detached-criteria query that failed when its criteria used the root alias. It does not change successful unaliased queries.

The separate Hibernate 7 root-alias restriction and sort counterpart is tracked by #17167, together with the finding on #17073. It does not block this Hibernate 5 count-copy fix.

Verification

The full Hibernate 5 core suite passes after merging the reviewed parent branch (778 cases, zero failures, 28 skipped). Full codeStyle, RAT and the guide build pass. 6d91d1d carries the parent fixes and resolves the release-branch conflicts.

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.

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.
A paged query of a detached criteria that has an alias for the queried
class, and criteria that use it, failed when totalCount was read:

    new DetachedCriteria(Team, 't').eq('t.name', 'Arsenal').list(max: 2).totalCount

The count runs on a copy of the criteria, and the copy was created
without the alias, so Hibernate could not resolve t.name. The copy now
has the alias of the original.

Part 3 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. The total of an unaliased page changes when the sort joins an association the call asked to keep.

@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 62bf8ed (base 7.0.x), the last commit. I probed paged lists and their totalCount at 7.0.x, at part 2 (279520e) and here, with a team without a club among three that match.

The fix is right. Without an alias, CriteriaImpl holds Hibernate's root alias this, which createCriteria(entityName) uses anyway, so the copy only changes for a query through an alias of the queried class:

new DetachedCriteria(Team, 't') 7.0.x and part 2 This head
.eq('t.name', 'Arsenal First Team').list(max: 2) throws could not resolve property: t 1 row, totalCount 1
.like('t.name', '%First%').list(max: 2, sort: 't.name') throws 2 rows, totalCount 3
.like('t.name', '%First%').join('club', JoinType.LEFT).list(max: 10, sort: 'club.name') throws 3 rows, totalCount 3
.like('name', '%First%').list(max: 2), alias unused 2 rows, totalCount 3 the same

The where queries and the detached criteria without an alias that I probed give the same rows and totals at part 2 and here.

Needs a fix

1. The description promises a note that isn't there. It says the upgrade guide has a note under Grails 7.0.18 and the upgrade skill the same entry, but the commit has neither. By #17061, a fix that makes a failing call work is a bug fix like any other, so it doesn't need them. Please drop that sentence and the "Results that change" section. The section also says the count "did not match the list", but I found no case where the count ran and differed from the list, at 7.0.x or here. If you have one, the note is needed after all.

2. Add run ci, as on #17062: the test jobs were skipped.

Notes

  1. On James's comment: the total of Team.where { name ==~ '%First%' }.join('club', JoinType.LEFT).list(max: 10, sort: 'club.name') does go from 2 to 3. The list goes from 2 rows to 3 with it, though, so the count agrees with the list. Both change in part 1 (#16853), not in this commit:
7.0.x #16853 Part 2 This head
list 2 3 3 3
totalCount 2 3 3 3

Whether the list should keep that team is a question for #16853, which I've answered there: the change stays, with a note under 7.0.18.

  1. With Hibernate 7 on 8.0.x (0518f48), criteria through the root alias of a detached criteria fail before any count runs: eq('t.name', …) throws Cannot use comparison criteria on non-existent property [t.name], and with sort: 't.name' the list throws a NullPointerException. After the merge-up the call works with Hibernate 5 only, which is no reason to hold this. Hibernate 7 needs an issue of its own, together with part 13's sort through the root alias.

Verified as correct

  • The new feature fails at part 2 with could not resolve property: t and passes here.

Keep join lookup private to its only caller. Document changed page contents and collection duplicates, and cover sorting before pagination. Preserve both sets of upgrade notes when merging the release branch.

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

The items this review found have been addressed. The count keeps the root alias, and a sort map joins each association once.

matrei
matrei previously approved these changes Nov 23, 2026

@matrei matrei left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed again at bb5befa. The merge only brings in #17062, so the change is still 62bf8ed. Both points from my first review are fixed:

  • The description no longer promises a note, and the "Results that change" section is gone.
  • The pull request has run ci, and CI passed with the tests.

I ran my probes again here and with #17062's sources (fdc3ec9). Only the three queries that use the alias t change: they threw could not resolve property: t and now return their rows, with a totalCount that matches the list. The other seven, where queries and detached criteria without the alias among them, give the same rows and totals on both. The new feature fails with #17062's sources and passes here.

This change only makes a failing call work, so one approval is enough.

On Hibernate 7: criteria through the root alias of a detached criteria still fail there before any count runs. The issue for that, together with the sort through the root alias of #17073, isn't open yet. That's no reason to hold this one.

sbglasius
sbglasius previously approved these changes Nov 24, 2026

@sbglasius sbglasius left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an AI assisted review

LGTM. One-line fix, covered by the new test.

@github-actions
github-actions Bot dismissed stale reviews from matrei and sbglasius November 25, 2026 03:11

Dismissed automatically: the pull request changed after this approval, from bb5befa to 6d91d1d.

sbglasius
sbglasius previously approved these changes Nov 25, 2026

@sbglasius sbglasius left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an AI assisted review

Only change since my last review is the explicit expected rows in the multi-property sort test. LGTM.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requested changes. A left join the call asked for is undone by a sort, so a team with no club is left out.

for (Object o : fetch.keySet()) {
String associationName = (String) o;

final FetchMode fetchMode = getFetchMode(fetch.get(associationName));

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.

Team.list(sort: 'club.name', fetch: [club: JoinType.LEFT]) should include Unattached First Team, whose club is empty. It is left out. The left join is not made, and the sort joins club as an inner join. Keep the left join.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This static Team.list case is real at this head, but it is pre-existing behavior and outside the scope of this PR. Relative to #17062, #17063 only preserves the root alias when copying detached criteria for totalCount; this call does not exercise that change.

Applying the requested fetch join type before a static-list sort belongs to #17071. It is fixed there, with a regression covering this exact LEFT-fetch/sort combination, which passes in both compatibility modes. Keeping the fix in #17071 preserves the agreed split of the stack.

matrei
matrei previously approved these changes Nov 25, 2026

@matrei matrei left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed again at 6d91d1d. Since my approval, the only change is the merge of 2020942 from #17062: the page assertion in AssociationSortSpec, one line (git diff bb5befaba8 6d91d1d0d8). CI passed with the tests.

@github-actions
github-actions Bot dismissed stale reviews from sbglasius and matrei November 25, 2026 16:32

Dismissed automatically: the pull request changed after this approval, from 6d91d1d to 42001ad.

@sbglasius sbglasius left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an AI assisted review

@testlens-app

testlens-app Bot commented Nov 25, 2026

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: 42001ad
▶️ Tests: 59489 executed
⚪️ Checks: 58/58 completed


Learn more about TestLens at testlens.app/docs.

@jdaugherty
jdaugherty merged commit 2e02ab8 into apache:7.0.x Nov 25, 2026
59 checks passed
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