borinquenkid opened a new pull request, #16651: URL: https://github.com/apache/grails-core/pull/16651
Fixes #14545 ## What `validate()` switches the Hibernate session to `FlushMode.MANUAL` while it runs the constraints. The `finally` block restored the previous flush mode only `if (!errors.hasErrors())`, so after a **failed** `validate()` the session stayed in MANUAL. A surrounding `@Transactional` / `withTransaction` then never flushed on commit, and saves of unrelated valid objects were silently lost. ## Change The previous flush mode is now restored unconditionally in `finally`, in both the Hibernate 7 module (`HibernateGormValidationApi`) and the Hibernate 5 module (`AbstractHibernateGormValidationApi`). This also restores the mode when a validator throws. ## Tests * Hibernate 7 (`HibernateGormValidationApiSpec`, 5 new features) and Hibernate 5 (`ValidateRestoresFlushModeSpec`, 4 features): the issue scenario (valid saves around a failed `validate()` are flushed on commit), `clearErrors()` then `validate()` again, `validate()` in an earlier transaction, and an invalid instance that is never persisted. * Failing first: the issue scenario and the `clearErrors()` case fail without the change in both modules. * Full core suites: Hibernate 7 3,270 tests and Hibernate 5 973 tests, 0 failures; CodeNarc and Checkstyle clean. ## Notes for review The old leak also kept an invalid, modified, attached entity from being flushed by a query-triggered AUTO flush. `save()` of an invalid instance still protects itself by making it read-only; `validate()` never did. I could not show a case where the commit flush now writes an invalid change: a modified loaded entity that is not passed to `save()` is not written at commit in either mode I tried. I did not test the Hibernate 5 module for this. The 9.0.x change is a separate PR. 🤖 Generated with [Claude Code](https://claude.com/claude-code) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
