SSH对Clob字段类型的支持
环境:
1、Oracle 9i 9.2.0.8.0
2、jdbc:ojdbc5.jar
3、Spring 2
4、Hibernate 3
系统中有个信息管理模块,其中的信息内容使用了FCKeditor进行编辑,内容可能会很长,因此决定将该字段类型由varchar2改为clob。
在网上查资料,据说Oracle 10g和1.4及以上版本的JDBC已经支持使用String直接映射Clob,配置如下:
- Hibernate的JDBC配置
Code
<property name="hibernate.connection.SetBigStringTryClob">true</property>
<property name="hibernate.connection.SetBigStringTryClob">true</property>
- Hibernate映射文件
Code
<property name="clobContent" type="text" column="V_CONTENT"></property>
<property name="clobContent" type="text" column="V_CONTENT"></property>
- 实体类
Code
private String clobContent;
private String clobContent;
可惜我们的环境并不支持这种简单的配置,经多番查找资料和试验,我采用了Spring的支持方式,配置如下:
- Spring 配置文件
Code
<!-- 对数据库Blob字段和Clob字段的支持 -->
<bean id="nativeJdbcExtractor" class="org.springframework.jdbc.support.nativejdbc.SimpleNativeJdbcExtractor" lazy-init="true">
</bean>
<bean id="lobHandler" class="org.springframework.jdbc.support.lob.OracleLobHandler" lazy-init="true">
<property name="nativeJdbcExtractor">
<ref local="nativeJdbcExtractor" />
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="app_ds"/>
<!-- 对数据库Blob字段和Clob字段的支持 -->
<property name="lobHandler">
<ref local="lobHandler"/>
</property>
<!-- 对数据库Blob字段和Clob字段的支持 -->
<bean id="nativeJdbcExtractor" class="org.springframework.jdbc.support.nativejdbc.SimpleNativeJdbcExtractor" lazy-init="true">
</bean>
<bean id="lobHandler" class="org.springframework.jdbc.support.lob.OracleLobHandler" lazy-init="true">
<property name="nativeJdbcExtractor">
<ref local="nativeJdbcExtractor" />
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="app_ds"/>
<!-- 对数据库Blob字段和Clob字段的支持 -->
<property name="lobHandler">
<ref local="lobHandler"/>
</property>
- Hibernate映射文件
Code
<property name="clobContent" type="org.springframework.orm.hibernate3.support.ClobStringType" column="V_CONTENT"></property>
<property name="clobContent" type="org.springframework.orm.hibernate3.support.ClobStringType" column="V_CONTENT"></property>
- 实体类
Code
private String clobContent;
private String clobContent;
这样配置后,即可正常运行,不需改动代码。
附:
1、JDBC版本很重要。最初我们的lib下扔了两个驱动classes12.jar和ojdbc5.jar,一直是classes12.jar在起作用,结果调试时读取数据没有问题,但写入数据时报“无法从套接字读取更多的数据”的错误,删除这个包后运行正常。
2、varchar2转clob的SQL
Code
ALTER TABLE tb_base_notify
ADD (v_content1 CLOB);
UPDATE tb_base_notify
SET v_content1=v_content;
ALTER TABLE tb_base_notify DROP COLUMN v_content;
ALTER TABLE tb_base_notify
RENAME COLUMN v_content1 TO v_content;
ALTER TABLE tb_base_notify
ADD (v_content1 CLOB);
UPDATE tb_base_notify
SET v_content1=v_content;
ALTER TABLE tb_base_notify DROP COLUMN v_content;
ALTER TABLE tb_base_notify
RENAME COLUMN v_content1 TO v_content;