Skip to content

Mark to-one associations read-only after a validation error in Hibernate 7 - #16391

Merged
jdaugherty merged 1 commit into
8.0.xfrom
fix/hibernate7-to-one-read-only-on-validation-error
Sep 24, 2026
Merged

jdaugherty merged 1 commit into
8.0.xfrom
fix/hibernate7-to-one-read-only-on-validation-error

Conversation

@matrei

@matrei matrei commented Sep 24, 2026

Copy link
Copy Markdown
Contributor

Summary

When save() fails validation, the Hibernate 7 HibernateGormInstanceApi.handleValidationError is meant to mark the invalid instance and its to-one associations read-only, so a later flush in the same session cannot write them. The association check was written as

if (association instanceof ToOne && !association instanceof Embedded) {

which Groovy parses as (!association) instanceof Embedded. That is always false, so only the instance itself was ever marked read-only. Unsaved changes on an associated object could still be flushed after the failed save.

This is the Hibernate 7 counterpart of #16390 (7.0.x, Hibernate 5). The Hibernate 5 copy on 8.0.x was already fixed in 153e14c. The Groovy 6 upgrade experiment (#16387) found the bug, since Groovy 6 rejects !x instanceof T, but the fix does not depend on Groovy 6.

Changes

  • HibernateGormInstanceApi.handleValidationError uses !(association instanceof Embedded), so non-embedded to-one associations are marked read-only as intended. Associations that are still unloaded proxies are left alone, as before.

Testing

HibernateGormInstanceApiSpec:

  • The existing feature "handleValidationError sets association to read-only" only checked that the save failed, so it passed with the bug. It now gives a saved author an unsaved rename, fails validation on a book referencing it, and checks that the author is read-only and that an explicit flush and reload leave its name unchanged. It fails without the fix.
  • New feature: when the author is an unloaded proxy, the failed save does not load it.

./gradlew clean aggregateViolations :grails-test-report:check --continue reports no Checkstyle, CodeNarc, PMD or SpotBugs violations. All Hibernate 7 module tests pass (3418 tests). The only failure in the full run was a 30-second fixture timeout in grails-shell-cli's SpringApplicationRunnerSpec, which passes when rerun on its own.

…ate 7

`!association instanceof Embedded` parsed as `(!association) instanceof
Embedded`, which is always false, so a failed save never marked the
invalid instance's to-one associations read-only and their pending
changes could still be flushed.
@codecov

codecov Bot commented Sep 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 57.4413%. Comparing base (7ea4cb9) to head (dc5fe4f).

Additional details and impacted files

Impacted file tree graph

@@                Coverage Diff                 @@
##                8.0.x     #16391        +/-   ##
==================================================
- Coverage     57.4441%   57.4413%   -0.0029%     
+ Complexity      22558      22555         -3     
==================================================
  Files            2129       2129                
  Lines          103840     103840                
  Branches        18646      18646                
==================================================
- Hits            59650      59647         -3     
- Misses          35929      35934         +5     
+ Partials         8261       8259         -2     

see 4 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.

@matrei matrei mentioned this pull request Sep 24, 2026
@testlens-app

testlens-app Bot commented Sep 24, 2026

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: dc5fe4f
▶️ Tests: 82189 executed
⚪️ Checks: 82/82 completed


Learn more about TestLens at testlens.app/docs.

@jdaugherty
jdaugherty merged commit 157195f into 8.0.x Sep 24, 2026
85 checks passed
@jdaugherty
jdaugherty deleted the fix/hibernate7-to-one-read-only-on-validation-error branch September 24, 2026 13:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants