Hibernate Books

All times are UTC - 5 hours [ DST ]

Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Hibernate NCLOB mapping
PostPosted: Wed May 16, 2012 9:35 am 

Joined: Tue May 15, 2012 4:40 pm
Posts: 1
Hi friends,

The normal character set in our database is set to WE8ISO8859P1(Western European), and we are not planning to convert it to UTF16 any time soon. To accommodate the extra characters entered by user, we decided to change the data type of one column to NCLOB. However, we couldn't figure out how to map NCLOB in Hibernate.

Any suggestions are truly appreciated!

Below are some version info
Hibernate 3.3.2
Spring 3.0.3
Oracle 11g
Oracle JDBC (ojdbc6)

update 3:
Here is what I use now. All characters are saved correctly and the performance is also good.
      Session session = this.getHibernateTemplate().getSessionFactory()
      session.doWork(new Work() {

         public void execute(Connection connection) throws SQLException {
            NClob nclob = connection.createNClob();
            nclob.setString(1, instance.getNote());

            PreparedStatement st = connection.prepareStatement(sql);
            st.setString(1, instance.getRecordKey());
            st.setString(2, instance.getSummary().getId().getSumPolnum());
            st.setString(3, instance.getSummary().getId()
            st.setString(4, instance.getNotesDesc().getNoteCode());
            st.setDouble(5, instance.getInsuredId());
            st.setString(6, instance.getUnderid().getUnderId());
            st.setNClob(7, nclob);

update 1:
After some research, I was able to get this to work in local environment (Eclipse with Jetty server). Below is the user type class.
public class NCharString extends StringType {
   private static final long serialVersionUID = 1L;

   public void set(PreparedStatement st, Object value, int index)
         throws SQLException {
      if (st instanceof OraclePreparedStatement) {
         ((OraclePreparedStatement) st).setFormOfUse(index,
      }else if (st instanceof WSJdbcPreparedStatement) { //still doesn't work in Websphere
         WSCallHelper.jdbcCall(OraclePreparedStatement.class, st,
               "setFormOfUse", new Object[] { index,
                     OraclePreparedStatement.FORM_NCHAR },
                     new Class[] {Integer.class, Short.class });
         System.out.println("In NCharString, st type is: " + st.getClass().toString());
      super.set(st, value, index);

I tried WSCallHelper in WebSphere to access the wrapped OraclePreparedStatment with no success. The closest class I could get was OraclePreparedStatementWrapper, and apparently it doesn't support setFormOfUse method.

And the Hibernate mapping:
<property name="note" type="mypackage.NCharString">
       <column name="NOTE"/>

update 2:

Came across this great article on Clob mapping.

I modified code for NClob and used setCharacterStream instead. But some characters are still not inserted correctly. I think something is still missing in nullSafeSet method. Any idea?

public class NClobString implements UserType {

   public int[] sqlTypes() {
      return new int[] { Types.NCLOB };


   public void nullSafeSet(PreparedStatement pst, Object data, int index)
         throws HibernateException, SQLException {
      data = data == null ? new String() : data;
      String in = (String) data;

      byte[] buf = in.getBytes();

      ByteArrayInputStream bais = new ByteArrayInputStream(buf);
      Reader inReader = new InputStreamReader(bais);
      pst.setCharacterStream(index, inReader);



Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC - 5 hours [ DST ]

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.