GORM: mapping large text fields database agnostically

↘锁芯ラ 提交于 2019-12-01 00:35:22

As hackish as it is, a solution eventually emerged: by querying the Grails configuration at startup time, you can select an appropriate data type.

class Note {

    String content

    static constraints = {
        content nullable: false, blank: false
    }

    static mappings = {
        content sqlType: DbSupport.bigStringType
    }
}

class DbSupport {

    static def getBigStringType() {

        // examine which hibernate dialect is selected, and pick
        // an appropriate type mapping for that database type:
        def dialect = ApplicationHolder.application.config.dataSource.dialect
        switch (dialect) {

            case "org.hibernate.dialect.SQLServerDialect":
                return "nvarchar"
                break

            case "org.hibernate.dialect.Oracle10gDialect":
                return "clob"
                break
        }

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