MartinJesusDev opened a new pull request, #16625: URL: https://github.com/apache/grails-core/pull/16625
## Description Fixes #16600. Loading a lazy proxy for a Groovy domain class that implements a trait with default field values fails with `Unable to generate proxy for <entity>`, caused by a `NullPointerException` in the trait's generated `$init$`. Groovy 3+ stopped marking the generated `getMetaClass()` as synthetic, so Hibernate's `groovyGetMetaClassFilter` (synthetic-only since HHH-3870) no longer excludes it from the entity proxy. The proxy constructor runs before Hibernate assigns the interceptor, so the intercepted `getMetaClass()` answers the stub value (`null`) and Groovy's dynamic dispatch dereferences it while the trait initializer runs (the proxy is built from the entity constructor). The fix wraps the proxy class in a subclass whose `getMetaClass()` answers the persistent class's MetaClass while the interceptor is not assigned yet - the same answer `GroovyProxyInterceptorLogic` gives for an uninitialized proxy - and otherwise leaves the interceptor handling untouched. Non-Groovy entities keep the stock proxy class unchanged. Applied to both `grails-data-hibernate5` (Hibernate 5.6) and `grails-data-hibernate7` (Hibernate 7.4). Known limitation (not introduced by this change): deserializing a previously serialized proxy of such an entity still goes through Hibernate's own helper and keeps the original behavior. The root cause is the stale synthetic-only filter in Hibernate. ## Contributor Checklist ### Issue and Scope - [x] Linked to issue #16600 (https://github.com/apache/grails-core/issues/16600). - [x] Addresses the complete scope: proxy creation is fixed for both Hibernate modules. - [x] Single focused change (fix + regression/unit tests). - [x] Targets `8.0.x` (patch release branch, bug fix only). ### Code Quality - [x] Added tests: `TraitInitializerProxySpec` (h5/h7), `GroovyProxyMetaClassDispatcherSpec` (h5/h7) and `ByteBuddyGroovyProxyFactorySpec` (new in h5, extended in h7). - [x] Module test suites run: h5 `1085 tests, 0 failures, 29 skipped`; h7 `3650 tests, 0 failures, 15 skipped`. - [x] `./gradlew :grails-data-hibernate5-core:codeStyle :grails-data-hibernate7-core:codeStyle` passes. - [x] No mass reformatting. - [x] AI tooling used (OpenCode); output reviewed and verified. ### Licensing and Attribution - [x] Apache License 2.0 headers on new files. - [x] Generative AI policy followed and disclosed below. ### Documentation - [x] No user-facing change: no documentation or upgrade notes required. - [x] PR explains what changed and why. ### Generative AI disclosure Prepared with the assistance of OpenCode (deepseek-v4.1-flash); the submitter reviewed and verified every line. The failure was reproduced independently of the change, and every claim was checked against the Hibernate 5.6/7.4 sources and the module test suites. -- 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]
