Skip to content

Composite key issues - #16485

Merged
jdaugherty merged 4 commits into
8.0.xfrom
fix/composite-ident
Oct 3, 2026
Merged

jdaugherty merged 4 commits into
8.0.xfrom
fix/composite-ident

Conversation

@jdaugherty

@jdaugherty jdaugherty commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Why this is needed

Grails 8's GormInstanceApi.ident() reads the literal id property instead of the mapped identifier following the registry consolidation in #16066. That loses two behaviors supported in Grails 7:

  • With id name: 'code', ident() returns null instead of the assigned code.
  • With a composite key, ident() returns null instead 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, so DomainClass.load(entity.ident()) returned null before a proxy could be created. It also breaks callers using get(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 named getId() or getIdentifier() as an identifier getter, and matched a renamed getter by name without checking its signature. It could return the composite key through a generated Long 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 generated getId() methods rather than custom overrides.

Operation Grails 7.2.4, both providers Grails 8 before this follow-up
Composite-key getId() null Composite-key object cast to Long fails
Renamed-key getId() null String key cast to Long fails
Renamed getter overload getCode('prefix-') prefix-NAMED Returns NAMED, ignoring the argument
Business getter getIdentifier() Its stored external-reference value Numeric primary key cast to String fails

Dynamic id access 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 dynamic id, getProperty('id'), and getAt('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 direct getId() call.

Changes

  • Resolve the identifier through the entity's mapping metadata.
  • Read the mapped property for a single-property identifier, including a renamed one.
  • Construct a separate serializable domain instance for composite identifiers and copy only the mapped key properties, including associations.
  • Cover generated IDs, renamed IDs, composite-key copying, persisted reloads, and lazy loading through public GORM APIs on both Hibernate versions.
  • Exercise the existing default-ID test through the domain API and expand the ident() reference documentation.
  • Respect the mapped getter's full method identity in the Hibernate 5 and Hibernate 7 proxy interceptors.
  • Add real-proxy regressions for composite and renamed keys, getter overloads, business identifier getters, detached default-ID access, and the existing dynamic Groovy identifier shortcuts.
  • Correct the 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:

Module Passed Skipped Failed
grails-datamapping-core 1,694 1 0
grails-data-hibernate5-core 961 40 0
grails-data-hibernate7-core 3,352 30 0

Main-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:

GRAILS_INCLUDE_MAVEN_LOCAL=1 ./gradlew clean aggregateViolations :grails-test-report:check --continue --console=plain --max-workers=4 -PmaxTestParallel=2
  • Combined report: 22,838 tests, 0 failures, 0 errors, 445 skipped.
  • Unit report: 19,027 tests, 0 failures/errors, 337 skipped.
  • Integration report: 3,811 tests, 0 failures/errors, 108 skipped.
  • RAT, Checkstyle, CodeNarc, repository conventions, and PMD for its enabled modules passed. SpotBugs is disabled in this configuration.

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 --continue then passed with zero unknown licenses and the clean test report above.

@codecov

codecov Bot commented Oct 2, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.88889% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 59.3828%. Comparing base (813221e) to head (84314f3).
⚠️ Report is 62 commits behind head on 8.0.x.

Files with missing lines Patch % Lines
...y/org/grails/datastore/gorm/GormInstanceApi.groovy 88.8889% 0 Missing and 2 partials ⚠️
Additional details and impacted files

Impacted file tree graph

@@                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     
Files with missing lines Coverage Δ
...y/org/grails/datastore/gorm/GormInstanceApi.groovy 89.1304% <88.8889%> (-0.1258%) ⬇️

... and 25 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.

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.
@jdaugherty jdaugherty changed the title Restore mapped identifiers in GORM ident() Composite key issues Oct 3, 2026
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 borinquenkid left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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().
@testlens-app

testlens-app Bot commented Oct 3, 2026 •

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: 84314f3
▶️ Tests: 23607 executed
🟡 Checks: 86/87 completed


Learn more about TestLens at testlens.app/docs.

@jdaugherty

Copy link
Copy Markdown
Contributor Author

@jamesfredley this is ready for your review again.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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().

@jdaugherty
jdaugherty merged commit 4fc83cd into 8.0.x Oct 3, 2026
154 of 155 checks passed
@jdaugherty
jdaugherty deleted the fix/composite-ident branch October 3, 2026 21:42
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.

3 participants