mysql – 使用TEXT字段将Grails Domain类映射到旧数据库时出现问题

我正在尝试使用Grails 1.3.7和
MySQL 5.1.56为遗留数据库构建一组域类.我将BuildConfig.groovy文件中的MySQL连接器指定为’mysql:mysql-connector-java:5.1.13′.

数据库模式有一个名为’abstract’的TEXT类型的字段.

我在我的班级中声明相应的属性如下(为清楚起见,仅显示相关部分):

class Paper {
    String abstractText

    static mapping = {
        table 'papers'
        abstractText column: 'abstract'
    }

    static constraints = {
        abstractText(nullable: false, maxSize: 65535)
    }
}

当我运行集成测试时,我收到以下错误:

Wrong column type in citeseerx.papers for column abstract. 
Found: text, expected: longtext

如果我改变声明

    static mapping = {
        abstractText column: 'abstract', type: 'text'
    }

我犯了同样的错误.如果我将类型设置为’longtext’,我会得到

Could not determine type for: longtext, at table: papers, 
for columns: [org.hibernate.mapping.Column(abstract)]

我看到了一个看似相关的Hibernate bug的讨论,我想知道是否有一个解决方法,或者其他一些方法来建模具有TEXT字段的模式.

谢谢,

基因

编辑:这是相关的DataSource.groovy片段:

dataSource {
    pooled = true
    driverClassName = "com.mysql.jdbc.Driver"
    url = "jdbc:mysql://mydbhost:3306/mydb"
    username = "u"
    password = "p"
    dbCreate = 'validate'
    //dialect = org.hibernate.dialect.MySQL5Dialect
    dialect = com.fxpal.citeseer.mysql.MyMySQLDialect
    println("Setting dialog = ${dialect}")  
}

hibernate {
    cache.use_second_level_cache = true
    cache.use_query_cache = true
    cache.provider_class = 'net.sf.ehcache.hibernate.EhCacheProvider'
}

编辑(2):这是自定义方言类,正如@Stefan的答案所示:

import java.sql.Types;

/**
 * An An extension to the SQL dialect for MySQL 5.1 to handle TEXT.
 *
 * @author Gene Golovchinsky
 */
public class MyMySQLDialect extends org.hibernate.dialect.MySQLDialect {

    public MyMySQLDialect() {
        super();
        System.out.println("MyMySQLDialect: created new instance");
    }

    protected void registerVarcharTypes() {
        System.out.println("MyMySQLDialect: RegisteringVarcharTypes");
        registerColumnType( Types.VARCHAR, 16777215, "mediumtext" );
        registerColumnType( Types.VARCHAR, 65535, "text" );
        registerColumnType( Types.VARCHAR, 255, "varchar($l)" );
        registerColumnType( Types.LONGVARCHAR, "longtext" );
    }
}

最佳答案 您可以从
https://github.com/hibernate/hibernate-core/blob/master/hibernate-core/src/main/java/org/hibernate/dialect/MySQLDialect.java派生自定义方言并更改“文本”的映射.然后使用“dialect =”设置将新方言应用于Datasource.groovy.

点赞