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

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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?
could you plz post the datasources.groovy snippet?
I added it to my question. Thanks for your help!
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.
Part of the problem was that I needed to clean the project in the IDE; apparently grails' clean wasn't flushing everything.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.