背景

在信息发布系统中,如果需要将用户发布的信息保存到Oralcle9i数据库中,使用varchar2(4000)有时是不够用的,因为除了用户录入的内容还要加上html编辑器附加的html标识,所以我建议使用clob

动手

其实也挺简单的:两个配置、两个注意

1hbm配置

<hibernate-mapping>

       <class name="com.wonders.pubinfo.bean.LawInfo" table="JZ_LAW_INFO">

              <id column="ID" name="id" type="java.lang.String">

                     <generator class="uuid.hex"/>

              </id>

              <property column="TYPE" length="4" name="type" type="java.lang.String"/>

              <property column="TITLE" length="200" name="title" type="java.lang.String"/>

              <property column="KEYWORD" length="200" name="keyword" type="java.lang.String"/>

              <property column="CONTENT" name="content" type="org.springframework.orm.hibernate.support.ClobStringType"/>

              <property column="PUBLISH_TIME" length="23" name="publishTime" type="java.sql.Timestamp"/>

              <property column="PRACTICE_TIME" length="23" name="practiceTime" type="java.sql.Timestamp"/>

              <property column="END_TIME" length="23" name="endTime" type="java.sql.Timestamp"/>

              <property column="FILE_NUM" length="50" name="fileNum" type="java.lang.String"/>

              <property column="PUB_ORGAN" length="50" name="pubOrgan" type="java.lang.String"/>

              <property column="TEMPLATE" length="20" name="template" type="java.lang.String"/>

              <property column="VALIDITY" length="1" name="validity" type="java.lang.String"/>

              <property column="HTML_FILE" length="50" name="htmlFile" type="java.lang.String"/>

       </class>

</hibernate-mapping>

2application配置

<beans>

       <!--**** Start of PERSISTENCE DEFINITIONS ****-->

  <bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

    <property name="location">

      <value>init.properties</value>

    </property>

  </bean> 

 

  <!-- JDBC Connection -->

  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">

    <property name="driverClassName">

      <value>${datasource.driverClassName}</value>

    </property>

    <property name="url">

      <value>${datasource.url}</value>

    </property>

    <property name="username">

      <value>${datasource.username}</value>

    </property>

    <property name="password">

      <value>${datasource.password}</value>

    </property>

    <property name="maxActive">

      <value>${datasource.maxActive}</value>

    </property>

    <property name="maxIdle">

      <value>${datasource.maxIdle}</value>

    </property>

    <property name="maxWait">

      <value>${datasource.maxWait}</value>

    </property>

    <property name="defaultAutoCommit">

      <value>${datasource.defaultAutoCommit}</value>

    </property>

  </bean>

 

  <!--**** OracleLobHandler ****-->

  <bean id="oracleLobHandler" class="org.springframework.jdbc.support.lob.OracleLobHandler">

              <property name="nativeJdbcExtractor">

                     <ref local="nativeJdbcExtractor"/>

              </property>

  </bean>

 

       <bean id="nativeJdbcExtractor" class="org.springframework.jdbc.support.nativejdbc.SimpleNativeJdbcExtractor"></bean>

      

       <!--**** SessionFactory Definition ****-->

       <bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">

              <property name="dataSource">

                     <ref local="dataSource" />

              </property>

              <property name="lobHandler">

                     <ref bean="oracleLobHandler" />

              </property>

              <property name="mappingResources">

                     <list>

                            <value>com\wonders\pubinfo\bean\LawInfo.hbm.xml</value>

                            <value>com\wonders\pubinfo\bean\LawType.hbm.xml</value>

                     </list>

              </property>

              <property name="hibernateProperties">

                     <props>

                            <prop key="hibernate.dialect">${hibernate.dialect}</prop>

                            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>

                            <prop key="hibernate.jdbc.fetch_size">${hibernate.jdbc.fetch_size}</prop>

                            <prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>

                     </props>

              </property>

       </bean>

 

</beans>

3、注意

1)、注意红色字体部分,就是你要配置的;在引用spring类的地方,注意查看一下你使用的spring包中有的做相应修改。

2)、注意使用的datasource一定使用NativeJdbcExtractor,就是适用jdbc连接,不要用容器提供连接,不然会报如下错误:

OracleLobCreator needs to work on [oracle.jdbc.OracleConnection], not on [class com.ibm.ws.rsadapter.jdbc.WSJdbcConnection] - specify a corresponding NativeJdbcExtractor

4bean中的clob字段使用String类型

public class LawInfo implements Serializable {

       ...

       private String content;

       ...

       public String getContent() {

              return content;

       }

 

       public void setContent(String content) {

              this.content = content;

       }

 

       ...   

}

 

posted on 2006-03-08 21:03 野草 阅读(1441) 评论(2)  编辑  收藏 所属分类: 2shtv

评论:
# re: Spring + Hibernate + Oracle9i中使用Clob 2007-05-17 10:31 | 野心
谢谢你的提示~~
问题解决了~~


  回复  更多评论
  
# re: Spring + Hibernate + Oracle9i中使用Clob 2009-08-07 15:24 | slandi
请问你说的2个注意是什么意思呀,没太明白
我按照你说的,除datasource使用的proxool 0.9.1的jar包,其余的都一样,使用的是oracle9i数据库,运行后可以正确查询出clob字段的数据,可是插入时报如下错误:
org.springframework.dao.InvalidDataAccessApiUsageException: OracleLobCreator needs to work on [oracle.jdbc.OracleConnection], not on [oracle.jdbc.internal.OracleConnection$$EnhancerByProxool$$3dcd939d]: specify a corresponding NativeJdbcExtractor; nested exception is java.lang.ClassCastException: oracle.jdbc.internal.OracleConnection$$EnhancerByProxool$$3dcd939d incompatible with oracle.jdbc.driver.OracleConnection
at org.springframework.jdbc.support.lob.OracleLobHandler$OracleLobCreator.createLob(OracleLobHandler.java:357)
不知该如何解决?  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: