GORM: mapping large text fields database agnostically

后端 未结 1 1468
灰色年华
灰色年华 2021-01-07 05:07

I have a Grails application that will run against either a SQL Server or Oracle backend. I am using GORM as an ORM.

I want to map a large text field in a way that su

相关标签:
1条回答
  • 2021-01-07 06:03

    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
            }
    
        }
    }
    
    0 讨论(0)
提交回复
热议问题