gorm mysql text,GORM:无法定位大型文本字段数据库

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 supports both database types. In my Grails domain class I have something like:

class Note {

String content

static constraints = {

content nullable: false, blank: false

}

}

I then declare database tables that look like this:

-- oracle

CREATE TABLE NOTE

(

id NUMBER(19, 0) NOT NULL,

version NUMBER(19, 0) NOT NULL,

content CLOB NOT NULL

);

-- SQL Server

CREATE TABLE NOTE

(

id NUMERIC(19, 0) NOT NULL,

version NUMERIC(19, 0) NOT NULL,

content NVARCHAR(MAX) NOT NULL

);

GORM is running in validate mode on startup, and I can't find a combination of Oracle and SQL Server data types and GORM mappings that allow the storage or large text fields without GORM failing to start correctly.

I have tried:

setting the type to text in mappings, but this doesn't seem to work. Oracle complains about expecting the content field to be of type long, and SQL Server wants a type of text in these circumstances.

setting the type to clob, which passes schema validation but then doesn't allow me to set the field as a string value - GORM expects data of type CLOB.

How should I configure my database definitions and GORM to make this work?

解决方案

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值