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=””/>


One thought on “Mapping large String to Clob for Oracle with Hibernate and Spring

  1. I’ll immediately clutch your rss feed as I can’t in finding your
    email subscription hyperlink or e-newsletter service.
    Do you’ve any? Kindly let me realize so that I may subscribe.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s