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

瘦欲@ 提交于 2020-01-03 03:11:30

问题


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

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

Thanks.

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


回答1:


The POS thin 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 supposed to 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.

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

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

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



回答2:


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 :(




回答3:


If want to insert the data through hibernate,add this below code in your 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

<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

This worked for me.



来源:https://stackoverflow.com/questions/2115420/cannot-save-clob-data-type-in-database-struts-spring-hibernate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!