4

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" );
    }
}
Gene Golovchinsky
  • 6,101
  • 7
  • 53
  • 81

1 Answers1

2

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.

Stefan Armbruster
  • 39,465
  • 6
  • 87
  • 97
  • I am trying to change the dialect by setting the dialect property in the DataSource.groovy dataSource closure, but it doesn't appear to get instantiated. (I put a print statement into the constructor and don't see the corresponding output before the stack trace.) I also tried setting the dialect to MySQL5Dialect, and finally I set the hibernate logging level to trace, which produced a bunch of "Binding property" statements, but nothing indicative of the error. What else should I try to make sure the dialect setting is respected? – Gene Golovchinsky Jul 05 '11 at 20:20
  • could you plz post the datasources.groovy snippet? – Stefan Armbruster Jul 05 '11 at 20:26
  • I added it to my question. Thanks for your help! – Gene Golovchinsky Jul 05 '11 at 21:02
  • I'm not sure but this might be a classloading issue, I assume the given dialect class is not found. To verify the assumption, could you place a typo in the classname. – Stefan Armbruster Jul 05 '11 at 21:50
  • It fails to find the class when I misspell it, and reports the dialect as an empty map ([:]). No other difference in execution is apparent form the trace. – Gene Golovchinsky Jul 05 '11 at 21:55
  • 1
    Part of the problem was that I needed to clean the project in the IDE; apparently grails' clean wasn't flushing everything. – Gene Golovchinsky Jul 06 '11 at 22:28