在 Grails 2.0.1 中使用 Oracle 10g CLOB
我正在处理一个使用 Oracle 10g 和 Grails v2.0.1 的项目.
I'm working on a project using Oracle 10g and Grails v2.0.1.
我正在尝试将 CLOB 数据类型用于我的域类中的文本输入字段,但它似乎不起作用.我的第一次尝试基于我阅读的内容 here aboutGORM,这里说的是使用type: 'text'
,就像这个例子:
I'm trying to use a CLOB data type for a text input field in my Domain class, and it doesn't seem to be working. My first attempt was based on what I read here about GORM, where is says to use type: 'text'
, like this example:
class Address {
String number
String postCode
static mapping = {
postCode type: 'text'
}
}
Grails 将其映射到我的数据库中的 LONG
数据类型,即 不可取
Grails mapped that to a LONG
data type in my DB, which is not desirable
第二次尝试是尝试 type: 'clob'
.这在使我的 DB 数据类型为 CLOB 方面很有效,但由于属性本身被定义为字符串,即 String postCode
,因此导致了类转换错误.(请注意,我从未在文档中看到 type:'clob'
,但我可以从方言类推断 clob
可能是那里的有效输入)
2nd attempt was to try type: 'clob'
. That WAS effective in getting my DB datatype to be CLOB, but resulted in a class cast error because the property itself was defined as a string, i.e. String postCode
. (Note that I've never seen type:'clob'
in documentation, but I could deduce from the dialect class that clob
might be a valid input there)
我随后尝试将该属性定义为 java.sql.Clob
,即 Clob postCode;
,但根本不起作用.没有错误消息,但也没有任何东西被持久化到数据库中.
I subsequently tried defining the property as a java.sql.Clob
, i.e. Clob postCode;
, and that didn't work at all. No error messages, but nothing was getting persisted to the DB either.
我采取了保持 Clob
方法的最后一步,但使用瞬态 String
属性,其中 getter/setter 尝试将瞬态 String 值映射到持久性Clob 领域.但我不知道如何将我的字符串值放入 Clob.Grails 不会抛出错误,但是在我尝试赋值之后的 println()
永远不会打印.我尝试使用 myClob.setString(1, theString)
进行赋值,但没有成功.
I took a final step of keeping the Clob
approach, but using a transient String
property in which the getters/setters attempt to map the transient String value to the persistent Clob field. But I cannot figure out how to get my string value into the Clob. Grails doesn't throw an error, but the println()
after my attempted assignment never prints. I've tried using myClob.setString(1, theString)
to make an assignment, but with no success.
长话短说,我似乎无法在我的场景中使用 Clob,我想知道是否有其他人已经看到并能够克服它.如果是这样,你能告诉我我可能做错了什么吗?
So to make a long story short, I can't seem to use a Clob in my scenario, and I'm wondering if anyone else has seen this and been able to overcome it. If so, can you tell me what I might be doing wrong?
OR... 有没有办法覆盖 Grails 选择的数据类型,以便我可以强制它将 postCode type: 'text'
映射到 CLOB
?我不精通 Hibernate,所以如果有办法,我不确定如何去做.
OR... is there a way to override the datatype Grails chooses such that I could force it to map postCode type: 'text'
to a CLOB
? I'm not proficient with Hibernate, so I'm not sure how to go about that if there's a way.
旁注:在我们从 Grails 1.3.7 升级到 2.0.1 之前,我很确定 type: 'text'
实际上确实映射到 Oracle 中的 CLOB.所以这可能是 2.0.1 的新内容.
Side note: prior to our upgrade from Grails 1.3.7 to 2.0.1, I'm pretty sure the type: 'text'
did, in fact, map to a CLOB in Oracle. So this might be new to 2.0.1.
推荐答案
我认为我在自定义休眠类型.
在这种情况下,使用 sqlType 属性覆盖列的 SQL 类型
In that case, override the column's SQL type using the sqlType attribute
这似乎有效.
看起来我可以使用它来强制我的数据库类型为 CLOB,同时保持 java 类型为字符串.换句话说,也许 type
会同时选择 DB 类型和 Java 类型来处理该字段?但是 sqlType
提供了更多的粒度来指定要使用的数据库类型.
Looks like I'm able to use that to force my DB type to be CLOB while keeping the java type a String. In other words, maybe type
chooses both a DB type and a Java type for handling the field? But sqlType
gives a little more granularity to specify the DB type to use.
所以上面的示例域类在我的例子中应该是这样的:
So the sample Domain class above should look like this in my case:
class Address {
String number
String postCode
static mapping = {
postCode sqlType: 'clob'
}
}
我从关于该主题的另一个 StackOverflow 问题中收集到这一点(问题本身暗示了我,而接受的答案误导了我!):
I gleaned this from another StackOverflow question on the topic (the question itself clued me in, whereas the accepted answer misled me!):
- 我该怎么做让 Grails 将我的 String 字段映射到 Clob?
我花了一天的时间试图弄清楚这一切,这令人非常沮丧.因此,也许我在此主题上的笔记将帮助其他人避免这种体验!
I spent a day trying to figure this all out, and it was incredibly frustrating. So maybe my notes on the topic here will help someone else avoid that experience!
虽然我在这里做笔记...这篇文章在解决如何在我的映射中获得更具体的问题方面被证明是有用的:
And while I'm keeping notes here... this post proved somewhat useful in terms of troubleshooting how to get more specific in my mappings:
- http://omaha-seattle.blogspot.com/2010/02/grails-hibernate-custom-data-type.html
此处复制了有趣的代码:
Interesting code from that is reproduced here:
//CONFIG.GROOVY (maps a custom SixDecimal type)
grails.gorm.default.mapping = {
'user-type'( type: SixDecimalUserType, class: SixDecimal )
}
相关文章