Skip to content

fix(hibernate5): a criteria's cache(false) is no longer overridden by the mapping in list(map) { } - #17014

Draft
borinquenkid wants to merge 6 commits into
7.0.xfrom
fix/h5-criteria-cache-option-7.0.x
Draft

borinquenkid wants to merge 6 commits into
7.0.xfrom
fix/h5-criteria-cache-option-7.0.x

Conversation

@borinquenkid

@borinquenkid borinquenkid commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

#17066, which this pull request waited for, has merged. It stays a draft while #17070 is open: #17070 moves a call that this pull request removes, so whichever of the two lands second drops it in its new place.

Entity.createCriteria().list([:]) { cache(false) } on an entity whose mapping enables the cache still put the query in the query cache. A list with a map builds a paged result list and populates it after the closure has run, and with no cache argument that re-applied the mapping's default over the cache(false) the closure had set (GrailsHibernateUtil.populateArgumentsForCriteria, as the issue says). The criteria already holds the mapping's default, set when it was created, so HibernateCriteriaBuilder.createPagedResultList now hands the criteria's own cacheable state on when the caller gave no cache argument. The cache argument still wins when it is there.

Spec: QueryCacheOptionSpec reads Hibernate's query cache statistics (puts) for 31 rows: what asks for caching (the mapping, the cache argument, the cache call in a criteria) x how the query is run (criteria list, list(map), count, static list, dynamic finder, where query) x the global grails.hibernate.cache.queries, plus the total count of a paged list for the same forms. Three rows fail without the change, all of the list(map) { cache(false) } shape.

#14458 (the total count ignores cache: false) does not reproduce: the total count puts nothing in the cache when the list is not cached, for criteria, static list and where queries, with and without the global switch. The rows pin it.

Left out: the total count of a paged list is not cached when its list is (it is a separate criteria that takes nothing from the first), and grails.hibernate.cache.queries does not make a criteria list cacheable on an uncached mapping; neither is what the two issues ask. Hibernate 7 has its own criteria builder; the same table goes on 8.0.x with the merge-up.

The caching guide says the cache argument and the cache call take precedence over the mapping, in either direction.

Full Hibernate 5 suite on this branch: 718 tests, 0 failures; codeStyle clean.

Fixes #14472
Refs #14458

https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

… the mapping in list(map) { }

list(map) { } builds a paged result list, and populating it re-applied the mapping's cache default over a cache(false) the closure had set. The criteria's own cacheable state is now handed on when the caller gave no cache argument.

Fixes #14472

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@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. cache(false) is still overridden when the arguments sort through an association whose mapping caches queries.

…ther the query is cached

A sort argument through an association (sort: 'author.name') created a sub-criteria and applied the cache mapping of the
associated entity to it. A sub-criteria shares the setting of its root, so this ran after the root had taken the cache
argument or the cache() call, and turned caching back on over an explicit false. The cache setting of a query now comes from
its own entity, the cache argument and the cache call only; the entity that is sorted through takes no part.

QueryCacheOptionSpec gains rows for a criteria list, a static list and a dynamic finder sorted through a cached association,
with cache(false), with cache: false, with cache(true) and with no setting, on a cached and an uncached mapping, and the total
count of a sorted criteria list with cache(false).

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
@borinquenkid

Copy link
Copy Markdown
Member Author

Thank you. That was a second way to the same override: a sort argument through an association (sort: 'author.name') created a sub-criteria and applied the cache mapping of the associated entity to it. A sub-criteria shares the setting of its root, so this ran after the root had taken the cache argument or the cache() call, and turned caching back on over an explicit false.

The cache setting of a query now comes from its own entity, the cache argument and the cache() call. The entity that is only sorted through takes no part. The same override was in the second sort helper (GrailsHibernateQueryUtils, used by the static list and the dynamic finders), so I removed it there too; the JPA-criteria sort path never had it.

One behaviour changes beyond your case, and I want it stated: before, sorting through a cached association could switch caching on for a root whose own mapping does not cache. It no longer does. A root that wants the cache says so through its mapping, the argument or cache(true).

QueryCacheOptionSpec has nine new rows with a cached Book and an uncached Note, both sorted through a cached author: a criteria list, a static list and a dynamic finder, with cache(false), with cache: false, with cache(true) and with no setting, and the total count of a sorted criteria list with cache(false). The rows fail without the fix (4 of 40) and pass with it, the Hibernate 5 module is green (727 tests), and codeStyle is clean.

@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. cache(false) stays off when the arguments sort through an association, and a criteria that does not set cache still uses the mapping.

