在Grails 2.0.1中使用Oracle 10g CLOB [英] Using Oracle 10g CLOB with Grails 2.0.1

查看:106
本文介绍了在Grails 2.0.1中使用Oracle 10g CLOB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Oracle 10g和Grails v2.0.1开展一个项目。



我试图在文本输入字段中使用CLOB数据类型我的域类,它似乎并没有工作。我的第一次尝试是基于我读过的这里GORM ,这里说的是使用类型:'text',就像这个例子:

<$ p $类型:'text'
}
字符串数字
字符串postCode
静态映射= {
}

Grails将其映射到 LONG 数据键入我的数据库,这是不可取的



第二次尝试是尝试键入:'clob'。这有效地使我的DB数据类型成为CLOB,但导致类转换错误,因为属性本​​身被定义为字符串,即 String postCode 。 (请注意,我从来没有在文档中看到类型:'clob',但是我可以从方言类推断出 clob 可能是一个有效的输入)



我后来尝试将该属性定义为 java.sql.Clob ,即 Clob postCode; ,并且根本不起作用。没有错误消息,但是没有任何东西被持久保存到数据库中。



我最后一步保持 Clob 方法,但是使用一个瞬态 String 属性,其中getters / setters尝试将瞬态字符串值映射到持久性Clob字段。但我无法弄清楚如何让我的字符串值进入Clob。 Grails不会抛出错误,但是我的尝试赋值之后的 println()不会打印。我已经尝试使用 myClob.setString(1,theString)作出任务,但没有成功。

所以简而言之,我似乎无法在我的场景中使用Clob,而且我想知道是否有其他人看到了这一点,并能够克服它。如果是这样,你能告诉我我可能做错了什么?



或...有没有一种方法可以覆盖Grails选择的数据类型,以便我可以强制它map postCode类型:'text'转换为 CLOB ?我不熟悉Hibernate,所以我不知道如何去解决这个问题。



附注:在我们从Grails 1.3升级之前。 7到2.0.1,我很确定类型:'text'确实映射到Oracle中的CLOB。所以这可能是2.0.1的新功能。

解决方案

I think 我找到了一个答案有关自定义Hibernate类型的文档。


在这种情况下,请使用sqlType属性重写列的SQL类型

这似乎是工作。

看起来我可以使用它来强制我的数据库类型为CLOB,同时保持Java类型为String。换句话说,也许 type 选择数据库类型和Java类型来处理字段?但是 sqlType 给出了更精细的指定要使用的数据库类型。

因此,上面的示例Domain类应该看起来像这样在我的情况:

  class地址{
字符串编号
字符串postCode
静态映射= {
postCode sqlType:'clob'
}
}







我花了一天的时间试图弄清楚这一切,令人难以置信的令人沮丧。所以也许我关于这个话题的笔记会帮助其他人避免这种体验!






虽然我在这里记笔记...这篇文章在解决问题方面证明了一些用处,如何在我的映射中获得更具体的内容: //omaha-seattle.blogspot.com/2010/02/grails-hibernate-custom-data-type.htmlrel =nofollow noreferrer> http://omaha-seattle.blogspot.com/2010/02/grails -hibernate-custom-data-type.html


有趣的代码来自这里:

  // CONFIG.GROOVY(映射一个自定义的SixDecimal类型)
grails.gorm.default.mapping = {
'user -type'(类型:SixDecimalUserType,class:SixDecimal)
}


I'm working on a project using Oracle 10g and Grails v2.0.1.

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 mapped that to a LONG data type in my DB, which is not desirable

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)

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.

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.

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... 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.

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.

解决方案

I think I found an answer tucked into the documentation on Custom Hibernate Types.

In that case, override the column's SQL type using the sqlType attribute

This appears to be working.

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'
    }
} 

I gleaned this from another StackOverflow question on the topic (the question itself clued me in, whereas the accepted answer misled me!):

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:

Interesting code from that is reproduced here:

//CONFIG.GROOVY (maps a custom SixDecimal type)
grails.gorm.default.mapping = {
    'user-type'( type: SixDecimalUserType, class: SixDecimal )
}

这篇关于在Grails 2.0.1中使用Oracle 10g CLOB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