Skip to content

fix(hibernate): apply maxSize to the column of a string-mapped enum property - #16844

Open
borinquenkid wants to merge 6 commits into
8.0.xfrom
fix/enum-max-size-column-8.0.x
Open

borinquenkid wants to merge 6 commits into
8.0.xfrom
fix/enum-max-size-column-8.0.x

Conversation

@borinquenkid

@borinquenkid borinquenkid commented Nov 3, 2026 •

Copy link
Copy Markdown
Member

An enum property mapped as a string ignored its maxSize constraint, so its column was always VARCHAR(255) while a String property honoured it. The enum column now takes the same length constraints on both bindings.

  • Hibernate 5: GrailsDomainBinder applies the string column constraints to a default or string enum column.
  • Hibernate 7: EnumTypeBinder applies them too. A native ENUM column (H2, MySQL, MariaDB) has no length and is unaffected; PostgreSQL got varchar(255) before.

Covered by a spec in each core module, an integration scenario in each example app, a binder spec, and a Testcontainers spec for PostgreSQL, MySQL and MariaDB.

Fixes #14505

https://claude.ai/code/session_01Dmki4SQGwRSj8pRJ5KCWyX

…property

An enum property mapped as a string ignored its maxSize constraint, so the column was
always VARCHAR(255) while a String property honoured it. The enum column now takes
the same length constraints. Hibernate 7 maps the enum to a native ENUM column where a
length does not apply; a spec in each core module and an integration scenario in each
example app pin both bindings.

Fixes #14505

Claude-Session: https://claude.ai/code/session_01Dmki4SQGwRSj8pRJ5KCWyX
@borinquenkid
borinquenkid requested review from jamesfredley and a balanced review from Copilot November 3, 2026 21:18

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.

…property

The Hibernate 7 enum binder built its column without the length constraints, so on a
database that maps an enum to a varchar (PostgreSQL) the column was always 255 long
while a String property honoured maxSize. A native enum column (MySQL, MariaDB, H2)
has no length and is unaffected. A container spec covers PostgreSQL, MySQL and
MariaDB, and the binder spec pins which enum mappings take the length.

Refs #14505

Claude-Session: https://claude.ai/code/session_01Dmki4SQGwRSj8pRJ5KCWyX
@borinquenkid borinquenkid changed the title fix(hibernate5): apply maxSize to the column of a string-mapped enum property fix(hibernate): apply maxSize to the column of a string-mapped enum property Nov 3, 2026
@borinquenkid borinquenkid added this to the grails:8.0.1 milestone Nov 4, 2026
@borinquenkid
borinquenkid requested a review from matrei November 12, 2026 14:13

@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 enum inList with no maxSize throws while the mapping is built.