@jdaugherty jdaugherty 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 fix does what #14472 asks: list(map) { cache(false) } is no longer put in the query cache, also when the closure calls setCacheable(false) directly, and lock: true still keeps the query out of the cache. #14458 does not reproduce on 7.0.x for me either.

Requested changes, details inline:

  • an upgrade guide section for the queries whose caching changes
  • test rows that reach the change in GrailsHibernateQueryUtils
  • two statements in caching.adoc that do not match the behaviour

Class<?> propertyTargetClass = ((Association) property).getAssociatedEntity().getJavaClass();
GrailsHibernateUtil.cacheCriteriaByMapping(datastore, propertyTargetClass, subCriteria);
// the cache setting of a query comes from its own entity, the cache argument and the cache call: the mapping of an
// entity that is only sorted through is no part of it, and a sub-criteria shares the setting of its root

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 PR changes what existing queries do, so it needs a section in the upgrade guide (grails-doc/src/en/guide/upgrading/upgrading60x.adoc, next free number is 22). Comparing this branch with 7.0.x:

Query 7.0.x This PR
createCriteria().list(map) { cache(false) } on an entity whose mapping caches cached not cached
entity whose mapping does not cache, createCriteria().list(max: 2, sort: 'author.name') { }, where author is an entity whose mapping caches cached not cached
the same entity with findWhere / findAllWhere / find(example, args) / findAll(example, args) and sort: 'author.name' cached not cached
createCriteria().list(max: 2, sort: 'author.name', lock: true) { } on an entity whose mapping caches cached not cached

The second and third rows are more than #14472 asks for: an application can be relying on those queries being cached today. Please list each change with what happened before, what happens now, and how to keep the old result (cache: true in the arguments).

Class<?> propertyTargetClass = associatedEntity.getJavaClass();
cacheCriteriaByMapping(propertyTargetClass, subCriteria);
// the cache setting of a query comes from its own entity, the cache argument and the cache call: the mapping of an
// entity that is only sorted through is no part of it, and a sub-criteria shares the setting of its root

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.

No row of QueryCacheOptionSpec reaches this change. This helper is only called for findWhere, findAllWhere, find(example, args) and findAll(example, args). The "static list" and "dynamic finder" sorted rows go through other code (list builds a JPA CriteriaQuery, the finder a HibernateQuery) and pass on 7.0.x as it is.

With this file reverted, QcBook.findAllWhere([title: 'b1'], [sort: 'author.name', cache: false]) is still put in the query cache, and so is QcNote.findAllWhere([title: 'b1'], [sort: 'author.name']). Please add rows of that shape so the change here is covered.

}
----

An entity whose mapping enables the cache has its queries cached by default. The `cache` argument and the `cache` call inside a criteria take precedence over that default, in either direction, so `cache false` (or `cache: false`) keeps one query out of the query cache, and the total count of a paged list follows it:

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.

Two statements in this sentence do not match what the code does:

  1. "An entity whose mapping enables the cache has its queries cached by default" is too broad. On such an entity, criteria queries, list, dynamic finders and where queries are cached by default, but count(), executeQuery('from ...') and findAll('from ...') put nothing in the query cache. Please name the kinds of query the default applies to.
  2. "the total count of a paged list follows it" holds only for false. When the list is cached, its total count is still not cached (a cached list(max: 2) followed by totalCount gives one put, not two), which the PR description lists under "Left out". Please say what happens instead: the total count is not put in the query cache. The class comment of QueryCacheOptionSpec ("the total count of a paged list follows the list") has the same wording.

It would also help to say here which one wins when both are given: list(max: 2, cache: true) { cache(false) } is cached, so the argument takes precedence over the call. A row in the spec for each direction would pin that.

…bernateQueryUtils, caching.adoc says what is cached, and the upgrade guide lists the queries that change

QueryCacheOptionSpec has rows for findAllWhere, findWhere, find(example, args) and findAll(example, args)
sorted through a cached association, a criteria list with lock and a sort, and the precedence of the
cache argument over cache() inside the closure. 13 rows fail without the change.

caching.adoc names the ways a query is cached (the mapping, cache: true, grails.hibernate.cache.queries),
says that an explicit false or true takes precedence in either direction and the argument wins over the
call, and that the total count of a paged list is not put in the query cache.

The upgrade guide has a section with a table of the queries that were cached against the cache option
or because of the entity they sort through, and how to keep the old result.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
@borinquenkid

Copy link
Copy Markdown
Member Author

