使用hibernate3和Spring解决Oracle大字段(长文本)问题

       使用Oracle来存储长文本始终没Mysql来的方便,Mysql不用特殊配置直接使用java.lang.String来存储Text 字段内容即可。这里可以看出Mysql的优势了吧。^-^
很久之前在网上找了些,加上自己摸索的,应该算是很简单的了。
来个总结备案。

步骤:
1。使用Clob类型来存储长文本。
2。在你的Spring的applicationContext-hibernate.xml里配置以下两个bean.

 

<bean id="nativeJdbcExtractor" class="org.springframework.jdbc.support.nativejdbc.SimpleNativeJdbcExtractor"/>
<bean id="oracleLobHandle" class="org.springframework.jdbc.support.lob.OracleLobHandler" Lazy-init="true">
 <property name="nativeJdbcExtractor">
   <ref local="nativeJdbcExtractor"/>
 </property>
</bean>

 

 

 

 

3。在有clob类型sessionFactory(org.springframework.com.hibernate.LocalSessionFractoryBean)中加入

<property name="lobHandler"><ref bean="oracleLobHandle"/></property>

 

 4。在*.hbm文件里使用长文本的字段加上对应的Java类型。

<property name="content" type="org.springframework.orm.hibernate3.support.ClobStringType" column="TRACK_CONTENT" length="100000"/>

 

 

 5。上边的这个content,即JavaBean的对应的属性类型为String,不是ClobStringType。

 

 

 

如果只配置到这里的话,启动应用的时候会报缺少事务支持的错误,忘了具体报什么错了。

6。所以要给save,remove方法加上事务支持。

<bean id="txProxyTemplate" abstract="true"
        class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="transactionManager"><ref bean="transactionManager"/></property>
        <property name="transactionAttributes">
            <props>
                <prop key="save*">PROPAGATION_REQUIRED</prop>
                <prop key="remove*">PROPAGATION_REQUIRED</prop>
                <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
            </props>
        </property>
    </bean>
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory"><ref bean="sessionFactory"/></property>
    </bean>
 <bean id="cnProjectTrackManager" parent="txProxyTemplate">
        <property name="target">
            <bean class="com.helloworld.cn.manager.project.bl.impl.ProjectTrackManagerImpl">
                <property name="projectTrackDAO"><ref bean="cnProjectTrackDAO"/></property>
            </bean>
        </property>
    </bean>

 

 

 7。结束。
下边是配置所需的3个文件。
applicationContext.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
    
    <bean id="txProxyTemplate" abstract="true"
        class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="transactionManager"><ref bean="transactionManager"/></property>
        <property name="transactionAttributes">
            <props>
                <prop key="save*">PROPAGATION_REQUIRED</prop>
                <prop key="remove*">PROPAGATION_REQUIRED</prop>
                <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
            </props>
        </property>
    </bean>
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory"><ref bean="sessionFactory"/></property>
    </bean>
    <bean id="cnProjectTrackManager" parent="txProxyTemplate">
        <property name="target">
            <bean class="com.helloworld.cn.manager.project.bl.impl.ProjectTrackManagerImpl">
                <property name="projectTrackDAO"><ref bean="cnProjectTrackDAO"/></property>
            </bean>
        </property>
    </bean>
    
    <!--  bean id="jtaTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
        <property name="sessionFactory"><ref bean="sessionFactory"/></property>
    </bean-->
     <!--bean id="cnProjectTrackManager" class="com.helloworld.cn.manager.project.bl.impl.ProjectTrackManagerImpl">
        <property name="projectTrackDAO"><ref bean="cnProjectTrackDAO"/></property>
     </bean-->
</beans>

 

 applicationContext-hibernate.xml

 

 

 

 

 

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="com.vitamin.cn.manager.project.entity.ProjectTrack" dynamic-update="false" 
        dynamic-insert="false" table="VITAMIN_CN_PROJECT_TRACK" lazy="false">
        <id name="id" column="TRACK_ID" type="java.lang.Long" unsaved-value="null">
            <!-- generator for mysql
            <generator class="native">
            </generator> 
            -->
            <!-- generator for oracle -->
            <generator class="sequence"> 
              <param name="sequence">SEQ_CN_PROJECT_TRACK</param> 
            </generator>
        </id>
        <many-to-one name="project" column="PROJECT_ID"/>
        <property name="date" type="java.util.Date"  column="TRACK_DATE" />
        <property name="editor" type="java.lang.String"  column="TRACK_EDITOR" />
        <property name="content" type="org.springframework.orm.hibernate3.support.ClobStringType" column="TRACK_CONTENT" length="100000"/>
        <property name="name" type="java.lang.String"  column="TRACK_NAME" />
    </class>

</hibernate-mapping>

 

  

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
    <!-- bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean" -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource"><ref bean="dataSource"/></property>
        <property name="mappingResources">
            <list>
                <value>/hbm.ProjectTrack.xml</value>                        
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <!-- prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop-->
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop >
                <!-- prop key="hibernate.dialect">net.sf.hibernate.dialect.MySQLDialect</prop -->                
                <!-- prop key="hibernate.hbm2ddl.auto">update</prop -->
                 <prop key="hibernate.show_sql">true</prop>
                 <!-- prop key="hibernate.jdbc.batch_size">0</prop-->
                <prop key="hibernate.use_outer_join">true</prop>                
                <!-- prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop -->                 
            </props>
        </property>
        <property name="lobHandler"><ref bean="oracleLobHandle"/></property>
    </bean>
    
    <bean id="oracleLobHandle" class="org.springframework.jdbc.support.lob.OracleLobHandler" lazy-init="true">
      <property name="nativeJdbcExtractor">
        <ref local="nativeJdbcExtractor"/>
      </property>
    </bean>
    <bean id="nativeJdbcExtractor" class="org.springframework.jdbc.support.nativejdbc.SimpleNativeJdbcExtractor"/>
    <bean id="cnProjectTrackDAO" class="com.helloworld.cn.manager.project.dao.hibernate.ProjectTrackDAOHibernate">
        <property name="sessionFactory"><ref bean="sessionFactory"/></property>
    </bean>
</beans>

 

hbm.ProjectTrack.xml

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值