Spring+hibernate+Oracle环境下的Clob字段处理方式
Spring或Hibernate其实都提供了透明处理Clob的方法。
具体做法为:
1.在sessionFactory中加入lobHandler的注射:
<bean id="sessionFactory" ref="dataSource"/>
??????? <property name="lobHandler" ref="lobHandler"/>
??????? <property name="mappingResources">
????????????????? <value>xxx.hbm.xml</value>
????????????????? .....
???????? </property>
</bean>
2.定义这个lobHandler,值得注意的是这里有Oracle的版本区别:
<bean id="lobHandler" lazy-init="true"?? lazy-init="true"?? lazy-init="true"? column="context" type="org.springframework.orm.hibernate.support.ClobStringType" length="1048576000"/>
这里的length是字节了,不是长度,最大可以设到2G.对应的,该字段在领域对象中直接申明成String就可以了.当对这个字段写入长数据时直接调用其set方法就可以了,Spring会自己帮你做余下的处理,让你透明的处理Clob字段.
5.业务逻辑层对该字段的操作必须需要在有事务管理的方法中使用,否则会报:
java.lang.IllegalStateException: Active Spring transaction synchronization or active JTA transaction with 'jtaTransactionManager' on LocalSessionFactoryBean required 这个错误