oracle 无法在数据库中保存 clob 数据类型(Struts、Spring、Hibernate)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2115420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 19:50:52  来源:igfitidea点击:

Cannot save clob data type in database (Struts, Spring, Hibernate)

javaoraclehibernatespringclob

提问by ASC

@Column(name="transpired")
@Lob
private String transpired;
public String getTranspired() {
    return transpired;
}
public void setTranspired(String transpired) {
    this.transpired = transpired;
}

I tried using the following code in our model class. Transpired is a field with long text messages (reports). When viewing the "report", it retrieves the data from the database and displays it correctly in our UI. However, when I'm saving (upon editing or creating) the report, the field save on the database is (null).

我尝试在我们的模型类中使用以下代码。Transpired 是一个带有长文本消息(报告)的字段。查看“报告”时,它会从数据库中检索数据并在我们的 UI 中正确显示。但是,当我保存(在编辑或创建时)报告时,保存在数据库中的字段为(空)。

Any idea on how I could save long texts? We were using varchar2(4000) before but most reports are more than 4000 characters.

关于如何保存长文本的任何想法?我们之前使用过 varchar2(4000) 但大多数报告都超过 4000 个字符。

Thanks.

谢谢。

EDIT:I'm using Oracle 10g. Column type is CLOB.

编辑:我使用的是 Oracle 10g。列类型为 CLOB。

回答by Pascal Thivent

The POSthin drivers that Oracle delivers are well known to automatically and silently nullify CLOB-fields when you try to save more than 4K (saving more that 4K, amazing for cLob). This is however supposedto be working when using the standard APIs - which Hibernate does - with Oracle 10g JDBC driver (see Handling Clobs in Oracle 10g). Surprisingly, many threads (e.g. this one) mention a similar problem with old versions of Oracle 10g thin driver so make sure that you use Oracle 10g Release 2 drivers(pick up the most recent ojdbc14.jar i.e. 10.2.0.4) or later.

众所周知,Oracle 提供的POS瘦驱动程序会在您尝试保存超过 4K(保存超过 4K,对于 cLob 来说很棒)时自动且静默地取消 CLOB 字段。然而,这应该在使用标准 API 时(Hibernate 所做的)与 Oracle 10g JDBC 驱动程序一起工作(请参阅处理 Oracle 10g 中的 Clob)。令人惊讶的是,许多线程(例如这个)都提到了旧版本的 Oracle 10g 瘦驱动程序的类似问题,因此请确保您使用Oracle 10g 第 2 版驱动程序(选择最新的 ojdbc14.jar,即 10.2.0.4)或更高版本

Note that Oracle has a limitation of 32K for CLOBs. To overcome this limitation, you'll need to the set the connection property SetBigStringTryClobto true. According to various sources, it seems that you will also need to disable JDBC batching (i.e. set batch_sizeto 0).

请注意,Oracle对 CLOB限制为 32K。要克服此限制,您需要将连接属性设置SetBigStringTryClobtrue。根据各种消息来源,您似乎还需要禁用 JDBC 批处理(即设置batch_size0)。

To do so, add the following properties to your hibernate.cfg.xml(or in your Spring configuration).

为此,请将以下属性添加到您的hibernate.cfg.xml(或在您的 Spring 配置中)。

<!-- Tell Oracle to allow CLOBs larger than 32K -->
<property name="hibernate.connection.SetBigStringTryClob">true</property>
<property name="hibernate.jdbc.batch_size">0</property>

回答by Omar Al Kababji

Using oracle9i I faced the same problem and I couldn't solve it, I had to do it manually by JDBC, however in JPA its a piece of cake. I don't know if they solved it in hibernate or not, It was one year and a half ago :(

使用 oracle9i 我遇到了同样的问题并且我无法解决它,我不得不通过 JDBC 手动完成它,但是在 JPA 中它是小菜一碟。我不知道他们是否在 hibernate 中解决了这个问题,一年半以前:(

回答by Karthik Gajula

If want to insert the data through hibernate,add this below code in your springs XML

如果想通过 hibernate 插入数据,请在 springs XML 中添加以下代码

<property name="hibernate.connection.SetBigStringTryClob">true</property>
 <property name="hibernate.jdbc.batch_size">0</property>

or

或者

<prop key="hibernate.connection.SetBigStringTryClob">true</prop>
 <prop key="hibernate.jdbc.batch_size">0</prop>

If you are intrested in adding through JDBC, add the following code in your data-source say Oracle-ds.xml for JBOSS

如果您有兴趣通过 JDBC 添加,请在数据源中添加以下代码,例如 JBOSS 的 Oracle-ds.xml

<connection-property name="SetBigStringTryClob">true</connection-property> 

Make sure that you use latest ojdbc14.jar and for JDBC connection and some jars like classes12.jar obstructs saving huge clob.So replace classes12.jar with ojdbc14.jar

确保你使用最新的 ojdbc14.jar 和 JDBC 连接和一些 jars 像 classes12.jar 阻碍保存巨大的 clob.So 用 ojdbc14.jar 替换 classes12.jar

This worked for me.

这对我有用。