I just wanted to get a critique of the XMLType UserType class that I have written. Note, I have actually not yet actually used it, but am in the process of integrating it with out hibernate project.
I just wanted to put it up here and get some feedback from the community about the UserType class itself (if it looks correct) and about the XMLType itself. I did look at the threads here about XMLType but no one actually posted an entire class.
My questions would be:
1. Does the UserType class structure look correct and have I implemented the methods to do the intended tasks.
2. Does the conversion logic from and to XMLType and Clob look correct? (I have mapped my classes to use the Hibernate Clob type and this class to use the sql Clob type, will these two work correctly together?)
Thanks for any input.
So, here it is:
Code:
import java.io.Serializable;
import java.sql.Clob;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import net.sf.hibernate.Hibernate;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.UserType;
import oracle.jdbc.OraclePreparedStatement;
import oracle.jdbc.driver.OracleResultSet;
import oracle.sql.OPAQUE;
public class XMLType implements UserType, Serializable {
private static final Class returnedClass = XMLType.class;
private static final int[] SQL_TYPES = new int[] { oracle.xdb.XMLType._SQL_TYPECODE };
/* (non-Javadoc)
* @see net.sf.hibernate.UserType#sqlTypes()
*/
public int[] sqlTypes() {
return SQL_TYPES;
}
/* (non-Javadoc)
* @see net.sf.hibernate.UserType#returnedClass()
*/
public Class returnedClass() {
return returnedClass;
}
/* (non-Javadoc)
* @see net.sf.hibernate.UserType#equals(java.lang.Object, java.lang.Object)
*/
public boolean equals(Object arg0, Object arg1) throws HibernateException {
if(arg0 == null || arg1 == null){
throw new HibernateException("None of the arguments can be null.");
}
if(arg0 instanceof oracle.xdb.XMLType
&& arg1 instanceof oracle.xdb.XMLType){
return arg0.equals(arg1);
}
return false;
}
/* (non-Javadoc)
* @see net.sf.hibernate.UserType#nullSafeGet(java.sql.ResultSet, java.lang.String[], java.lang.Object)
*/
public Object nullSafeGet(ResultSet rs, String[] names, Object arg2)
throws HibernateException, SQLException {
OracleResultSet ors = (OracleResultSet) rs;
OPAQUE op = ors.getOPAQUE(names[0]);
oracle.xdb.XMLType xt= oracle.xdb.XMLType.createXML(op);
return xt.getClobVal();
}
/* (non-Javadoc)
* @see net.sf.hibernate.UserType#nullSafeSet(java.sql.PreparedStatement, java.lang.Object, int)
*/
public void nullSafeSet(PreparedStatement st, Object value, int index)
throws HibernateException, SQLException {
Clob aClob = Hibernate.createClob((String) value);
OraclePreparedStatement ost = (OraclePreparedStatement) st;
ost.setOPAQUE(index, oracle.xdb.XMLType.createXML((OPAQUE) aClob));
}
/* (non-Javadoc)
* @see net.sf.hibernate.UserType#deepCopy(java.lang.Object)
*/
public Object deepCopy(Object value) throws HibernateException {
if (value == null) return null;
Clob aClob = Hibernate.createClob((String) value);
try{
return oracle.xdb.XMLType.createXML((OPAQUE) aClob);
}catch(SQLException e){
throw new HibernateException(e);
}
}
/* (non-Javadoc)
* @see net.sf.hibernate.UserType#isMutable()
*/
public boolean isMutable() {
return false;
}
}