Mapping large String to Clob for Oracle with Hibernate and Spring

By default String mapping is limited to 4000 bytes in Oracle DB because of the VARCHAR2 type. You can leverage this restriction by storing larger String in a Clob type.

For doing this, you only have to change de text type used in Spring configuration to map your field. Using the ClobStringType will tell Hibernate to look for a Clob in the database.

This method is very useful and solve the problem of reading and writing String larger than 4000 bytes in the DB. In fact, while the content of the field was small, there wasn’t any problem, but with a larger one, Hibernate was not able to store it into the DB. Even if I tryed to store it with JDBC to see what was the problem, Hibernate couldn’t retrieve the stored field. It clearly was a problem of mapping.

Code :

.: To Complete :.

Let’s suppose we have the basic following class Book in our datamodel :

public class Book
    private String summary;
    public String getSummary() {
        return summary;

    public void setSummary(String summary) {
        this.summary = summary;

In our configuration file for hibernate, we find :

<bean id="lobHandler" lazy-init="true"/>

<bean id="mySessionFactory">
 <property name="dataSource" ref="myDataSource"/>
  <property name="mappingLocations">
  <property name="hibernateProperties">
    <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
    <property name="lobHandler" ref="lobHandler"/>

The hibernate-mapping.hbm.xml gives us :

<class name=”Book” table=”book”>
<id name=”id” type=”integer” unsaved-value=”null”>
<param name=”sequence”>book_id_seq</param>

<property name=”summary” type=””/>