Repository navigation
Composite key issues - #16485
Composite key issues#16485
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## 8.0.x #16485 +/- ##
==================================================
+ Coverage 59.1571% 59.3828% +0.2257%
- Complexity 24607 24844 +237
==================================================
Files 2170 2171 +1
Lines 107610 108203 +593
Branches 19606 19735 +129
==================================================
+ Hits 63659 64254 +595
+ Misses 35097 35023 -74
- Partials 8854 8926 +72
🚀 New features to boost your workflow:
|
Match the configured identifier getter by Method rather than name. This prevents composite identifiers being returned through Long getId(), and lets overloaded getters and business getIdentifier() methods execute normally. Verify the compatibility cases against Grails 7.2.4 with both stock Hibernate and hibernate-groovy-proxy 1.1. Preserve the existing dynamic Groovy id shortcuts on uninitialized proxies.
Fall back to the id property when the mapping context has no entity for the class, and add GormInstanceApiIdentSpec covering generated, renamed, composite, non-Serializable composite, subclass, no-identity and unmapped cases in the owning core module. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
borinquenkid
left a comment
There was a problem hiding this comment.
Approving. I pushed one small commit (5003c0c) covering a gap in GormInstanceApi.ident(): it now falls back to the id property when the mapping context has no entity for the class, and the new GormInstanceApiIdentSpec exercises its branches in grails-datamapping-core. The tests in that module pass, and the Hibernate 5 and 7 interceptor changes are fully covered by the existing proxy specs.
jamesfredley
left a comment
There was a problem hiding this comment.
Line comments from review of mapped ident() and proxy identifier access. The blocking item is lazy ident() on non-Hibernate proxies.
Reading the mapped identity property made ident() initialize an uninitialized non-Hibernate proxy whose key is not named `id`, because only `id` and `getId()` are answered by the proxy handler. On a missing row that load fails. Return the proxy handler's identifier for an uninitialized proxy before reading mapped properties. Document that an uninitialized proxy returns the identifier it was created with, which for a composite key is the object passed to load(). Cover renamed and generated keys on Javassist proxies and composite keys on Hibernate 5 and 7 proxies, and obtain the business getIdentifier() proxy through load().
✅ All tests passed ✅🏷️ Commit: 84314f3 Learn more about TestLens at testlens.app/docs. |
|
@jamesfredley this is ready for your review again. |
jamesfredley
left a comment
There was a problem hiding this comment.
Approved. 84314f3 addresses the three comments: ident() returns the proxy identifier before reading mapped properties, the docs describe the uninitialized-proxy exception, and the cited spec uses load().
Why this is needed
Grails 8's
GormInstanceApi.ident()reads the literalidproperty instead of the mapped identifier following the registry consolidation in #16066. That loses two behaviors supported in Grails 7:id name: 'code',ident()returnsnullinstead of the assignedcode.ident()returnsnullinstead of a serializable identifier containing the mapped key fields.This surfaced during an application upgrade on a Spring Batch parameter entity whose key combines a parameter name and its job-execution association. Even after persistence,
entity.ident()was null, soDomainClass.load(entity.ident())returned null before a proxy could be created. It also breaks callers usingget(entity.ident())to reload such entities.The new public-API regression tests reproduce both problems on Hibernate 5 and Hibernate 7: before the fix, three of the four tests failed on each version.
Follow-up: proxy getter dispatch
Restoring the composite identifier allowed the application's proxy checks to reach the next operation,
proxy.getId(). That exposed a second issue: the Grails 8 proxy interceptor treated any method namedgetId()orgetIdentifier()as an identifier getter, and matched a renamed getter by name without checking its signature. It could return the composite key through a generatedLong getId()method or bypass an unrelated business method or overload.The follow-up matches Hibernate's actual configured getter with
Method.equals. Other methods execute normally.Verified Grails 7 behavior
These cases were run using published Grails 7.2.4 Hibernate 5 dependencies against H2, both with stock Hibernate proxies and with
org.yakworks:hibernate-groovy-proxy:1.1. Fresh Hibernate proxies were used to distinguish getter results from initialization effects. The fixture domains use their generatedgetId()methods rather than custom overrides.getId()nullLongfailsgetId()nullLongfailsgetCode('prefix-')prefix-NAMEDNAMED, ignoring the argumentgetIdentifier()external-referencevalueStringfailsDynamic
idaccess is not universally the same across Grails 7 proxy providers. Stock Hibernate initializes these fixture proxies and reads the ordinary property; the optional Groovy-aware provider returns the mapped identifier for dynamicid,getProperty('id'), andgetAt('id')access while uninitialized. Grails 8 already retains that shortcut. This follow-up preserves it and fixes the incorrectly intercepted getter methods; it does not make dynamic property access interchangeable with a directgetId()call.Changes
ident()reference documentation.load()documentation to distinguish mapped getter access from dynamic Groovy property access.Verification
The new regression tests pass on both Hibernate versions after the fix. Full affected-module results:
grails-datamapping-coregrails-data-hibernate5-coregrails-data-hibernate7-coreMain-source style checks and repository-conventions validation also passed. Test-source style checks are disabled by the repository's default configuration.
Repository-wide pre-commit verification was run with:
The latest full run's only task failure was RAT finding generated Neo4j runtime files from a previous test run. Those ignored runtime files were moved out of the source tree, and
aggregateViolations :grails-test-report:markdownCombinedAggregateTestReport --continuethen passed with zero unknown licenses and the clean test report above.