Repository navigation
fix(hibernate): apply maxSize to the column of a string-mapped enum property - #16844
borinquenkid wants to merge 6 commits into
Conversation
…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
…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
jamesfredley
left a comment
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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
|
@jamesfredley Reproduced, and fixed in the new commit, on Hibernate 7 and on Hibernate 5. The length of a string-mapped enum column comes from One correction to myself: I first ran this against a copy of the branch that did not have the Hibernate 7 commit ( 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 Branch policy: the schema change of this PR, the length of the column of a string-mapped enum from 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. |
|
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. |
jamesfredley
left a comment
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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.
…e length of the column of one value Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
Pushed 354a72a. You are right: The test: 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 |
…st name of the enum at least Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
Pushed 312255f. In the review of the Hibernate 5 pull request of this series, #17059, sbglasius pointed out that The test: |
…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
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.
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