Repository navigation
fix(hibernate5): honour length on the id in MappingBuilder.orm - #17266
borinquenkid wants to merge 9 commits into
Conversation
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
There was a problem hiding this comment.
🟢 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
lengthto 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
left a comment
There was a problem hiding this comment.
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' |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
@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.
There was a problem hiding this comment.
@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.
|
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. |
…er call gives the id Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
jdaugherty
left a comment
There was a problem hiding this comment.
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
|
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.
|
jamesfredley
left a comment
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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') |
There was a problem hiding this comment.
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
|
A name given in a block ( A second column ( I generated the class instead of adding two rows. 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 The Hibernate 5 core module passes (1431 tests on this branch) and |
jamesfredley
left a comment
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
|
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
So the property mapping of 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 (
One row of that spec is gone.
|
🚨 All tests passed but jobs failed 🚨Failed Jobs without Test Failures❌ CI / Hibernate5 Functional Tests (25) 🏷️ Commit: 8dc66f3 Rerun ControlsClick the checkbox to trigger a rerun:
Learn more about TestLens at testlens.app/docs. |
jamesfredley
left a comment
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
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 toIdentity, which has nolength, soid generator: 'assigned', length: 20insideMappingBuilder.ormwas dropped and the id column stayedVARCHAR(255).Change: the length goes to the id column, as with a
mappingclosure. The column starts from the'*'defaults, so a default such as ansqlTypestill 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
IdentityLengthSpecreads the columns from the schema. Without the change 4 of its 8 tests fail.grails-data-hibernate5-core: 1381 tests, 59 skipped, none failing.codeStylepasses.Not covered
columnon the id is still ignored byMappingBuilder.orm, with or without this change: Hibernate 5: MappingBuilder.orm ignores column on the id #17256.Fixes #14497
https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL