How do I get Grails to map my String field to a Clob?

时光毁灭记忆、已成空白 提交于 2019-12-22 05:14:23

问题


My domain class looks like this:

package com.initech.tps

class Foo
{
    String stuff

    static mapping = {
        // mapping to a legacy table as opposed to letting Grails create it
        table name: 'FOO', schema: 'TPS'
        id generator: 'sequence', params: [sequence: 'MY_SEQ'], 
            column: 'FOO_ID', sqlType: 'integer' 
        foo column: 'STUFF'
    }

    static constraints = {
        stuff(nullable: true, maxSize: 40000)
    }
} 

I was under the impression Grails would figure out to use a CLOB instead of a VARCHAR based on my passing in a big enough value for the maxSize constraint, instead I get this error message in the console:

org.hibernate.HibernateException: Wrong column type in FOO for column STUFF. 
Found: clob, expected: varchar(40000)

Do I need an explicit sqlType on the mapping? I tried using different values of maxSize, and leaving it out altogether, with no difference. Also adding sqlType: clob or sqlType: text doesn't work.

I'm on Grails 1.3.7, using IBM DB2-Express.


回答1:


Found the answer. Nothing like reading the documentation to find something out.

The change that worked was changing the mapping for the clob column to

foo column: 'STUFF', type: "text"



回答2:


I know it's too late, but for future references:

you just has to add a mapping

    static mapping = {  stuff type:'text' }

And that's all, grails change the data type to clob or any other that can hold large strings based in the database you're using.




回答3:


I used " Clob data" in my Grails project and that worked fine.

Can you use Clob ?

class Foo
{
 Clob stuff
 }



回答4:


Although actual Oracle DB column type is CLOB, type:'text' didn't help when dbCreate is set to validate in DataSource.groovy.

But this works for my Grails 2.2.0:

static mapping = { stuff sqlType: 'clob' }


来源:https://stackoverflow.com/questions/9268777/how-do-i-get-grails-to-map-my-string-field-to-a-clob

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!