Skip to content

fix(hibernate5): honour length on the id in MappingBuilder.orm - #17266

Open
borinquenkid wants to merge 9 commits into
7.0.xfrom
fix/mapping-builder-id-length-on-7.0.x
Open

borinquenkid wants to merge 9 commits into
7.0.xfrom
fix/mapping-builder-id-length-on-7.0.x

Conversation

@borinquenkid

@borinquenkid borinquenkid commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

Replaces #16987, which was based on 7.2.x. The bug is also on 7.0.x, the oldest branch with it, so the change is rebuilt there. The review history is on #16987.

Mapping.id(Map) bound the map to Identity, which has no length, so id generator: 'assigned', length: 20 inside MappingBuilder.orm was dropped and the id column stayed VARCHAR(255).

Change: the length goes to the id column, as with a mapping closure. The column starts from the '*' defaults, so a default such as an sqlType still reaches the id, and a later declaration of the id fills in the same column instead of adding a second one.

Since a column Grails creates now has the declared length, there is an upgrade note under Grails 7.0.18.

Tests

  • IdentityLengthSpec reads the columns from the schema. Without the change 4 of its 8 tests fail.
  • grails-data-hibernate5-core: 1381 tests, 59 skipped, none failing. codeStyle passes.

Not covered

Fixes #14497

https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

Mapping.id(Map) bound the map to Identity, which has no length, so the
value was dropped and the id column stayed VARCHAR(255). The length now
goes to the id column the way the mapping closure form already did.

Fixes #14497

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
Answers the review of #16987: the id config started blank, which stopped the fallback to the '*' defaults, so an id with a length lost the wildcard SQL type. It now starts from the wildcard defaults, as every other property does, and the length is set on that column.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
…s length made

id generator: 'assigned', length: 20 followed by property('id') { column { name 'custom_id' } } gave the id two columns, and
mapping validation failed. The length sizes the id column the mapping builder keeps for it, and marked that column as taken, so
the column a later declaration makes was added beside it. The column a length creates, or takes over from the wildcard defaults,
is now left open to the next declaration, as a copy of the defaults is, so the identifier keeps one column, in either order.

IdentityLengthSpec gains rows for the length before and after the declaration, with a wildcard SQL type, and for a declaration
that gives no name.

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.

🟢 Approval recommended

No active review issues block the Hibernate 5 identifier-sizing fix.

No open issues 🎉

What changed in this PR

Fixes Hibernate 5 identifier sizing in MappingBuilder.orm, aligning it with mapping closures and addressing #14497.

Changes:

  • Applies numeric identifier length to the column while preserving wildcard defaults and reusing it for later declarations.
  • Adds schema-based regression tests for identifier lengths, wildcard SQL types, and declaration order.
  • Documents the changed behavior in the Grails 7.0.18 upgrade notes.
File Description
grails-doc/​src/​en/​guide/​upgrading/​upgrading60x.adoc Explains identifier sizing and its upgrade impact.
grails-data-hibernate5/​core/​src/​test/​groovy/​grails/​gorm/​tests/​IdentityLengthSpec.groovy Adds eight schema-based regression cases.
grails-data-hibernate5/​core/​src/​main/​groovy/​org/​grails/​orm/​hibernate/​cfg/​Mapping.groovy Binds identifier length while preserving defaults and column reuse.

Review effort: Balanced

Both sides added upgrade notes at one place; all are kept.

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

@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. A length set on an id is not moved when the id is later named.

}

