Skip to content

Mark to-one associations read-only after a validation error - #16390

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

jdaugherty merged 1 commit into
7.0.xfrom
fix/hibernate-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 5 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.

The Groovy 6 upgrade experiment (#16387) found this: Groovy 6 rejects !x instanceof T. The fix does not depend on Groovy 6, so it is submitted separately here.

Changes

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

Testing

New ValidationErrorToOneReadOnlySpec in grails-data-hibernate5-core:

  • A saved author gets an unsaved rename, then a book referencing it fails validation. The author is read-only in the session, and after an explicit flush and reload its name is unchanged. This feature fails without the fix.
  • When the author is an unloaded proxy, the failed save does not load it.

:grails-data-hibernate5-core:test passes (601 tests, 0 failures) and so does ./gradlew codeStyle.

Follow-up

On 8.0.x the Hibernate 5 copy is already fixed, so the merge-up only brings this spec along. The same check in the Hibernate 7 HibernateGormInstanceApi needs its own PR against 8.0.x.

`!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.
@testlens-app

testlens-app Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

✅ All tests passed ✅

⚠️ TestLens detected flakiness ⚠️

Test Summary

CI / Build Grails Forge (17) > :grails-forge-cli:test [grails-forge]

Test Runs Flakiness
CreateControllerCommandSpec > test app with controller ❌ ❌ ✅ 1% 🟡

🏷️ Commit: 9416da6
▶️ Tests: 9560 executed
⚪️ Checks: 51/51 completed

Test Failures

CreateControllerCommandSpec > test app with controller (:grails-forge-cli:test [grails-forge] in CI / Build Grails Forge (17) | Attempt 2/3)
Condition not satisfied after 240.00 seconds and 238 attempts
	at spock.util.concurrent.PollingConditions.within(PollingConditions.java:185)
	at spock.util.concurrent.PollingConditions.eventually(PollingConditions.java:140)
	at org.grails.forge.cli.CommandSpec.testOutputContains(CommandSpec.groovy:64)
	at org.grails.forge.cli.command.CreateControllerCommandSpec.test app with controller(CreateControllerCommandSpec.groovy:73)
Caused by: Condition not satisfied:

output.toString().contains(value)
|      |          |        |
|      |          false    BUILD SUCCESSFUL
|      To honour the JVM settings for this build a single-use Daemon process will be forked. For more on this, please refer to https://docs.gradle.org/8.14.5/userguide/gradle_daemon.html#sec:disabling_the_daemon in the Gradle documentation.
|      Daemon will be stopped at the end of the build 
|       
|      > Task :assetCompile
|      Starting AssetCompiler [-i, /tmp/grailsforgetmp18142131554244541920/grails-app/assets, -o, /tmp/grailsforgetmp18142131554244541920/build/assets, -j, /home/runner/.gradle/caches/modules-2/files-2.1/org.webjars.npm/bootstrap/5.3.8/1a2f960f569cba04bce1c79ff07ab8985a0f56a0/bootstrap-5.3.8.jar, -j, /home/runner/.gradle/caches/modules-2/files-2.1/org.webjars.npm/bootstrap-icons/1.13.1/add743da3f8acf43906ece5d5fb8b759d2b0eeed/bootstrap-icons-1.13.1.jar, -j, /home/runner/.gradle/caches/modules-2/files-2.1/org.webjars.npm/jquery/3.7.1/abca7c4ab5c6ff5f533f5d7dbc646661c36c8b6b/jquery-3.7.1.jar, -B, eyJjb25maWdPcHRpb25zIjp7fSwiZW5hYmxlRGlnZXN0cyI6dHJ1ZSwiZW5hYmxlR3ppcCI6dHJ1ZSwiZW5hYmxlU291cmNlTWFwcyI6dHJ1ZSwiZXhjbHVkZXNHemlwIjpbXSwibWF4VGhyZWFkcyI6bnVsbCwibWluaWZ5Q3NzIjp0cnVlLCJtaW5pZnlKcyI6dHJ1ZSwic2tpcE5vbkRpZ2VzdHMiOnRydWUsIm1pbmlmeU9wdGlvbnMiOnt9LCJ2ZXJib3NlIjp0cnVlLCJleGNsdWRlcyI6WyJ3ZWJqYXJzL2pxdWVyeS8qKiIsIndlYmphcnMvYm9vdHN0cmFwLyoqIiwid2ViamFycy9ib290c3RyYXAtaWNvbnMvKioiXSwiaW5jbHVkZXMiOlsid2ViamFycy9qcXVlcnkvKi9kaXN0L2pxdWVyeS5qcyIsIndlYmphcnMvYm9vdHN0cmFwLyovZGlzdC9qcy9ib290c3RyYXAuYnVuZGxlLmpzIiwid2ViamFycy9ib290c3RyYXAvKi9kaXN0L2Nzcy9ib290c3RyYXAuY3NzIiwid2ViamFycy9ib290c3RyYXAtaWNvbnMvKi9mb250L2Jvb3RzdHJhcC1pY29ucy5jc3MiLCJ3ZWJqYXJzL2Jvb3RzdHJhcC1pY29ucy8qL2ZvbnQvZm9udHMvKiJdLCJyZXNvbHZlcnMiOltdLCJhc3NldHNQYXRoIjoiL3RtcC9ncmFpbHNmb3JnZXRtcDE4MTQyMTMxNTU0MjQ0NTQxOTIwL2dyYWlscy1hcHAvYXNzZXRzIiwiY2FjaGVMb2NhdGlvbiI6Ii90bXAvZ3JhaWxzZm9yZ2V0bXAxODE0MjEzMTU1NDI0NDU0MTkyMC9idWlsZC8uYXNzZXRjYWNoZSJ9]
|      10:35:50.341 [main] INFO asset.pipeline.LoggingAssetEventListener -- Finished Precompiling Assets
|       
|      > Task :compileJava NO-SOURCE
|      > Task :compileGroovy
|      > Task :buildProperties
|      > Task :processResources
|      > Task :classes
|      > Task :compileWebappGroovyPages NO-SOURCE
|      > Task :compileGroovyPages
|      > Task :findMainClass
|      > Task :bootWar
|      > Task :war
|      > Task :assemble
|      > Task :compileTestJava NO-SOURCE
|      > Task :compileTestGroovy
|      > Task :processTestResources NO-SOURCE
|      > Task :testClasses
|      > Task :test
|      > Task :compileIntegrationTestJava NO-SOURCE
|      > Task :compileIntegrationTestGroovy
|      > Task :processIntegrationTestResources
|      > Task :integrationTestClasses
|      > Task :integrationTest
To honour the JVM settings for this build a single-use Daemon process will be forked. For more on this, please refer to https://docs.gradle.org/8.14.5/userguide/gradle_daemon.html#sec:disabling_the_daemon in the Gradle documentation.
Daemon will be stopped at the end of the build 
 
> Task :assetCompile
Starting AssetCompiler [-i, /tmp/grailsforgetmp18142131554244541920/grails-app/assets, -o, /tmp/grailsforgetmp18142131554244541920/build/assets, -j, /home/runner/.gradle/caches/modules-2/files-2.1/org.webjars.npm/bootstrap/5.3.8/1a2f960f569cba04bce1c79ff07ab8985a0f56a0/bootstrap-5.3.8.jar, -j, /home/runner/.gradle/caches/modules-2/files-2.1/org.webjars.npm/bootstrap-icons/1.13.1/add743da3f8acf43906ece5d5fb8b759d2b0eeed/bootstrap-icons-1.13.1.jar, -j, /home/runner/.gradle/caches/modules-2/files-2.1/org.webjars.npm/jquery/3.7.1/abca7c4ab5c6ff5f533f5d7dbc646661c36c8b6b/jquery-3.7.1.jar, -B, eyJjb25maWdPcHRpb25zIjp7fSwiZW5hYmxlRGlnZXN0cyI6dHJ1ZSwiZW5hYmxlR3ppcCI6dHJ1ZSwiZW5hYmxlU291cmNlTWFwcyI6dHJ1ZSwiZXhjbHVkZXNHemlwIjpbXSwibWF4VGhyZWFkcyI6bnVsbCwibWluaWZ5Q3NzIjp0cnVlLCJtaW5pZnlKcyI6dHJ1ZSwic2tpcE5vbkRpZ2VzdHMiOnRydWUsIm1pbmlmeU9wdGlvbnMiOnt9LCJ2ZXJib3NlIjp0cnVlLCJleGNsdWRlcyI6WyJ3ZWJqYXJzL2pxdWVyeS8qKiIsIndlYmphcnMvYm9vdHN0cmFwLyoqIiwid2ViamFycy9ib290c3RyYXAtaWNvbnMvKioiXSwiaW5jbHVkZXMiOlsid2ViamFycy9qcXVlcnkvKi9kaXN0L2pxdWVyeS5qcyIsIndlYmphcnMvYm9vdHN0cmFwLyovZGlzdC9qcy9ib290c3RyYXAuYnVuZGxlLmpzIiwid2ViamFycy9ib290c3RyYXAvKi9kaXN0L2Nzcy9ib290c3RyYXAuY3NzIiwid2ViamFycy9ib290c3RyYXAtaWNvbnMvKi9mb250L2Jvb3RzdHJhcC1pY29ucy5jc3MiLCJ3ZWJqYXJzL2Jvb3RzdHJhcC1pY29ucy8qL2ZvbnQvZm9udHMvKiJdLCJyZXNvbHZlcnMiOltdLCJhc3NldHNQYXRoIjoiL3RtcC9ncmFpbHNmb3JnZXRtcDE4MTQyMTMxNTU0MjQ0NTQxOTIwL2dyYWlscy1hcHAvYXNzZXRzIiwiY2FjaGVMb2NhdGlvbiI6Ii90bXAvZ3JhaWxzZm9yZ2V0bXAxODE0MjEzMTU1NDI0NDU0MTkyMC9idWlsZC8uYXNzZXRjYWNoZSJ9]
10:35:50.341 [main] INFO asset.pipeline.LoggingAssetEventListener -- Finished Precompiling Assets
 
> Task :compileJava NO-SOURCE
> Task :compileGroovy
> Task :buildProperties
> Task :processResources
> Task :classes
> Task :compileWebappGroovyPages NO-SOURCE
> Task :compileGroovyPages
> Task :findMainClass
> Task :bootWar
> Task :war
> Task :assemble
> Task :compileTestJava NO-SOURCE
> Task :compileTestGroovy
> Task :processTestResources NO-SOURCE
> Task :testClasses
> Task :test
> Task :compileIntegrationTestJava NO-SOURCE
> Task :compileIntegrationTestGroovy
> Task :processIntegrationTestResources
> Task :integrationTestClasses
> Task :integrationTest

	at org.grails.forge.cli.CommandSpec.testOutputContains_closure1(CommandSpec.groovy:65)
	at org.grails.forge.cli.CommandSpec.testOutputContains_closure1(CommandSpec.groovy)
	at groovy.lang.Closure.call(Closure.java:427)
	at spock.util.concurrent.PollingConditions.within(PollingConditions.java:168)
	... 3 more
CreateControllerCommandSpec > test app with controller (:grails-forge-cli:test [grails-forge] in CI / Build Grails Forge (17) | Attempt 1/3)
Condition not satisfied after 240.00 seconds and 238 attempts
	at spock.util.concurrent.PollingConditions.within(PollingConditions.java:185)
	at spock.util.concurrent.PollingConditions.eventually(PollingConditions.java:140)
	at org.grails.forge.cli.CommandSpec.testOutputContains(CommandSpec.groovy:64)
	at org.grails.forge.cli.command.CreateControllerCommandSpec.test app with controller(CreateControllerCommandSpec.groovy:73)
Caused by: Condition not satisfied:

output.toString().contains(value)
|      |          |        |
|      |          false    BUILD SUCCESSFUL
|      To honour the JVM settings for this build a single-use Daemon process will be forked. For more on this, please refer to https://docs.gradle.org/8.14.5/userguide/gradle_daemon.html#sec:disabling_the_daemon in the Gradle documentation.
|      Daemon will be stopped at the end of the build 
|       
|      > Task :assetCompile
|      Starting AssetCompiler [-i, /tmp/grailsforgetmp12148135985937377977/grails-app/assets, -o, /tmp/grailsforgetmp12148135985937377977/build/assets, -j, /home/runner/.gradle/caches/modules-2/files-2.1/org.webjars.npm/bootstrap/5.3.8/1a2f960f569cba04bce1c79ff07ab8985a0f56a0/bootstrap-5.3.8.jar, -j, /home/runner/.gradle/caches/modules-2/files-2.1/org.webjars.npm/bootstrap-icons/1.13.1/add743da3f8acf43906ece5d5fb8b759d2b0eeed/bootstrap-icons-1.13.1.jar, -j, /home/runner/.gradle/caches/modules-2/files-2.1/org.webjars.npm/jquery/3.7.1/abca7c4ab5c6ff5f533f5d7dbc646661c36c8b6b/jquery-3.7.1.jar, -B, eyJjb25maWdPcHRpb25zIjp7fSwiZW5hYmxlRGlnZXN0cyI6dHJ1ZSwiZW5hYmxlR3ppcCI6dHJ1ZSwiZW5hYmxlU291cmNlTWFwcyI6dHJ1ZSwiZXhjbHVkZXNHemlwIjpbXSwibWF4VGhyZWFkcyI6bnVsbCwibWluaWZ5Q3NzIjp0cnVlLCJtaW5pZnlKcyI6dHJ1ZSwic2tpcE5vbkRpZ2VzdHMiOnRydWUsIm1pbmlmeU9wdGlvbnMiOnt9LCJ2ZXJib3NlIjp0cnVlLCJleGNsdWRlcyI6WyJ3ZWJqYXJzL2pxdWVyeS8qKiIsIndlYmphcnMvYm9vdHN0cmFwLyoqIiwid2ViamFycy9ib290c3RyYXAtaWNvbnMvKioiXSwiaW5jbHVkZXMiOlsid2ViamFycy9qcXVlcnkvKi9kaXN0L2pxdWVyeS5qcyIsIndlYmphcnMvYm9vdHN0cmFwLyovZGlzdC9qcy9ib290c3RyYXAuYnVuZGxlLmpzIiwid2ViamFycy9ib290c3RyYXAvKi9kaXN0L2Nzcy9ib290c3RyYXAuY3NzIiwid2ViamFycy9ib290c3RyYXAtaWNvbnMvKi9mb250L2Jvb3RzdHJhcC1pY29ucy5jc3MiLCJ3ZWJqYXJzL2Jvb3RzdHJhcC1pY29ucy8qL2ZvbnQvZm9udHMvKiJdLCJyZXNvbHZlcnMiOltdLCJhc3NldHNQYXRoIjoiL3RtcC9ncmFpbHNmb3JnZXRtcDEyMTQ4MTM1OTg1OTM3Mzc3OTc3L2dyYWlscy1hcHAvYXNzZXRzIiwiY2FjaGVMb2NhdGlvbiI6Ii90bXAvZ3JhaWxzZm9yZ2V0bXAxMjE0ODEzNTk4NTkzNzM3Nzk3Ny9idWlsZC8uYXNzZXRjYWNoZSJ9]
|      08:50:31.663 [main] INFO asset.pipeline.LoggingAssetEventListener -- Finished Precompiling Assets
|       
|      > Task :compileJava NO-SOURCE
|      > Task :compileGroovy
|      > Task :buildProperties
|      > Task :processResources
|      > Task :classes
|      > Task :compileWebappGroovyPages NO-SOURCE
|      > Task :compileGroovyPages
|      > Task :findMainClass
|      > Task :bootWar
|      > Task :war
|      > Task :assemble
|      > Task :compileTestJava NO-SOURCE
|      > Task :compileTestGroovy
|      > Task :processTestResources NO-SOURCE
|      > Task :testClasses
|      > Task :test
|      > Task :compileIntegrationTestJava NO-SOURCE
|      > Task :compileIntegrationTestGroovy
|      > Task :processIntegrationTestResources
|      > Task :integrationTestClasses
|      > Task :integrationTest
To honour the JVM settings for this build a single-use Daemon process will be forked. For more on this, please refer to https://docs.gradle.org/8.14.5/userguide/gradle_daemon.html#sec:disabling_the_daemon in the Gradle documentation.
Daemon will be stopped at the end of the build 
 
> Task :assetCompile
Starting AssetCompiler [-i, /tmp/grailsforgetmp12148135985937377977/grails-app/assets, -o, /tmp/grailsforgetmp12148135985937377977/build/assets, -j, /home/runner/.gradle/caches/modules-2/files-2.1/org.webjars.npm/bootstrap/5.3.8/1a2f960f569cba04bce1c79ff07ab8985a0f56a0/bootstrap-5.3.8.jar, -j, /home/runner/.gradle/caches/modules-2/files-2.1/org.webjars.npm/bootstrap-icons/1.13.1/add743da3f8acf43906ece5d5fb8b759d2b0eeed/bootstrap-icons-1.13.1.jar, -j, /home/runner/.gradle/caches/modules-2/files-2.1/org.webjars.npm/jquery/3.7.1/abca7c4ab5c6ff5f533f5d7dbc646661c36c8b6b/jquery-3.7.1.jar, -B, eyJjb25maWdPcHRpb25zIjp7fSwiZW5hYmxlRGlnZXN0cyI6dHJ1ZSwiZW5hYmxlR3ppcCI6dHJ1ZSwiZW5hYmxlU291cmNlTWFwcyI6dHJ1ZSwiZXhjbHVkZXNHemlwIjpbXSwibWF4VGhyZWFkcyI6bnVsbCwibWluaWZ5Q3NzIjp0cnVlLCJtaW5pZnlKcyI6dHJ1ZSwic2tpcE5vbkRpZ2VzdHMiOnRydWUsIm1pbmlmeU9wdGlvbnMiOnt9LCJ2ZXJib3NlIjp0cnVlLCJleGNsdWRlcyI6WyJ3ZWJqYXJzL2pxdWVyeS8qKiIsIndlYmphcnMvYm9vdHN0cmFwLyoqIiwid2ViamFycy9ib290c3RyYXAtaWNvbnMvKioiXSwiaW5jbHVkZXMiOlsid2ViamFycy9qcXVlcnkvKi9kaXN0L2pxdWVyeS5qcyIsIndlYmphcnMvYm9vdHN0cmFwLyovZGlzdC9qcy9ib290c3RyYXAuYnVuZGxlLmpzIiwid2ViamFycy9ib290c3RyYXAvKi9kaXN0L2Nzcy9ib290c3RyYXAuY3NzIiwid2ViamFycy9ib290c3RyYXAtaWNvbnMvKi9mb250L2Jvb3RzdHJhcC1pY29ucy5jc3MiLCJ3ZWJqYXJzL2Jvb3RzdHJhcC1pY29ucy8qL2ZvbnQvZm9udHMvKiJdLCJyZXNvbHZlcnMiOltdLCJhc3NldHNQYXRoIjoiL3RtcC9ncmFpbHNmb3JnZXRtcDEyMTQ4MTM1OTg1OTM3Mzc3OTc3L2dyYWlscy1hcHAvYXNzZXRzIiwiY2FjaGVMb2NhdGlvbiI6Ii90bXAvZ3JhaWxzZm9yZ2V0bXAxMjE0ODEzNTk4NTkzNzM3Nzk3Ny9idWlsZC8uYXNzZXRjYWNoZSJ9]
08:50:31.663 [main] INFO asset.pipeline.LoggingAssetEventListener -- Finished Precompiling Assets
 
> Task :compileJava NO-SOURCE
> Task :compileGroovy
> Task :buildProperties
> Task :processResources
> Task :classes
> Task :compileWebappGroovyPages NO-SOURCE
> Task :compileGroovyPages
> Task :findMainClass
> Task :bootWar
> Task :war
> Task :assemble
> Task :compileTestJava NO-SOURCE
> Task :compileTestGroovy
> Task :processTestResources NO-SOURCE
> Task :testClasses
> Task :test
> Task :compileIntegrationTestJava NO-SOURCE
> Task :compileIntegrationTestGroovy
> Task :processIntegrationTestResources
> Task :integrationTestClasses
> Task :integrationTest

	at org.grails.forge.cli.CommandSpec.testOutputContains_closure1(CommandSpec.groovy:65)
	at org.grails.forge.cli.CommandSpec.testOutputContains_closure1(CommandSpec.groovy)
	at groovy.lang.Closure.call(Closure.java:427)
	at spock.util.concurrent.PollingConditions.within(PollingConditions.java:168)
	... 3 more

Learn more about TestLens at testlens.app/docs.

@jdaugherty
jdaugherty merged commit dd6e9da into 7.0.x Sep 24, 2026
136 of 138 checks passed
@jdaugherty
jdaugherty deleted the fix/hibernate-to-one-read-only-on-validation-error branch September 24, 2026 14:19
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