Repository navigation
Keep the root alias of a detached criteria in the count of a paged list - #17063
Conversation
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
left a comment
There was a problem hiding this comment.
Requested changes. The total of an unaliased page changes when the sort joins an association the call asked to keep.
matrei
left a comment
There was a problem hiding this comment.
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
- 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.
- 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', …)throwsCannot use comparison criteria on non-existent property [t.name], and withsort: 't.name'the list throws aNullPointerException. 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: tand 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
left a comment
There was a problem hiding this comment.
The items this review found have been addressed. The count keeps the root alias, and a sort map joins each association once.
matrei
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
This is an AI assisted review
LGTM. One-line fix, covered by the new test.
sbglasius
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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)); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
sbglasius
left a comment
There was a problem hiding this comment.
This is an AI assisted review
✅ All tests passed ✅🏷️ Commit: 42001ad Learn more about TestLens at testlens.app/docs. |
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:
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
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