t.addColumn(column);
simpleValue.addColumn(column);
if (enumType == GrailsEnumType.DEFAULT || enumType == GrailsEnumType.STRING) {
stringColumnConstraintsBinder.bindStringColumnConstraints(column, pc, 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.

status inList: [Status.AVAILABLE], with no maxSize. Startup throws. The allowed value is the enum, not a string. Do not read it as a string.

… while the mapping is built

The column of a string-mapped enum takes its length from maxSize or, without one, from its inList,
and the code that reads the inList took each allowed value for a String: an inList of enum constants
threw a ClassCastException when the mapping was built, which stopped the whole application, on
Hibernate 5 and on Hibernate 7. An allowed value is read only when it is a string; the constants of
an enum, and the lists that are the allowed values of a collection, have no text of their own and
are left out, and a list without such values leaves the default length of the column.

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

Copy link
Copy Markdown
Member Author

@jamesfredley Reproduced, and fixed in the new commit, on Hibernate 7 and on Hibernate 5. EnumMaxSizeColumnSpec has an enum with inList: [EnumSizedKind.ONE] and no maxSize on both modules: the mapping failed to build with ClassCastException: EnumSizedKind cannot be cast to String, which stopped the whole application at startup, and the rows fail without the change.

The length of a string-mapped enum column comes from maxSize or, without one, from its inList, and the code that reads the inList took every allowed value for a string. An enum property lists its constants, so the cast failed. An allowed value is read only when it is a string now. The constants of an enum have no text of their own for this, so the column keeps its default length; the list of a collection, whose allowed values are whole collections, is left out the same way, and I added the rows for both to StringColumnConstraintsBinderSpec. The Hibernate 5 code had the same cast, in getMaxSize, and the same fix.

One correction to myself: I first ran this against a copy of the branch that did not have the Hibernate 7 commit (f5ada8f545), and on it Hibernate 7 did not fail. On the pushed head it does, and the line you cited is the code that reaches it.

The Hibernate 5 module (1,163 tests) and the Hibernate 7 module (4,348 tests) pass, and so do the two enum scenarios of the example applications on both, and codeStyle.

Branch policy: the schema change of this PR, the length of the column of a string-mapped enum from maxSize or inList, was already in it. This commit adds no schema change and no throw. Where the PR belongs is still open; I have not moved it.

If you find another shape that does not reproduce first time, a failing spec on a throwaway branch, or the few lines you ran, would help me a lot.

@borinquenkid

Copy link
Copy Markdown
Member Author

The Hibernate 5 half is now #17059 against 7.0.x: the column contradicts the constraint on the property, so it goes to the oldest branch with the bug (dev@ rule). This PR keeps the Hibernate 7 half; once the 7.0.x PR is merged and merged up, the Hibernate 5 files here drop out of the diff.

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 maxSize on an enum collection limits each value, not the number of values.

Table t = simpleValue.getTable();
t.addColumn(column);
simpleValue.addColumn(column);
if (enumType == GrailsEnumType.DEFAULT || enumType == GrailsEnumType.STRING) {

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 collection of enum values has maxSize 2, which should limit the collection to two values. Saving a valid value that is longer than two characters fails, because the column is limited to two characters. Limit the collection, not each value.

@borinquenkid

Copy link
Copy Markdown
Member Author

Pushed 354a72a. You are right: maxSize on a collection of enum values is the number of values, as it is on any collection, and I was setting it as the length of the column of one value. EnumTypeBinder.bindEnumType, line 127 now leaves the length of the column alone when the property is a collection of basic values (HibernateBasicProperty); an enum property of its own keeps the length from maxSize, as before. An allowed value of a collection is a whole collection, so inList is skipped for the same reason.

The test: EnumTypeBinderSpec line 140, a row beside the default, string and ordinal rows, with a Set of enum values and maxSize: 42: the column keeps its default length. It fails without the change. I also added EnumMaxSizeColumnSpec line 68, a collection with maxSize: 2 of values longer than two characters that is saved, read back, and refused with a third value; on H2 that row passes with or without the change, because H2 stores an enum as its own type with no length, so the failure you describe shows on a database whose enum column is a varchar, and the binder row is the one that proves the fix. grails-data-hibernate7-core runs 4,350 tests green and codeStyle is clean.

I did not re-check the Hibernate 5 binder of this pull request for the same case; say if you want that looked at in this push. Nothing in the guide gives the length of the column of an enum collection; the only text is the reference page for maxSize, maxSize.adoc line 43, which says it sets the maximum size of a collection or number property.

@borinquenkid

Copy link
Copy Markdown
Member Author

Pushed 312255f. In the review of the Hibernate 5 pull request of this series, #17059, sbglasius pointed out that maxSize does not validate an enum (MaxSizeConstraint supports strings, collections and arrays), so a maxSize shorter than the longest constant name passes validation and the insert fails in the database. The same case is here: EnumTypeBinder.bindEnumType, line 131 now widens a column that is shorter than the longest constant name to that length, as the Hibernate 5 change does; a longer maxSize, an inList, and a column with no constraint are as they were.

The test: EnumTypeBinderSpec line 123, an enum whose longest constant, OUT_OF_STOCK, is 12 characters, with maxSize: 2: the column is 12 wide. It fails without the change. grails-data-hibernate7-core runs 4,351 tests green and codeStyle is clean. Nothing in the guide gives the length of an enum column.

…lumn-8.0.x

# Conflicts:
#	grails-data-hibernate5/core/src/main/groovy/org/grails/orm/hibernate/cfg/GrailsDomainBinder.java
#	grails-data-hibernate5/core/src/test/groovy/grails/gorm/tests/EnumMaxSizeColumnSpec.groovy

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.

For Enum field in domain class, maxSize cannot be applied in migration

3 participants