Skip to content

fix(hibernate7): query arguments reach criteria queries and a criteria get is unique - #17077

Open
borinquenkid wants to merge 4 commits into
8.0.xfrom
fix/h7-criteria-query-args-clean-8.0.x
Open

borinquenkid wants to merge 4 commits into
8.0.xfrom
fix/h7-criteria-query-args-clean-8.0.x

Conversation

@borinquenkid

Copy link
Copy Markdown
Member

Hibernate 7 part of #16823, rebased on 8.0.x on its own, with no new public API. The Hibernate 5 part (7.0.x) and @DelegatesTo (8.1.x) follow as separate pull requests.

Part of #16808

The bug

With Hibernate 7, readOnly, fetchSize, timeout and flushMode reach HQL queries but not criteria queries: where { }.list(readOnly: true), find, findAll(args) { }, finders with a map and withCriteria(args) all ignored them, and createCriteria().list(args) ignored them too. createCriteria().get { } returned the first of several matching rows instead of the unique result the reference describes (Hibernate 5 throws). A criteria call that threw left its session bound.

The change

  • HibernateQuery implements the existing QueryArgumentsAware, which DynamicFinder and DetachedCriteria already call; HibernateQuerySettings (package-private) parses the arguments for the criteria and HQL paths alike. A fetchSize or timeout that is not a whole number throws an IllegalArgumentException naming the argument.
  • A criteria get takes the rows from list() and throws NonUniqueResultException for several, null for none.
  • CriteriaMethodInvoker closes the session the builder opened when the call throws, and leaves a caller's session alone.

No public class or method is added: the settings record and the cleanup hook are package-private, and the criteria get uses the existing list().

What changes for applications

Arguments that criteria queries and finders ignored now apply; a criteria get that returned the first of several rows now throws (the note shows how to keep the first row with an order and maxResults(1)); a fetchSize or timeout that is not a whole number throws. The upgrade note is in the Grails 8.0.1 section, the grails-upgrade skill has an entry. This changes a result, so it needs two reviewers.

Tests

  • QueryArgumentsPropagationSpec (9 features, 6 fail when setArguments does nothing), CriteriaSessionCleanupSpec, HibernateQuerySettingsSpec, HibernateQueryExecutorSpec and CriteriaMethodInvokerSpec.
  • Example app (Hibernate 7): CriteriaQueryArgsScenarioSpec and CriteriaSingleResultScenarioSpec, on a domain class of their own (CriteriaItem). The scenarios that return one of several rows no longer assume an order.
  • The full grails-data-hibernate7-core suite passes.

https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

…a get is unique

readOnly, fetchSize, timeout and flushMode were dropped by criteria queries, where queries and dynamic finders. QueryArgumentsAware is implemented by HibernateQuery, a criteria get throws when several rows match, and a criteria call that throws closes the session it opened. No public API is added: the settings record and the cleanup hook are package-private.

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. A criteria get of one row can return a proxy that a cast used to accept.

if (rows.size() > 1) {
throw new NonUniqueResultException(rows.size());
}
result = rows.isEmpty() ? null : rows.get(0);

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 session already holds a proxy of the base class for the one matching subclass row. get used to unwrap that proxy. It now returns the proxy, so a cast to the subclass fails. Unwrap the one row before returning it.

@borinquenkid

Copy link
Copy Markdown
Member Author

@jamesfredley: the same finding you made on the Hibernate 5 pull request was open here. I ran every query setting through every paged route on Hibernate 7 (where, findAll with a closure, createCriteria().list, a grouped criteria, the static list), and with the arguments wired as this pull request does, flushMode: 'MANUAL' left the pending change out of the page, and reading totalCount still flushed it: the page held no row and the total said one. The count runs on a copy of the query, and the copy did not carry the flush mode or the timeout. It does now. QueryArgumentsPropagationSpec has four routes with MANUAL and ALWAYS, on a session that flushes by itself; the four MANUAL rows fail without the change. No count ran for update and lock, readOnly, timeout and fetchSize changed nothing else on any route. The whole grails-data-hibernate7-core module passes (4,716 tests).

https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

@codecov

codecov Bot commented Nov 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 62.5796%. Comparing base (b380a8f) to head (45c199c).
⚠️ Report is 29 commits behind head on 8.0.x.

Additional details and impacted files

Impacted file tree graph

@@               Coverage Diff                @@
##             8.0.x     #17077         +/-   ##
================================================
+ Coverage         0   62.5796%   +62.5796%     
- Complexity       0      27545      +27545     
================================================
  Files            0       2249       +2249     
  Lines            0     111725     +111725     
  Branches         0      20486      +20486     
================================================
+ Hits             0      69917      +69917     
- Misses           0      32450      +32450     
- Partials         0       9358       +9358     

see 2249 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@testlens-app

testlens-app Bot commented Nov 21, 2026

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: 45c199c
▶️ Tests: 111732 executed
⚪️ Checks: 99/99 completed


Learn more about TestLens at testlens.app/docs.

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