转-Spring + Hibernate + Oracle9i中使用Clob

背景

在信息发布系统中,如果需要将用户发布的信息保存到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)、注意使用的NativeJdbcExtractor一定要使用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;

       }

       ...   

}

 

你可能感兴趣的:(Hibernate)