From 98149b564a360be735ab34966a44902526ec5b8e Mon Sep 17 00:00:00 2001 From: Walter Duque de Estrada Date: Tue, 3 Nov 2026 15:11:58 -0600 Subject: [PATCH 1/5] fix(hibernate5): apply maxSize to the column of a string-mapped enum 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 --- .../orm/hibernate/cfg/GrailsDomainBinder.java | 4 ++ .../gorm/tests/EnumMaxSizeColumnSpec.groovy | 61 ++++++++++++++++ .../gorm/tests/EnumMaxSizeColumnSpec.groovy | 70 +++++++++++++++++++ .../functional/tests/ScenarioEnumSized.groovy | 36 ++++++++++ .../tests/EnumMaxSizeScenarioSpec.groovy | 70 +++++++++++++++++++ .../functional/tests/ScenarioEnumSized.groovy | 36 ++++++++++ .../tests/EnumMaxSizeScenarioSpec.groovy | 69 ++++++++++++++++++ 7 files changed, 346 insertions(+) create mode 100644 grails-data-hibernate5/core/src/test/groovy/grails/gorm/tests/EnumMaxSizeColumnSpec.groovy create mode 100644 grails-data-hibernate7/core/src/test/groovy/grails/gorm/tests/EnumMaxSizeColumnSpec.groovy create mode 100644 grails-test-examples/hibernate5/grails-hibernate/grails-app/domain/functional/tests/ScenarioEnumSized.groovy create mode 100644 grails-test-examples/hibernate5/grails-hibernate/src/integration-test/groovy/functional/tests/EnumMaxSizeScenarioSpec.groovy create mode 100644 grails-test-examples/hibernate7/grails-hibernate/grails-app/domain/functional/tests/ScenarioEnumSized.groovy create mode 100644 grails-test-examples/hibernate7/grails-hibernate/src/integration-test/groovy/functional/tests/EnumMaxSizeScenarioSpec.groovy diff --git a/grails-data-hibernate5/core/src/main/groovy/org/grails/orm/hibernate/cfg/GrailsDomainBinder.java b/grails-data-hibernate5/core/src/main/groovy/org/grails/orm/hibernate/cfg/GrailsDomainBinder.java index c1084c11f94..0405a7c58a7 100644 --- a/grails-data-hibernate5/core/src/main/groovy/org/grails/orm/hibernate/cfg/GrailsDomainBinder.java +++ b/grails-data-hibernate5/core/src/main/groovy/org/grails/orm/hibernate/cfg/GrailsDomainBinder.java @@ -2264,6 +2264,10 @@ else if (!"ordinal".equalsIgnoreCase(enumType)) { simpleValue.addColumn(column); + if (typeName == null && (pc == null || DEFAULT_ENUM_TYPE.equals(pc.getEnumType()) || "string".equalsIgnoreCase(pc.getEnumType()))) { + bindStringColumnConstraints(column, property); + } + PropertyConfig propertyConfig = getPropertyConfig(property); if (propertyConfig != null && !propertyConfig.getColumns().isEmpty()) { ColumnConfig cc = propertyConfig.getColumns().get(0); diff --git a/grails-data-hibernate5/core/src/test/groovy/grails/gorm/tests/EnumMaxSizeColumnSpec.groovy b/grails-data-hibernate5/core/src/test/groovy/grails/gorm/tests/EnumMaxSizeColumnSpec.groovy new file mode 100644 index 00000000000..bb562b02d71 --- /dev/null +++ b/grails-data-hibernate5/core/src/test/groovy/grails/gorm/tests/EnumMaxSizeColumnSpec.groovy @@ -0,0 +1,61 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package grails.gorm.tests + +import grails.gorm.annotation.Entity +import spock.lang.Issue + +@Issue('https://github.com/apache/grails-core/issues/14505') +class EnumMaxSizeColumnSpec extends HibernateGormDatastoreSpec { + + void setupSpec() { + manager.registerDomainClasses(EnumSizedThing) + } + + void "maxSize constrains the column of an enum property like it does a string property"() { + when: + Map lengths = [:] + manager.sessionFactory.currentSession.doWork { conn -> + def rs = conn.createStatement().executeQuery( + "select column_name, character_maximum_length from information_schema.columns " + + "where lower(table_name) = 'enum_sized_thing'") + while (rs.next()) { + lengths[rs.getString(1).toLowerCase()] = rs.getInt(2) + } + } + + then: + lengths['string_field'] == 100 + lengths['enum_field'] == 100 + } +} + +enum EnumSizedKind { ONE, TWO } + +@Entity +class EnumSizedThing { + String stringField + EnumSizedKind enumField + + static constraints = { + stringField nullable: true, maxSize: 100 + enumField nullable: true, maxSize: 100 + } +} diff --git a/grails-data-hibernate7/core/src/test/groovy/grails/gorm/tests/EnumMaxSizeColumnSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/grails/gorm/tests/EnumMaxSizeColumnSpec.groovy new file mode 100644 index 00000000000..688b7a69689 --- /dev/null +++ b/grails-data-hibernate7/core/src/test/groovy/grails/gorm/tests/EnumMaxSizeColumnSpec.groovy @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package grails.gorm.tests + +import grails.gorm.annotation.Entity +import spock.lang.Issue + +@Issue('https://github.com/apache/grails-core/issues/14505') +class EnumMaxSizeColumnSpec extends HibernateGormDatastoreSpec { + + void setupSpec() { + manager.registerDomainClasses(EnumSizedThing) + } + + void "an enum property with maxSize still stores and reads its value"() { + given: + new EnumSizedThing(stringField: 'x', enumField: EnumSizedKind.TWO).save(flush: true, failOnError: true) + session.clear() + + expect: + EnumSizedThing.findByEnumField(EnumSizedKind.TWO).enumField == EnumSizedKind.TWO + EnumSizedThing.count() == 1 + } + + void "the string property keeps the length from maxSize"() { + when: + Map lengths = [:] + manager.sessionFactory.currentSession.doWork { conn -> + def rs = conn.createStatement().executeQuery( + "select column_name, character_maximum_length from information_schema.columns " + + "where lower(table_name) = 'enum_sized_thing'") + while (rs.next()) { + lengths[rs.getString(1).toLowerCase()] = rs.getInt(2) + } + } + + then: + lengths['string_field'] == 100 + } +} + +enum EnumSizedKind { ONE, TWO } + +@Entity +class EnumSizedThing { + String stringField + EnumSizedKind enumField + + static constraints = { + stringField nullable: true, maxSize: 100 + enumField nullable: true, maxSize: 100 + } +} diff --git a/grails-test-examples/hibernate5/grails-hibernate/grails-app/domain/functional/tests/ScenarioEnumSized.groovy b/grails-test-examples/hibernate5/grails-hibernate/grails-app/domain/functional/tests/ScenarioEnumSized.groovy new file mode 100644 index 00000000000..37dd567f6fa --- /dev/null +++ b/grails-test-examples/hibernate5/grails-hibernate/grails-app/domain/functional/tests/ScenarioEnumSized.groovy @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package functional.tests + +import grails.gorm.annotation.Entity + +enum ScenarioEnumKind { ONE, TWO } + +@Entity +class ScenarioEnumSized { + + String label + ScenarioEnumKind kind + + static constraints = { + label nullable: true, maxSize: 100 + kind nullable: true, maxSize: 100 + } +} diff --git a/grails-test-examples/hibernate5/grails-hibernate/src/integration-test/groovy/functional/tests/EnumMaxSizeScenarioSpec.groovy b/grails-test-examples/hibernate5/grails-hibernate/src/integration-test/groovy/functional/tests/EnumMaxSizeScenarioSpec.groovy new file mode 100644 index 00000000000..177601d1f6d --- /dev/null +++ b/grails-test-examples/hibernate5/grails-hibernate/src/integration-test/groovy/functional/tests/EnumMaxSizeScenarioSpec.groovy @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package functional.tests + +import javax.sql.DataSource + +import org.springframework.beans.factory.annotation.Autowired +import spock.lang.Specification + +import grails.testing.mixin.integration.Integration + +/** + * maxSize on an enum property is honoured like it is on a string property (#14505). + */ +@Integration(applicationClass = Application) +class EnumMaxSizeScenarioSpec extends Specification { + + @Autowired + DataSource dataSource + + void cleanup() { + ScenarioEnumSized.withNewTransaction { + ScenarioEnumSized.list()*.delete(flush: true) + } + } + + void "an enum property with maxSize stores and reads its value"() { + given: + ScenarioEnumSized.withNewTransaction { + new ScenarioEnumSized(label: 'x', kind: ScenarioEnumKind.TWO).save(failOnError: true) + } + + expect: + ScenarioEnumSized.withNewSession { ScenarioEnumSized.findByKind(ScenarioEnumKind.TWO).kind == ScenarioEnumKind.TWO } + } + + void "the enum column is as long as maxSize says"() { + when: + Map lengths = [:] + dataSource.connection.withCloseable { conn -> + def rs = conn.createStatement().executeQuery( + "select column_name, character_maximum_length from information_schema.columns " + + "where lower(table_name) = 'scenario_enum_sized'") + while (rs.next()) { + lengths[rs.getString(1).toLowerCase()] = rs.getInt(2) + } + } + + then: + lengths['label'] == 100 + lengths['kind'] == 100 + } +} diff --git a/grails-test-examples/hibernate7/grails-hibernate/grails-app/domain/functional/tests/ScenarioEnumSized.groovy b/grails-test-examples/hibernate7/grails-hibernate/grails-app/domain/functional/tests/ScenarioEnumSized.groovy new file mode 100644 index 00000000000..37dd567f6fa --- /dev/null +++ b/grails-test-examples/hibernate7/grails-hibernate/grails-app/domain/functional/tests/ScenarioEnumSized.groovy @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package functional.tests + +import grails.gorm.annotation.Entity + +enum ScenarioEnumKind { ONE, TWO } + +@Entity +class ScenarioEnumSized { + + String label + ScenarioEnumKind kind + + static constraints = { + label nullable: true, maxSize: 100 + kind nullable: true, maxSize: 100 + } +} diff --git a/grails-test-examples/hibernate7/grails-hibernate/src/integration-test/groovy/functional/tests/EnumMaxSizeScenarioSpec.groovy b/grails-test-examples/hibernate7/grails-hibernate/src/integration-test/groovy/functional/tests/EnumMaxSizeScenarioSpec.groovy new file mode 100644 index 00000000000..9b996b42ace --- /dev/null +++ b/grails-test-examples/hibernate7/grails-hibernate/src/integration-test/groovy/functional/tests/EnumMaxSizeScenarioSpec.groovy @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package functional.tests + +import javax.sql.DataSource + +import org.springframework.beans.factory.annotation.Autowired +import spock.lang.Specification + +import grails.testing.mixin.integration.Integration + +/** + * maxSize on an enum property is honoured like it is on a string property (#14505). + */ +@Integration(applicationClass = Application) +class EnumMaxSizeScenarioSpec extends Specification { + + @Autowired + DataSource dataSource + + void cleanup() { + ScenarioEnumSized.withNewTransaction { + ScenarioEnumSized.list()*.delete(flush: true) + } + } + + void "an enum property with maxSize stores and reads its value"() { + given: + ScenarioEnumSized.withNewTransaction { + new ScenarioEnumSized(label: 'x', kind: ScenarioEnumKind.TWO).save(failOnError: true) + } + + expect: + ScenarioEnumSized.withNewSession { ScenarioEnumSized.findByKind(ScenarioEnumKind.TWO).kind == ScenarioEnumKind.TWO } + } + + void "the string column keeps its maxSize"() { + when: + Map lengths = [:] + dataSource.connection.withCloseable { conn -> + def rs = conn.createStatement().executeQuery( + "select column_name, character_maximum_length from information_schema.columns " + + "where lower(table_name) = 'scenario_enum_sized'") + while (rs.next()) { + lengths[rs.getString(1).toLowerCase()] = rs.getInt(2) + } + } + + then: + lengths['label'] == 100 + } +} From f5ada8f545e837c2301513681424cdda14961a6a Mon Sep 17 00:00:00 2001 From: Walter Duque de Estrada Date: Tue, 3 Nov 2026 15:24:40 -0600 Subject: [PATCH 2/5] fix(hibernate7): apply maxSize to the column of a string-mapped enum 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 --- .../domainbinding/binder/EnumTypeBinder.java | 8 +- .../EnumMaxSizeColumnIntegrationSpec.groovy | 93 +++++++++++++++++++ .../domainbinding/EnumTypeBinderSpec.groovy | 33 +++++++ 3 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/EnumMaxSizeColumnIntegrationSpec.groovy diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/EnumTypeBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/EnumTypeBinder.java index 0c14d5c8b24..772a7717d9b 100644 --- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/EnumTypeBinder.java +++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/EnumTypeBinder.java @@ -49,6 +49,7 @@ public class EnumTypeBinder { private final ColumnConfigToColumnBinder columnConfigToColumnBinder; private final PersistentEntityNamingStrategy namingStrategy; private final CreateKeyForProps createKeyForProps; + private final StringColumnConstraintsBinder stringColumnConstraintsBinder = new StringColumnConstraintsBinder(); public EnumTypeBinder( MetadataBuildingContext metadataBuildingContext, @@ -98,10 +99,12 @@ public BasicValue bindEnumType(@Nonnull HibernateEnumProperty property, String p Class propertyType = property.getEnumType(); PropertyConfig pc = property.getHibernateMappedForm(); String typeName = property.getTypeName(propertyType); + GrailsEnumType enumType = null; if (typeName != null) { simpleValue.setTypeName(typeName); } else { - GrailsEnumType.fromString(pc.getEnumType()).configure(simpleValue, propertyType); + enumType = GrailsEnumType.fromString(pc.getEnumType()); + enumType.configure(simpleValue, propertyType); } Properties enumProperties = new Properties(); enumProperties.put(ENUM_CLASS_PROP, propertyType.getName()); @@ -118,6 +121,9 @@ public BasicValue bindEnumType(@Nonnull HibernateEnumProperty property, String p Table t = simpleValue.getTable(); t.addColumn(column); simpleValue.addColumn(column); + if (enumType == GrailsEnumType.DEFAULT || enumType == GrailsEnumType.STRING) { + stringColumnConstraintsBinder.bindStringColumnConstraints(column, pc, null); + } createKeyForProps.createKeyForProps(property, path, t, columnName); if (!pc.getColumns().isEmpty()) { diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/EnumMaxSizeColumnIntegrationSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/EnumMaxSizeColumnIntegrationSpec.groovy new file mode 100644 index 00000000000..9ccd377a007 --- /dev/null +++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/EnumMaxSizeColumnIntegrationSpec.groovy @@ -0,0 +1,93 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.grails.orm.hibernate + +import grails.gorm.annotation.Entity +import grails.gorm.tests.HibernateGormDatastoreSpec +import org.testcontainers.mariadb.MariaDBContainer +import org.testcontainers.mysql.MySQLContainer +import org.testcontainers.postgresql.PostgreSQLContainer +import org.testcontainers.spock.Testcontainers +import spock.lang.Issue +import spock.lang.Requires +import spock.lang.Shared + +@Issue('https://github.com/apache/grails-core/issues/14505') +@Testcontainers +@Requires({ isDockerAvailable() }) +class EnumMaxSizeColumnIntegrationSpec extends HibernateGormDatastoreSpec { + + @Shared PostgreSQLContainer postgres = new PostgreSQLContainer("postgres:16") + @Shared MySQLContainer mysql = new MySQLContainer("mysql:8.0") + @Shared MariaDBContainer mariadb = new MariaDBContainer("mariadb:10.11") + + void setupSpec() { + manager.registerDomainClasses(EnumDdlThing) + } + + void "the enum column honours maxSize on #db"() { + given: + if (!container.isRunning()) { + container.start() + } + manager.destroy() + manager.grailsConfig = [ + 'dataSource.url' : container.jdbcUrl, + 'dataSource.driverClassName' : container.driverClassName, + 'dataSource.username' : container.username, + 'dataSource.password' : container.password, + 'dataSource.dbCreate' : 'create-drop', + 'hibernate.hbm2ddl.auto' : 'create', + ] + manager.setup(this.class) + + when: + Map column = [:] + datastore.dataSource.connection.withCloseable { conn -> + conn.createStatement().withCloseable { stmt -> + stmt.executeQuery("select data_type, character_maximum_length from information_schema.columns " + + "where lower(table_name) = 'enum_ddl_thing' and lower(column_name) = 'enum_field'").with { rs -> + rs.next() + column = [type: rs.getString(1), maxLength: rs.getObject(2)] + } + } + } + + then: 'a native enum has no length, any other column is as long as maxSize says' + column.type == 'enum' || (column.maxLength as Integer) == 100 + + where: + db | container + 'postgres' | postgres + 'mysql' | mysql + 'mariadb' | mariadb + } +} + +enum EnumDdlKind { ONE, TWO } + +@Entity +class EnumDdlThing { + EnumDdlKind enumField + + static constraints = { + enumField nullable: true, maxSize: 100 + } +} diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/EnumTypeBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/EnumTypeBinderSpec.groovy index 27ba192c413..a4a037c1c3c 100644 --- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/EnumTypeBinderSpec.groovy +++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/EnumTypeBinderSpec.groovy @@ -120,6 +120,25 @@ class EnumTypeBinderSpec extends HibernateGormDatastoreSpec { Person05 | UserTypeEnumType | UserTypeEnumType.class.getName() | null | false } + @Unroll + def "maxSize is applied to the column of a #mapping enum: #applied"() { + given: + def table = new Table("person") + def property = setupProperty(clazz, "status", table) + + when: + def simpleValue = binder.bindEnumType(property as HibernateEnumProperty, "") + + then: 'maxSize reaches a string-mapped enum column and leaves an ordinal one at the default' + (simpleValue.getColumns()[0].getLength() == 42L) == applied + + where: + clazz | mapping | applied + PersonSizedDefault | "default" | true + PersonSizedString | "string" | true + PersonSizedOrdinal | "ordinal" | false + } + @Unroll def "should set column nullability"() { given: "A root entity and its enum property" @@ -240,6 +259,20 @@ enum Status01 { AVAILABLE, OUT_OF_STOCK } Long id; Status01 status static constraints = { status nullable: false } } +@Entity class PersonSizedDefault { + Long id; Status01 status + static constraints = { status maxSize: 42 } +} +@Entity class PersonSizedString { + Long id; Status01 status + static mapping = { status enumType: "string" } + static constraints = { status maxSize: 42 } +} +@Entity class PersonSizedOrdinal { + Long id; Status01 status + static mapping = { status enumType: "ordinal" } + static constraints = { status maxSize: 42 } +} @Entity class Person02 { Long id; Status01 status static mapping = { status enumType: "string" } From 2c837ecfca252b63ac12f1f839433a045358f331 Mon Sep 17 00:00:00 2001 From: Walter Duque de Estrada Date: Tue, 17 Nov 2026 17:07:47 -0600 Subject: [PATCH 3/5] fix(hibernate): an enum with an inList of enum values no longer fails 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 --- .../orm/hibernate/cfg/GrailsDomainBinder.java | 16 +++++++++--- .../gorm/tests/EnumMaxSizeColumnSpec.groovy | 26 ++++++++++++++++++- .../binder/StringColumnConstraintsBinder.java | 6 ++++- .../gorm/tests/EnumMaxSizeColumnSpec.groovy | 26 ++++++++++++++++++- .../StringColumnConstraintsBinderSpec.groovy | 25 ++++++++++++++++++ 5 files changed, 92 insertions(+), 7 deletions(-) diff --git a/grails-data-hibernate5/core/src/main/groovy/org/grails/orm/hibernate/cfg/GrailsDomainBinder.java b/grails-data-hibernate5/core/src/main/groovy/org/grails/orm/hibernate/cfg/GrailsDomainBinder.java index 0405a7c58a7..46f49f261e6 100644 --- a/grails-data-hibernate5/core/src/main/groovy/org/grails/orm/hibernate/cfg/GrailsDomainBinder.java +++ b/grails-data-hibernate5/core/src/main/groovy/org/grails/orm/hibernate/cfg/GrailsDomainBinder.java @@ -3370,7 +3370,10 @@ protected void bindStringColumnConstraints(Column column, PersistentProperty con if (columnLength != null) { column.setLength(columnLength.intValue()); } else if (inListValues != null) { - column.setLength(getMaxSize(inListValues)); + int longest = getMaxSize(inListValues); + if (longest > 0) { + column.setLength(longest); + } } } @@ -3448,14 +3451,19 @@ protected int countDigits(Number number) { } /** - * @return the maximum length of the strings in the specified list + * @return the maximum length of the allowed values in the specified list that are stored as text: a string, or + * the name of an enum constant. A value of another kind, as a list that is an allowed value of a collection, + * has no length of its own and is left out. */ protected int getMaxSize(List inListValues) { int maxSize = 0; for (Object inListValue : inListValues) { - String value = (String) inListValue; - maxSize = Math.max(value.length(), maxSize); + String value = inListValue instanceof Enum constant ? constant.name() : + inListValue instanceof CharSequence text ? text.toString() : null; + if (value != null) { + maxSize = Math.max(value.length(), maxSize); + } } return maxSize; diff --git a/grails-data-hibernate5/core/src/test/groovy/grails/gorm/tests/EnumMaxSizeColumnSpec.groovy b/grails-data-hibernate5/core/src/test/groovy/grails/gorm/tests/EnumMaxSizeColumnSpec.groovy index bb562b02d71..16fdb88a96c 100644 --- a/grails-data-hibernate5/core/src/test/groovy/grails/gorm/tests/EnumMaxSizeColumnSpec.groovy +++ b/grails-data-hibernate5/core/src/test/groovy/grails/gorm/tests/EnumMaxSizeColumnSpec.groovy @@ -26,7 +26,7 @@ import spock.lang.Issue class EnumMaxSizeColumnSpec extends HibernateGormDatastoreSpec { void setupSpec() { - manager.registerDomainClasses(EnumSizedThing) + manager.registerDomainClasses(EnumSizedThing, EnumListedThing) } void "maxSize constrains the column of an enum property like it does a string property"() { @@ -45,6 +45,21 @@ class EnumMaxSizeColumnSpec extends HibernateGormDatastoreSpec { lengths['string_field'] == 100 lengths['enum_field'] == 100 } + + void "an enum property with an inList of enum values stores and reads its value"() { + given: + new EnumListedThing(kind: EnumSizedKind.ONE).save(flush: true, failOnError: true) + session.clear() + + expect: + EnumListedThing.findByKind(EnumSizedKind.ONE).kind == EnumSizedKind.ONE + EnumListedThing.count() == 1 + } + + void "an enum inList that does not allow a value rejects it"() { + expect: + !new EnumListedThing(kind: EnumSizedKind.TWO).validate() + } } enum EnumSizedKind { ONE, TWO } @@ -59,3 +74,12 @@ class EnumSizedThing { enumField nullable: true, maxSize: 100 } } + +@Entity +class EnumListedThing { + EnumSizedKind kind + + static constraints = { + kind inList: [EnumSizedKind.ONE] + } +} diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/StringColumnConstraintsBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/StringColumnConstraintsBinder.java index 00c8d4a2140..9f6d61a6708 100644 --- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/StringColumnConstraintsBinder.java +++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/StringColumnConstraintsBinder.java @@ -55,7 +55,11 @@ private static boolean isUnboundedTextType(String typeName) { } private Optional getMax(Property mappedForm) { - return Optional.ofNullable(mappedForm.getInList()).flatMap(list -> list.stream() + // an allowed value is a string only when the property is one: an enum property lists its constants, and a + // collection lists whole collections, which have no text to read + return Optional.ofNullable(mappedForm.getInList()).flatMap(list -> ((java.util.List) list).stream() + .filter(String.class::isInstance) + .map(String.class::cast) .map(this::parseInt) .filter(Objects::nonNull) .reduce(Integer::max)); diff --git a/grails-data-hibernate7/core/src/test/groovy/grails/gorm/tests/EnumMaxSizeColumnSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/grails/gorm/tests/EnumMaxSizeColumnSpec.groovy index 688b7a69689..236237cd5fd 100644 --- a/grails-data-hibernate7/core/src/test/groovy/grails/gorm/tests/EnumMaxSizeColumnSpec.groovy +++ b/grails-data-hibernate7/core/src/test/groovy/grails/gorm/tests/EnumMaxSizeColumnSpec.groovy @@ -26,7 +26,7 @@ import spock.lang.Issue class EnumMaxSizeColumnSpec extends HibernateGormDatastoreSpec { void setupSpec() { - manager.registerDomainClasses(EnumSizedThing) + manager.registerDomainClasses(EnumSizedThing, EnumListedThing) } void "an enum property with maxSize still stores and reads its value"() { @@ -54,6 +54,21 @@ class EnumMaxSizeColumnSpec extends HibernateGormDatastoreSpec { then: lengths['string_field'] == 100 } + + void "an enum property with an inList of enum values stores and reads its value"() { + given: + new EnumListedThing(kind: EnumSizedKind.ONE).save(flush: true, failOnError: true) + session.clear() + + expect: + EnumListedThing.findByKind(EnumSizedKind.ONE).kind == EnumSizedKind.ONE + EnumListedThing.count() == 1 + } + + void "an enum inList that does not allow a value rejects it"() { + expect: + !new EnumListedThing(kind: EnumSizedKind.TWO).validate() + } } enum EnumSizedKind { ONE, TWO } @@ -68,3 +83,12 @@ class EnumSizedThing { enumField nullable: true, maxSize: 100 } } + +@Entity +class EnumListedThing { + EnumSizedKind kind + + static constraints = { + kind inList: [EnumSizedKind.ONE] + } +} diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/StringColumnConstraintsBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/StringColumnConstraintsBinderSpec.groovy index 8006ced8ba2..0c28a66b1b5 100644 --- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/StringColumnConstraintsBinderSpec.groovy +++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/StringColumnConstraintsBinderSpec.groovy @@ -101,6 +101,31 @@ class StringColumnConstraintsBinderSpec extends Specification { } + def "should leave the column length when the allowed values are not strings"() { + given: 'an enum property lists its constants, and a collection property lists whole collections' + mappedForm.getMaxSize() >> null + mappedForm.getInList() >> [java.util.concurrent.TimeUnit.SECONDS, ['abcdef'], 7] + def originalLength = column.length + + when: + binder.bindStringColumnConstraints(column, mappedForm, null) + + then: + column.length == originalLength + } + + def "should read the strings of a list that has other values too"() { + given: + mappedForm.getMaxSize() >> null + mappedForm.getInList() >> [java.util.concurrent.TimeUnit.SECONDS, '9', ['abcdef']] + + when: + binder.bindStringColumnConstraints(column, mappedForm, null) + + then: + column.length == 9 + } + def "should prioritize maxSize over inList when both are present"() { given: mappedForm.getMaxSize() >> 1 From 354a72ac12686f9e6bf34dc60066e10b60b855ad Mon Sep 17 00:00:00 2001 From: Walter Duque de Estrada Date: Tue, 24 Nov 2026 22:04:57 -0600 Subject: [PATCH 4/5] fix(hibernate7): the maxSize of a collection of enum values is not the length of the column of one value Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL --- .../domainbinding/binder/EnumTypeBinder.java | 5 +++- .../gorm/tests/EnumMaxSizeColumnSpec.groovy | 27 ++++++++++++++++++- .../domainbinding/EnumTypeBinderSpec.groovy | 7 ++++- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/EnumTypeBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/EnumTypeBinder.java index 772a7717d9b..bccf528f05b 100644 --- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/EnumTypeBinder.java +++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/EnumTypeBinder.java @@ -32,6 +32,7 @@ import org.grails.orm.hibernate.cfg.ColumnConfig; import org.grails.orm.hibernate.cfg.PersistentEntityNamingStrategy; import org.grails.orm.hibernate.cfg.PropertyConfig; +import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateBasicProperty; import org.grails.orm.hibernate.cfg.domainbinding.hibernate.HibernateEnumProperty; import org.grails.orm.hibernate.cfg.domainbinding.util.ColumnNameForPropertyAndPathFetcher; import org.grails.orm.hibernate.cfg.domainbinding.util.CreateKeyForProps; @@ -121,7 +122,9 @@ public BasicValue bindEnumType(@Nonnull HibernateEnumProperty property, String p Table t = simpleValue.getTable(); t.addColumn(column); simpleValue.addColumn(column); - if (enumType == GrailsEnumType.DEFAULT || enumType == GrailsEnumType.STRING) { + // on a collection of enum values the constraints are those of the collection: maxSize is the number of its values, + // and an allowed value is a whole collection, so neither is the length of the column of one value + if ((enumType == GrailsEnumType.DEFAULT || enumType == GrailsEnumType.STRING) && !(property instanceof HibernateBasicProperty)) { stringColumnConstraintsBinder.bindStringColumnConstraints(column, pc, null); } createKeyForProps.createKeyForProps(property, path, t, columnName); diff --git a/grails-data-hibernate7/core/src/test/groovy/grails/gorm/tests/EnumMaxSizeColumnSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/grails/gorm/tests/EnumMaxSizeColumnSpec.groovy index 236237cd5fd..64fb3ece664 100644 --- a/grails-data-hibernate7/core/src/test/groovy/grails/gorm/tests/EnumMaxSizeColumnSpec.groovy +++ b/grails-data-hibernate7/core/src/test/groovy/grails/gorm/tests/EnumMaxSizeColumnSpec.groovy @@ -26,7 +26,7 @@ import spock.lang.Issue class EnumMaxSizeColumnSpec extends HibernateGormDatastoreSpec { void setupSpec() { - manager.registerDomainClasses(EnumSizedThing, EnumListedThing) + manager.registerDomainClasses(EnumSizedThing, EnumListedThing, EnumCollectionThing) } void "an enum property with maxSize still stores and reads its value"() { @@ -65,6 +65,18 @@ class EnumMaxSizeColumnSpec extends HibernateGormDatastoreSpec { EnumListedThing.count() == 1 } + void "a maxSize on a collection of enum values limits the number of values and not the length of one"() { + given: 'a collection with maxSize 2 of values that are longer than two characters' + new EnumCollectionThing(kinds: [EnumLongKind.FIRST_VALUE, EnumLongKind.SECOND_VALUE]).save(flush: true, failOnError: true) + session.clear() + + expect: 'both values are stored and read back' + EnumCollectionThing.list()[0].kinds as Set == [EnumLongKind.FIRST_VALUE, EnumLongKind.SECOND_VALUE] as Set + + and: 'a third value is refused by the constraint of the collection' + !new EnumCollectionThing(kinds: [EnumLongKind.FIRST_VALUE, EnumLongKind.SECOND_VALUE, EnumLongKind.THIRD_VALUE]).validate() + } + void "an enum inList that does not allow a value rejects it"() { expect: !new EnumListedThing(kind: EnumSizedKind.TWO).validate() @@ -92,3 +104,16 @@ class EnumListedThing { kind inList: [EnumSizedKind.ONE] } } + +enum EnumLongKind { FIRST_VALUE, SECOND_VALUE, THIRD_VALUE } + +@Entity +class EnumCollectionThing { + Set kinds + + static hasMany = [kinds: EnumLongKind] + + static constraints = { + kinds maxSize: 2 + } +} diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/EnumTypeBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/EnumTypeBinderSpec.groovy index a4a037c1c3c..5b720f67370 100644 --- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/EnumTypeBinderSpec.groovy +++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/EnumTypeBinderSpec.groovy @@ -121,7 +121,7 @@ class EnumTypeBinderSpec extends HibernateGormDatastoreSpec { } @Unroll - def "maxSize is applied to the column of a #mapping enum: #applied"() { + def "maxSize is applied to the column of a #mapping: #applied"() { given: def table = new Table("person") def property = setupProperty(clazz, "status", table) @@ -137,6 +137,7 @@ class EnumTypeBinderSpec extends HibernateGormDatastoreSpec { PersonSizedDefault | "default" | true PersonSizedString | "string" | true PersonSizedOrdinal | "ordinal" | false + PersonSizedCollection | "collection of enum values, where maxSize is the number of values" | false } @Unroll @@ -317,3 +318,7 @@ class UserTypeEnumType implements UserType { @Override Serializable disassemble(Object value) { (Serializable)value } @Override Object assemble(Serializable cached, Object owner) { cached } } +@Entity class PersonSizedCollection { + Long id; Set status + static constraints = { status maxSize: 42 } +} From 312255f7d1d59dd217dddc3eb4ab2a8f626e92fe Mon Sep 17 00:00:00 2001 From: Walter Duque de Estrada Date: Wed, 25 Nov 2026 02:01:11 -0600 Subject: [PATCH 5/5] fix(hibernate7): the column of an enum with a maxSize holds the longest name of the enum at least Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL --- .../cfg/domainbinding/binder/EnumTypeBinder.java | 9 +++++++++ .../cfg/domainbinding/EnumTypeBinderSpec.groovy | 16 ++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/EnumTypeBinder.java b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/EnumTypeBinder.java index bccf528f05b..1fd26dcc2cd 100644 --- a/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/EnumTypeBinder.java +++ b/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/EnumTypeBinder.java @@ -126,6 +126,15 @@ public BasicValue bindEnumType(@Nonnull HibernateEnumProperty property, String p // and an allowed value is a whole collection, so neither is the length of the column of one value if ((enumType == GrailsEnumType.DEFAULT || enumType == GrailsEnumType.STRING) && !(property instanceof HibernateBasicProperty)) { stringColumnConstraintsBinder.bindStringColumnConstraints(column, pc, null); + // maxSize does not validate an enum value, so a value whose name is longer than the column would pass validation and + // fail in the database: the column holds the longest name of the enum at least + int longestName = 0; + for (Object constant : propertyType.getEnumConstants()) { + longestName = Math.max(longestName, ((Enum) constant).name().length()); + } + if (column.getLength() != null && column.getLength() < longestName) { + column.setLength((long) longestName); + } } createKeyForProps.createKeyForProps(property, path, t, columnName); diff --git a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/EnumTypeBinderSpec.groovy b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/EnumTypeBinderSpec.groovy index 5b720f67370..c3aabddd657 100644 --- a/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/EnumTypeBinderSpec.groovy +++ b/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/cfg/domainbinding/EnumTypeBinderSpec.groovy @@ -120,6 +120,18 @@ class EnumTypeBinderSpec extends HibernateGormDatastoreSpec { Person05 | UserTypeEnumType | UserTypeEnumType.class.getName() | null | false } + def "a maxSize shorter than the longest constant name does not make the column too short for a valid value"() { + given: 'an enum whose longest constant, OUT_OF_STOCK, is 12 characters, with a maxSize of 2' + def table = new Table("person") + def property = setupProperty(PersonSizedShort, "status", table) + + when: + def simpleValue = binder.bindEnumType(property as HibernateEnumProperty, "") + + then: 'the column holds the longest name, as maxSize does not validate an enum' + simpleValue.getColumns()[0].getLength() == 12L + } + @Unroll def "maxSize is applied to the column of a #mapping: #applied"() { given: @@ -322,3 +334,7 @@ class UserTypeEnumType implements UserType { Long id; Set status static constraints = { status maxSize: 42 } } +@Entity class PersonSizedShort { + Long id; Status01 status + static constraints = { status maxSize: 2 } +}