The new commit covers the requested upgrade section, the spec rows and the two corrections to caching.adoc.

  • Upgrade guide. A new section of upgrading60x.adoc has a table of the queries that change: before, they were cached against the cache option or because of the entity they sort through; now they are not. Its heading is UPGRADE-NUMBER, to be replaced with the next free number when the pull request is merged, so that the sections of the pull requests that add one do not collide; the merge script refuses a diff that still has the marker. I built the table from the spec: 13 rows fail on the 7.0.x code and pass now, and each row of the table is one of them or a family of them.
  • Rows that reach GrailsHibernateQueryUtils. findAllWhere, findWhere, find(example, args) and findAll(example, args) with sort: 'author.name', with and without cache: false, on a cached and an uncached mapping, a criteria list with lock: true and a sort, and two rows for the precedence of the argument over cache() in the closure.
  • caching.adoc. It now names the three ways a query is cached (the mapping, cache: true, grails.hibernate.cache.queries), says that an explicit false or true takes precedence in either direction, that the argument wins over the call (list(max: 2, cache: true) { cache(false) } is cached), and that the count query behind totalCount is not put in the query cache, also when the list is. The class comment of QueryCacheOptionSpec says the same.

QueryCacheOptionSpec has 52 rows and passes, the Hibernate 5 module (739 tests) passes, and so does codeStyle.

If a shape does not reproduce first time, a failing spec on a throwaway branch, or the few lines you ran, would help me a lot.

@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 criteria closure that turns the cache off is ignored when the arguments turn it on.

protected List createPagedResultList(Map args) {
GrailsHibernateUtil.populateArgumentsForCriteria(datastore, targetClass, criteria, args, conversionService);
Map populateArgs = args;
if (!args.containsKey(GrailsHibernateUtil.ARGUMENT_CACHE)) {

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 arguments ask to cache the query, and the closure turns the cache off. The query is cached. The closure asked not to cache it. Keep the cache off.

@borinquenkid

Copy link
Copy Markdown
Member Author

@jamesfredley: list(max: 2, cache: true) { cache(false) } is not cached now, and list(max: 2, cache: false) { cache(true) } is. A cache call in the closure wins over the argument, in both directions: it runs last and speaks for this query alone. cache(boolean) and a direct setCacheable both count, including when what they ask for is the mapping's own default, which is why the builder now notes the call and no longer compares values. Without a cache call, the argument applies, then the mapping.

@jdaugherty: this reverses what the spec pinned after your review (the argument won). James asked for the closure to win; if you prefer the other direction, it is the two rows of the spec and one sentence of caching.adoc. Both are updated, and the guide's table has the two queries that change.

Before asking for another look, I ran every combination of the mapping (cached or not), the argument (none, true, false) and the closure (nothing, cache(true), cache(false), setCacheable(true), setCacheable(false)): 30 rows, each checked against the query cache's put count; 10 fail without this change.

https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

@borinquenkid
borinquenkid marked this pull request as draft November 23, 2026 00:07
# Conflicts:
#	grails-doc/src/en/guide/upgrading/upgrading60x.adoc
@borinquenkid borinquenkid added no ci and removed run ci labels Nov 26, 2026

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

#17070 (part 10 of the #16853 split) rewrites GrailsHibernateUtil.addOrderPossiblyNested: it walks the sort path itself and still calls cacheCriteriaByMapping(datastore, currentClass, criteria) for each association it joins for the sort (at e431f96). The call that this pull request removes from that method moves there, so whichever of the two lands second has to drop it in its new place too; otherwise Team.createCriteria().list(max: 10, sort: 'club.name') { cache(false) } is still made cacheable when the mapping of Club enables the cache. The call this pull request removes from GrailsHibernateQueryUtils.addOrderPossiblyNested is still in place in #17071 and #17072, but #17072 reworks the code around it.

…e-option-7.0.x

# Conflicts:
#	grails-data-hibernate5/core/src/main/groovy/org/grails/orm/hibernate/query/GrailsHibernateQueryUtils.java
#	grails-doc/src/en/guide/upgrading/upgrading60x.adoc
@testlens-app

testlens-app Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

🔎 No tests executed 🔎

🏷️ Commit: 79b3d19
▶️ Tests: 0 executed
⚪️ Checks: 30/30 completed


Learn more about TestLens at testlens.app/docs.

@borinquenkid

Copy link
Copy Markdown
Member Author

@jdaugherty Agreed. #17066, which this waited for, has merged, and the description now says so. This stays a draft while #17070 is open. If it lands second, I will remove the cacheCriteriaByMapping call in its new place too, with a spec row for Team.createCriteria().list(max: 10, sort: 'club.name') { cache(false) }.

https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

@jdaugherty jdaugherty mentioned this pull request Dec 1, 2026
9 of 17 tasks

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

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

4 participants