Repository navigation
Description
Follow-up design issue for 9.0.x. The bugs below were each fixed locally (#16814 and its PRs #16818 and #16820, plus #16816); the pattern behind them is what this issue is about.
What keeps going wrong
DetachedCriteria exposes its internal state as live, mutable lists, and every datastore reads and sometimes writes them:
getCriteria()andgetProjections()return the field lists themselves.- A deferred criteria (
whereLazy,buildLazy) is materialized byapplyLazyCriteria(), which nullslazyQueryand replays the closure into the same object. The first call mutates shared state, which is the thread race in Datastore: Thread-safe deferred criteria (whereLazy) materialization on copies #16814. - Nothing makes a reader materialize first. About ten readers (the H5 and H7 builders,
JpaQueryBuilder,DynamicFinder, the Mongo, Neo4j and SimpleMap queries) each have to remember, and a missed one silently drops the restriction. clone()is shallow: junctions (or,and,not) and the projection list are shared by reference, so a derived criteria can edit a node the original still holds.- The H7
PredicateGeneratoradds a default projection to the list it was handed, so even a read mutates the criteria. applyLazyCriteria()exists twice (AbstractDetachedCriteriaandgrails.gorm.DetachedCriteria).
Where it came from
The lazy state, the live-list getters and the shallow clone are inherited from the original grails-data-mapping code, integrated in April 2025. What we added on 8.0.x: the H7 translators that write to the lists (April 2026) and the H7 whereLazy override that routes where queries through deferred criteria (April 2026). That second change made a rarely used path the common one on Hibernate 7, which is why the bugs surfaced now.
Proposal (9.0.x, breaking)
- Expose criteria and projections as read-only views, or as an immutable snapshot taken at execution.
- Make laziness a value: materialize once into a snapshot under a lock and hand out copies, instead of replaying into the live object.
- Deep-copy junctions and associations in
clone()with one identity map. - Translators build their own projection list and never write to the criteria.
- One
applyLazyCriteria().
Questions
- Is a breaking change to
QueryableCriteria.getCriteria()/getProjections()acceptable in 9.0, or should the views be a new method with the old ones deprecated? - Should RxGORM follow in the same change?
Interim: #16820 stays bounded (recursive junction copy, H5 materialization, no new public API) so 8.0.x gets the fixes without this redesign.
Metadata
Metadata
Assignees
Type
Projects
- StatusShow more project fieldsNo status
Activity
borinquenkid commentedon Nov 5, 2026
Update from the fix of #16820 (df1a73a, 6d2431c). Doing it for real showed the design problem is wider than the lists: a criteria is a builder with two kinds of ways in, and each reviewer round fixed the case the last one tried. The in-place methods (the criterion DSL,
order,sort,join,select,setAlias,createAlias, the projection methods) and the clone-and-modify methods (max,offset,withConnection,buildLazy,where) each reach the state, and so do the getters and the translators in the H5 builder, the H7 handlers,JpaQueryBuilder,DynamicFinder, MongoDB, Neo4j and the in-memory datastore. Onlyadd,methodMissingandhandleJunctionapplied the deferred closure.What #16820 does for 8.0.x is bounded: one chokepoint (
applyLazyCriteria(), once, under a lock) called first by every method that touches state, queries read a copy, and a spec lists the ways in. That fixed the first-use race and the ordering, but it keeps the shape: mutable lists behind getters, a shallow-proneclone(), andDetachedProjectionssharing a list. The proposal here still stands for 9.0.x: an immutable builder in which every method returns a new criteria and the deferral is part of that immutable state, so there is no "first use" to get wrong.https://claude.ai/code/session_01Dmki4Tgxp7dLBhzpe2xT93