Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.hibernate.mapping.Formula;
import org.hibernate.mapping.SimpleValue;
import org.hibernate.mapping.Table;
import org.hibernate.type.SqlTypes;
import org.jspecify.annotations.NonNull;

import org.grails.datastore.mapping.model.types.TenantId;
Expand Down Expand Up @@ -84,7 +85,20 @@ public SimpleValue bindSimpleValue(
String path) {

PropertyConfig propertyConfig = property.getHibernateMappedForm();
simpleValue.setTypeName(property.getTypeName(simpleValue));
String typeName = property.getTypeName(simpleValue);
if (isUnboundedTextType(typeName) && simpleValue instanceof BasicValue basicValue) {
// Hibernate's legacy named-type lookup resolves "text" to StandardBasicTypes.TEXT,
// whose JDBC type code is the legacy java.sql.Types.LONGVARCHAR. Dialects (e.g.
// Postgres) don't render that legacy code as their native unbounded text/CLOB type,
// falling back instead to a bounded VARCHAR at Hibernate's generic Length.LONG
// default (32600) when no explicit column length is set - see GH-16010. The modern
// SqlTypes.LONG32VARCHAR code is what dialects actually map to an unbounded type, so
// bind that directly rather than going through the ambiguous legacy type name.
basicValue.setExplicitJdbcTypeAccess(

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.

Isn't this dialect specific? This silently changes the DDL of type: 'text' on H2, MySQL/MariaDB, Oracle, and SQL Server:

  • H2 (the default test/dev DB): LONG32VARCHAR → character large object/clob rather than whatever LONGVARCHAR rendered before.
  • Oracle: → clob instead of the legacy path.
  • SQL Server: → varchar(max).
  • MySQL: → longtext.

i think this is ok, but I think in hiberate5 it was only supported if the dialect supported it.

typeConfiguration -> typeConfiguration.getJdbcTypeRegistry().getDescriptor(SqlTypes.LONG32VARCHAR));
} else {
simpleValue.setTypeName(typeName);
}
simpleValue.setTypeParameters(property.getTypeParameters(simpleValue));

if (propertyConfig.isDerived() && !(property instanceof TenantId)) {
Expand Down Expand Up @@ -112,4 +126,8 @@ public SimpleValue bindSimpleValue(
}
return simpleValue;
}

private static boolean isUnboundedTextType(String typeName) {
return "text".equalsIgnoreCase(typeName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* 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.hibernate.HibernateEntity
import grails.gorm.tests.HibernateGormDatastoreSpec
import org.testcontainers.containers.PostgreSQLContainer
import org.testcontainers.spock.Testcontainers
import spock.lang.Requires
import spock.lang.Shared

/**
* Reproduces https://github.com/apache/grails-core/issues/16010: a property mapped with
* {@code type: 'text'} must produce a genuinely unbounded {@code text} column on Postgres,
* not a bounded {@code varchar(n)} that can fail to accommodate existing data on schema update.
*/
@Testcontainers
@Requires({ isDockerAvailable() })
class GormTextTypeColumnIntegrationSpec extends HibernateGormDatastoreSpec {

@Shared PostgreSQLContainer postgres = new PostgreSQLContainer("postgres:16")

@Override
void setupSpec() {
manager.grailsConfig = [
'dataSource.url' : postgres.jdbcUrl,
'dataSource.driverClassName' : postgres.driverClassName,
'dataSource.username' : postgres.username,
'dataSource.password' : postgres.password,
'dataSource.dbCreate' : 'create-drop',
'hibernate.dialect' : 'org.hibernate.dialect.PostgreSQLDialect',
'hibernate.hbm2ddl.auto' : 'create',
]
manager.registerDomainClasses(TextTypeMessage)
}

void "a property mapped with type 'text' produces an unbounded text column on Postgres"() {
when:
Map<String, Object> column
datastore.dataSource.connection.withCloseable { conn ->
conn.createStatement().withCloseable { stmt ->
stmt.executeQuery('''
select data_type, character_maximum_length
from information_schema.columns
where table_name = 'text_type_message' and column_name = 'body'
'''.stripIndent()).with { rs ->
rs.next()
column = [dataType: rs.getString('data_type'), maxLength: rs.getObject('character_maximum_length')]
}
}
}

then:
column.dataType == 'text'
column.maxLength == null
}
}

@Entity
class TextTypeMessage implements HibernateEntity<TextTypeMessage> {
String body

static mapping = {
body type: 'text'
}
}
Loading