private void bindIdentityColumnLength(Identity id, int length) {
String name = id.name ?: 'id'

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.

An id is given a length, and a later call names the id code. The column for code should have that length. It does not, because the length stays on the first name. Move the length with the name.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@jamesfredley Confirmed, and it is not new in this pull request. I ran four shapes on 7.0.x and on this branch:

Mapping 7.0.x this branch
name and length in one call 255 20
name first, then id length: 20 255 20
length: 20 first, then id name: 'code' 255 255
the same two calls in a plain mapping closure 255 255

The third row is your case: the length stays on the first name of the id. It fails on 7.0.x, and in the mapping closure it fails on this branch as well, so I filed it as #17271 and left this pull request on the example of #14497, which it fixes.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@jamesfredley You are right, and this is in code this pull request adds. 8acb48b moves the length with the name: id generator: 'assigned', length: 20 followed by id name: 'code' now sizes the code column 20 (it was 255), and so does naming the id first and giving the length after. IdentityLengthSpec has a row for each order; the first fails on the previous head (the column is 255) and passes now. The Hibernate 5 module runs 1382 tests with 0 failures, and codeStyle is clean.

@borinquenkid

Copy link
Copy Markdown
Member Author

7.0.x is merged into this branch (1b13244) to resolve a conflict in the upgrade guide. Nothing else changed, and the tests and codeStyle pass on the merged tree. Approvals on the earlier commit need to be given again for the merge rule.

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

The original identifier-length case, preservation of wildcard SQL types, and reuse of the identifier column are addressed. The latest change also handles a length followed by a name when the destination property has no existing configuration. One combination of the reviewed cases remains incomplete.

P2 — Carry the identifier length into an already-configured destination. Mapping.groovy:217–220 moves the configuration only when columns[nameAfter] == null. For an entity with String code, consider:

static mapping = MappingBuilder.orm {
    property('code') { column { name 'custom_code' } }
    id generator: 'assigned', length: 20
    id name: 'code'
}

The first declaration sets only the destination column's name, not its length. It nevertheless prevents the transfer: the length remains under id, while the identifier is bound from code with its default column length. Giving the identifier name before its length instead applies 20, so these declarations still depend on ordering.

Please merge the identifier length into the destination configuration while preserving its explicit column settings. Add a schema-reading regression alongside the current rename cases that asserts one identifier column named custom_code with length 20. This is a source-based follow-up; I have not rerun the tests.

…rty that is configured already

When the id was named after a length, the length moved only to a destination with no configuration, so a destination whose
column name was set, such as custom_code, kept the default length. The length now joins the settings of that property, and a
length it sets itself stays.

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

Copy link
Copy Markdown
Member Author

Fixed in 6978437: when the id is named after its length, and the destination property is configured already, the length now joins the settings of that property, and a length that it sets itself stays. The earlier code moved the length only to a destination with no configuration, so a destination whose column name was set kept the default length, and the result depended on the order of the declarations.

IdentityLengthSpec has your case in both orders: the column name of code set first, then the id length, then the id named code, and the id length first, then the column name, then the name. Each gives one identifier column named custom_code, sized 20, and no other column but version. With the earlier code put back that row fails. The Hibernate 5 module passes (1383 tests) and codeStyle passes.

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

The earlier page-size finding is not in these commits. Requested changes. Renaming the identifier can leave two columns, and a length given in a block does not reach the destination.

// a column length that is not set is -1
if (sourceColumn != null && sourceColumn.length > 0) {
if (destination.columns.isEmpty()) {
destination.columns << new ColumnConfig()

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 property is declared, the identifier is given a length, and the identifier is then named that property. A later column declaration should replace the placeholder. It adds a second column, so the identifier has two columns. Keep one.

Object length = config.remove('length')
String nameBefore = ((Identity) identity).name ?: 'id'
Identity.configureExisting((Identity) identity, config)
moveIdentityLengthWithName(nameBefore, ((Identity) identity).name ?: 'id')

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.

The identifier is given a length, then named in a block. The named property should have that length. It keeps the default, because this does not move the length. Move it.

…p one column, and keep a declared length

The length that id(length:) gives the identifier now moves to the name that a later block gives it, id { name 'code' }, as it does for id(name:). When the length joins a property that is declared already without a column, the column it creates is filled in by a later declaration of the property, so the identifier keeps one column. A length that the declaration of the property sets itself stays, whichever call came first.

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

Copy link
Copy Markdown
Member Author

ee81381dd83 takes both findings.

A name given in a block (Mapping.groovy:205). id { name 'code' } now moves the length that id(length:) gave the identifier to the new name, as id(name:) did. id(Closure) did not call the move.

A second column (:233). When the length joins a property that is declared already without a column, the column it creates is a placeholder, and a later declaration of the property fills it in. The identifier keeps one column.

I generated the class instead of adding two rows. IdentityLengthMappingSpec builds the mapping through MappingBuilder.define for every combination of the form of the call that names the identifier (a map or a block), the order of that call and the length (name first or length first), and the declaration of the property (none, declared with no column, declared with a named column, declared with its own length, a column declared later, and the declaration between the two calls). That is 48 rows. Each asserts that the identifier is code, that its property has one column, that the column has the declared name, and that its length is 20 unless the declaration sets its own.

Running it showed one more case that the two findings did not name: a property that sets its own length, with the name given first and id(length:) after. The id length replaced the declared length, while the other order kept it. The declared length now stays in both orders, the rule moveIdentityLengthWithName already used.

The Hibernate 5 core module passes (1431 tests on this branch) and codeStyle passes. With Mapping.groovy put back, 18 of the 48 rows fail.

@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. Renaming the identifier moves an unrelated property mapping, and a later identifier length is ignored.

}
PropertyConfig destination = columns[nameAfter]
if (destination == null) {
columns[nameAfter] = columns.remove(nameBefore)

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.

id name: 'oldCode'; property('oldCode') { column { name 'legacy_code'; length 40 } }; id length: 20; id name: 'code' moves the oldCode mapping onto code. oldCode loses its column, and the new identifier inherits length 40 instead of 20. Move only the identifier length. Leave the property mapping where it was.

…ends, not when the length is declared

id length: n put the length on the column named by the identifier at that moment, and a later name moved it, so an identifier that was named, sized and
then renamed (id name: 'oldCode'; property('oldCode') {...length 40}; id length: 20; id name: 'code') moved the mapping of oldCode onto code: oldCode lost its
declared column and code took over its length. The length is now kept and applied once, when the mapping closure has ended, to the column of the identifier by its
final name, and the mapping of every other property stays where it was declared. This replaces the move on rename and the flag that kept a column open for a later
declaration. IdentityLengthSpec has the nine shapes of the identifier declarations (length then rename, rename then length, length before any name) and of the
property mapping (on the old name, on the new name, none); 4 of them fail with the previous code. A property declared with property() after a wildcard default
loses its declaration on 7.0.x already, so the row of this spec that relied on the removed mechanism for it is gone.
@borinquenkid

Copy link
Copy Markdown
Member Author

Pushed the next commit, which answers the review of 12-05 at ee81381. I measured your example first, on 7.0.x and on the head of the review, with the mapping of ProbeIdRename (a code and an oldCode property; id name: 'oldCode', property('oldCode') { column { name 'legacy_code'; length 40 } }, id length: 20, id name: 'code') and the columns that Hibernate creates:

CODE LEGACY_CODE
7.0.x (id length: is ignored) 255 40
the head of the review 255 20 (moved onto code's name, then the length landed on the wrong column)
this commit 20 40

So the property mapping of oldCode lost 20 characters of declared length, which is a regression of this pull request against 7.0.x, as you said.

The change is a smaller mechanism, not another case on top of the old one. The length used to be written onto the column of the identifier's name at the moment of the call, and a later name call moved it (moveIdentityLengthWithName), with a flag that kept a column open for a later declaration. Every rename was a new way for that move to touch another property. Now id length: n only records the number, and Mapping.sizeIdentityColumn() applies it once, when the mapping closure has ended (Mapping.configureExisting for MappingBuilder.orm, and the end of HibernateMappingBuilder.evaluate for a mapping closure), to the column of the identifier by its final name. The mapping of any other property is never touched, the column starts from the wildcard defaults as before, and a length that the declaration of the column sets itself stays, which IdentityLengthMappingSpec pins (48 rows, all pass). The move on rename and the flag are gone: the commit removes more lines than it adds in Mapping.groovy.

IdentityLengthSpec has the table: the identifier declarations in three orders (length then rename, rename then length, length before any name) crossed with where the property mapping sits (on the old name, on the new name, none), 9 domain classes, asserting the columns that Hibernate creates: the identifier column has the length, or the one that its declaration sets, and every other column has what it declared. Against the sources of the review's head the row of your example fails; the other rows, and the rows that the review's head already had (IdLengthThenName, NameThenIdLength, the two with a column name), pass with both.

One row of that spec is gone. '*'(sqlType ...) followed by property('id') { column { name 'custom_id' } } kept its column name only because the earlier mechanism created the column when the length was declared; without id length: the declaration of property() after a wildcard is lost on 7.0.x already (getOrInitializePropertyConfig returns the clone of the defaults without storing it), so that row was testing a different bug. It is #17339.

grails-data-hibernate5-core passes and codeStyle is clean.

@testlens-app

testlens-app Bot commented Dec 5, 2026 •

Copy link
Copy Markdown

🚨 All tests passed but jobs failed 🚨

Failed Jobs without Test Failures

❌ CI / Hibernate5 Functional Tests (25)

🏷️ Commit: 8dc66f3
▶️ Tests: 61971 executed
⚪️ Checks: 58/58 completed

Rerun Controls

Click the checkbox to trigger a rerun:

  • Rerun jobs

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. Renaming the identifier inside one closure no longer moves an unrelated mapping. A length declared in a base mapping, then a column declared in a child mapping, produces two identifier columns.

config.setDelegate(mapping)
config.setResolveStrategy(Closure.DELEGATE_ONLY)
config.call()
// every declaration of the identifier is in, so its name is final: the length that id length: n asked for goes on that column

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 base mapping with id length: 20, and a subclass with property('id') { column { name 'custom_id' } }, creates one column for the length and then appends the named column. The identifier has two columns, and Hibernate cannot start. Apply the pending length once, after every mapping in the hierarchy has been read.

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.

4 participants