Clob Processing in Java, Spring and Hibernate

This blog  shows how to save CLOB to a Relational Database using Java, Spring and Hibernate.This example is implemented using two classes from Spring Framework

1)OracleLobHandler

2)NativeJdbcExtractor (This example has been tested in Weblogic J2ee Container and this example uses WebLogicNativeJdbcExtractor.)

Spring Framework applicationContext.xml configuration:

<bean id=”mySessionFactory class=”org.springframework.orm.hibernate3.LocalSessionFactoryBean”>

<property name=”dataSource” ref=”myDataSource” />

<property name=”lobHandler” ref=”lobHandler” />

<property name=”mappingResources”>

<list> <value>com/clob/hibernate/lohHandler.hbm.xml</value>

</list></property> <property name=”hibernateProperties”><props><prop key=”hibernate.dialect”>org.hibernate.dialect.OracleDialect </prop>

<prop key=”hibernate.show_sql”>true</prop>

<prop key=”hibernate.connection.autocommit”>false</prop>

<prop key=”hibernate.connection.release_mode”>auto </prop>

<prop key=”hibernate.jdbc.use_streams_for_binary”>true </prop>

<prop key=”SetBigStringTryClob”>true</prop>

</props>

</property>

</bean>

<bean id=”lobHandler” class=”org.springframework.jdbc.support.lob.OracleLobHandler”

lazy-init=”true”>

<property name=”nativeJdbcExtractor”>

<beanclass=”org.springframework.jdbc.support.nativejdbc.WebLogicNativeJdbcExtractor” />

</property>

</bean>

Code block from lobHandler.hbm.xml:

<property name=”extdXml” type=”java.sql.Clob”>

<column name=”DTL_XML” /> </property>

Java bean class:

public class lohHandler implements java.io.Serializable {

 private Clob extdXml;

public void setExtdXml(Clob extdXml) { this.extdXml = extdXml;} public Clob getExtdXml() { return this.extdXml;}

public void setClobContent( String sourceStream )throws IOException{

setExtdXml( Hibernate.createClob( sourceStream ) );

}} Spring Framework Controller class:Upload the xml file and store contents of xml file in a String.Instantiate the bean.

lohHandler lHandler = new lohHandler ();

lHandler.setClobContent(xmlString);

setClobContent() method inturn calls Hibernate.createClob() method, which creates a CLOB out of a String. Now we are ready to save the lHandler bean object in a Hibernate Session.

Leave a Reply

*