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.