简体   繁体   中英

Problem mapping Grails Domain class to legacy database with TEXT field

I am trying to build a set of domain classes for a legacy database using Grails 1.3.7 and MySQL 5.1.56. I am specifying the MySQL connector in the BuildConfig.groovy file as 'mysql:mysql-connector-java:5.1.13'.

The database schema has a field named 'abstract' of type TEXT.

I am declaring the corresponding property in my class as follows (only relevant parts shown for clarity):

class Paper {
    String abstractText

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

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

When I run my integration test, I get the following error:

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

If I change the declaration to be

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

I get the same error. If I set the type to 'longtext', I get

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

I saw a discussion of a seemingly-related Hibernate bug , and I am wondering if there is a work-around for it, or some other way of modeling schemas that have TEXT fields.

Thanks,

Gene

EDITED: Here is the relevant DataSource.groovy snippet:

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'
}

EDITED(2): Here is the Custom Dialect class, as suggested by @Stefan's answer:

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" );
    }
}

You could probably derive a custom dialect from https://github.com/hibernate/hibernate-core/blob/master/hibernate-core/src/main/java/org/hibernate/dialect/MySQLDialect.java and change the mapping for 'text'. Then apply the new dialect to Datasource.groovy using the "dialect = " setting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM