问题描述
我有一个可以针对SQL Server或Oracle后端运行的Grails应用程序。我使用GORM作为ORM。
我想以支持两种数据库类型的方式映射大型文本字段。在我的Grails领域类中,我有类似的东西:
class Note {
字符串内容
static constraint = {
content nullable:false,blank:false
}
}
然后我声明如下的数据库表:
- 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在启动时运行于 validate
模式,而且我无法找到Oracle和SQL Server数据类型和GORM映射的组合,这些映射允许没有GORM的存储或大型文本字段无法正确启动。
I已尝试: 在<$ c中将类型设置为 我应该如何配置我的数据库定义和GORM才能使其工作?
text
$ c>映射,但这似乎不起作用。 Oracle抱怨期望 content
字段的类型为 long
,并且SQL Server需要一种<$ c $在这些情况下,你可以使用 CLOB $ c
最终出现了一种解决方案:通过在启动时查询Grails配置,您可以选择适当的数据类型。
class Note {
字符串内容
静态约束= {
content nullable:false,blank:false
}
static mappings = {
content sqlType:DbSupport.bigStringType
}
}
class DbSupport {
static def getBigStringType(){
//检查选择哪个hibernate方言,并选择
/ /该数据库类型的适当类型映射:
def dialect = ApplicationHolder.application.config.dataSource.dialect
switch(dialect){
caseorg.hibernate.dialect .SQLServerDialect:
returnnvarchar
break
caseorg.hibernate.dialect.O racle10gDialect:
returnclob
break
}
}
}
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
inmappings
, but this doesn't seem to work. Oracle complains about expecting thecontent
field to be of typelong
, and SQL Server wants a type oftext
in these circumstances.setting the
type
toclob
, which passes schema validation but then doesn't allow me to set the field as a string value - GORM expects data of typeCLOB
.
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
}
}
}
这篇关于GORM:无法定位大型文本字段数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!