-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Newer Implementation of OracleCHAR
PostPosted: Thu Apr 16, 2009 7:26 am 
Newbie

Joined: Thu Apr 16, 2009 7:09 am
Posts: 13
Hibernate version: 3.3.1.GA

Name and version of the database you are using: Oracle 10 >

Hi,

I thought that I would share with you my more up to date version of the OracleCHAR class found at http://www.hibernate.org/90.html

This implementation also logs out the required values being set when you have the log4j specified 'org.hibernate.type' to be trace level.

Code:
package your.package.here;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import oracle.jdbc.OracleTypes;

import org.hibernate.HibernateException;
import org.hibernate.type.Type;
import org.hibernate.usertype.UserType;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;

/**
* @author hugheos
*
*/
public class OracleCHAR implements UserType {
   
   private static final boolean IS_VALUE_TRACING_ENABLED = LoggerFactory.getLogger( Type.class.getName() ).isTraceEnabled();
   private Logger log;
   
   /**
    *
    */
   public OracleCHAR() {
      super();
   }

   /**
    * @return
    */
   private Logger log() {
      if ( log == null ) {
         log = LoggerFactory.getLogger( getClass() );
      }
      return log;
   }

   /* (non-Javadoc)
    * @see org.hibernate.usertype.UserType#sqlTypes()
    */
   public int[] sqlTypes() {
      return new int[] { OracleTypes.FIXED_CHAR };
   }

   /* (non-Javadoc)
    * @see org.hibernate.usertype.UserType#returnedClass()
    */
   public Class returnedClass() {
      return String.class;
   }

   /* (non-Javadoc)
    * @see org.hibernate.usertype.UserType#equals(java.lang.Object, java.lang.Object)
    */
   public boolean equals(Object x, Object y) throws HibernateException {
      return (x == y) || (x != null && y != null && (x.equals(y)));
   }

   /* (non-Javadoc)
    * @see org.hibernate.usertype.UserType#nullSafeGet(java.sql.ResultSet, java.lang.String[], java.lang.Object)
    */
   public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {
      String val = rs.getString(names[0]);
      if (null == val) {
         return null;
      }
      else {
         String trimmed = val.trim();
         if (trimmed.equals("")) {
            return null;
         }
         else {
            return trimmed;
         }
      }
   }

   /* (non-Javadoc)
    * @see org.hibernate.usertype.UserType#nullSafeSet(java.sql.PreparedStatement, java.lang.Object, int)
    */
   public void nullSafeSet(PreparedStatement st, Object value, int index)
         throws HibernateException, SQLException {
      if (value == null) {
         if ( IS_VALUE_TRACING_ENABLED ) {
            log().trace( "binding null to parameter: " + index );
         }
         st.setObject(index, "", OracleTypes.FIXED_CHAR);
      }
      else {
         if ( IS_VALUE_TRACING_ENABLED ) {
            log().trace( "binding '" + value + "' to parameter: " + index );
         }
         st.setObject(index, (String) value, OracleTypes.FIXED_CHAR);
      }
   }

   /* (non-Javadoc)
    * @see org.hibernate.usertype.UserType#deepCopy(java.lang.Object)
    */
   public Object deepCopy(Object value) throws HibernateException {
      if (value == null) {
         return null;
      }
      return new String((String) value);
   }

   /* (non-Javadoc)
    * @see org.hibernate.usertype.UserType#isMutable()
    */
   public boolean isMutable() {
      return false;
   }

   /* (non-Javadoc)
    * @see org.hibernate.usertype.UserType#assemble(java.io.Serializable, java.lang.Object)
    */
   public Object assemble(Serializable cached, Object owner) throws HibernateException {
      return cached;
   }

   /* (non-Javadoc)
    * @see org.hibernate.usertype.UserType#disassemble(java.lang.Object)
    */
   public Serializable disassemble(Object value) throws HibernateException {
      return (Serializable) value;
   }

   /* (non-Javadoc)
    * @see org.hibernate.usertype.UserType#hashCode(java.lang.Object)
    */
   public int hashCode(Object x) throws HibernateException {
      return x.hashCode();
   }

   /* (non-Javadoc)
    * @see org.hibernate.usertype.UserType#replace(java.lang.Object, java.lang.Object, java.lang.Object)
    */
   public Object replace(Object original, Object target, Object owner) throws HibernateException {
      return original;
   }

}


I hope this helps others like it did for me,
Enjoy.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 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.