Skip to content

fix(gorm): compile a JPA entity with several @Id properties - #16990

Open
borinquenkid wants to merge 3 commits into
7.0.xfrom
fix/jpa-multiple-id-compile-7.0.x
Open

borinquenkid wants to merge 3 commits into
7.0.xfrom
fix/jpa-multiple-id-compile-7.0.x

Conversation

@borinquenkid

Copy link
Copy Markdown
Member

DirtyCheckingTransformer added a transient getId() accessor for every JPA @Id property whose name is not id. An entity with two @Id properties (a composite id) got the method twice and failed to compile with "The method public java.lang.Long getId() duplicates another method of the same signature". The accessor is now added only when exactly one property is the @Id, since a composite id has no single value to return.

Verified locally: the new JpaCompositeIdSpec entity failed to compile with the three errors from the issue before the change; now it compiles, saves and loads by its composite id. A second feature keeps the single-@Id case working (getId() still returns the @Id value). All 252 grails-datamapping-core tests and the Hibernate 5 grails.gorm.tests.jpa specs pass; CodeNarc is clean.

Fixes #14494

https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

The dirty checking transformation added a getId() accessor for every
@id property not named id, so an entity with a composite id got the same
method twice and failed with 'duplicates another method'. The accessor
is now added only when exactly one property is the @id.

Fixes #14494

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@borinquenkid borinquenkid mentioned this pull request Nov 14, 2026
1 of 4 tasks

@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.

Not approving. An entity whose identifiers are all declared on it compiles. Identifiers split between a mapped superclass and the entity still fail to compile.

if (classNode.getField(GormProperties.IDENTITY) == null && gettersAndSetters[GormProperties.IDENTITY] == null) {
// if the property is the only JPA @Id but the property name is not id add a transient getter to retrieve the id called getId,
// a composite id has no single value to return
if (jpaIdPropertyCount == 1 && classNode.getField(GormProperties.IDENTITY) == null && gettersAndSetters[GormProperties.IDENTITY] == null) {

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.

A mapped superclass has @id Long orgId. The entity extends it and adds @id String userNo. The count sees only userNo, so it still generates getId() on both classes, with different return types. Compilation fails. Count inherited identifiers too, and do not generate getId() when there is more than one.

…whether to generate getId

Answers the review of #16990: an entity whose @id properties are split between a mapped superclass and the entity counted only its own, so it still generated getId() beside the superclass's, with a different return type. The table spec covers where the identifiers are declared.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
@borinquenkid

Copy link
Copy Markdown
Member Author

Answers the split-identifier case. The count that decides whether to generate getId() now includes the @Id properties a superclass declares (by field, property or getter), so an entity with an @Id on a mapped superclass and one of its own no longer generates getId() beside the superclass's.

JpaCompositeIdSpec has a five-layout table (the identifiers on the entity, one on the entity, one on a mapped superclass and one on the entity, one on a superclass only, two on a superclass and one on the entity); 2 of its 7 rows fail without the change. datamapping-core 252, Hibernate 5 694, in-memory 569 tests, 0 failures; codeStyle clean.

Not tried: a superclass that comes from a precompiled jar, whose annotations the compiler reads differently.

@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.

Requested changes. Identifiers split between a mapped superclass and the entity no longer generate two getId methods. An ordinary superclass that has an identifier is still counted, so an entity with one identifier loses its getter.

*/
private static int inheritedJpaIdCount(ClassNode classNode) {
Set<String> names = new LinkedHashSet<String>()
for (ClassNode superClass = classNode.superClass; superClass != null && superClass != ClassHelper.OBJECT_TYPE; superClass = superClass.superClass) {

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.

A plain superclass has an identifier field, and the entity has one identifier of its own. The entity used to get a getter. It does not now, because this counts the superclass field even though that class is not mapped. Count a superclass only when its mapping is kept.

…g is kept

The count of identifiers that decides whether an entity gets a generated getId() also counted a plain superclass that declares
an @id, which no mapping keeps. An entity with one identifier of its own then lost its getter, as if it had two. Only a mapped
superclass or an entity superclass is counted now, at any height of the hierarchy, a plain class in between being skipped.

JpaCompositeIdSpec gains rows for a plain superclass with one and with two identifiers, with one on a getter, an entity
superclass, and a mapped superclass above a plain class.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
@borinquenkid

Copy link
Copy Markdown
Member Author

Thank you, you are right: the count took a plain superclass's @Id as part of the key, and no mapping keeps it.

Only a superclass whose mapping is kept is counted now: a @MappedSuperclass or an entity, at any height of the hierarchy, with a plain class in between skipped. An entity with one identifier of its own under a plain superclass that declares an @Id (on a field, a property or a getter) gets its getId() again.

JpaCompositeIdSpec has five new rows: a plain superclass with one identifier, with two, and with one on a getter; an entity superclass with the entity (one each); and a mapped superclass above a plain class, which still counts. They fail without the fix (3 of 12) and pass with it. The Hibernate 5 module (699 tests) and grails-datamapping-core (252 tests), which owns the transformer, are green, and codeStyle is clean.

@testlens-app

testlens-app Bot commented Nov 16, 2026

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: 159d074
▶️ Tests: 58354 executed
⚪️ Checks: 57/57 completed


Learn more about TestLens at testlens.app/docs.

@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.

Requested changes. An ordinary superclass is no longer counted. The same identifier declared again on the entity is counted twice, so the getter is not generated.

}
}
// an identifier declared on a superclass belongs to the same key, so it counts as well
jpaIdPropertyCount += inheritedJpaIdCount(classNode)

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.

A mapped superclass declares getCode, and the entity declares the same identifier again. The entity used to get a getter. It does not now, because the same name is counted twice. Count each name once.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

3 participants