for Oracle9i and optional weblogic:
<code>
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.UserType;
import oracle.sql.CLOB;
import org.apache.commons.lang.ObjectUtils;
import org.apache.log4j.Logger;
import weblogic.jdbc.extensions.WLConnection;
/**
*/
public class StringClobType implements UserType {
private Logger log = Logger.getLogger(getClass());
/**
* Return the SQL type codes for the columns mapped by this type.
*/
public int[] sqlTypes() {
return new int[] { Types.CLOB};
}
/**
* The class returned by <tt>nullSafeGet()</tt>.
*/
public Class returnedClass() {
return String.class;
}
public boolean equals(Object x, Object y) {
return ObjectUtils.equals(x, y);
}
/**
* Retrieve an instance of the mapped class from a JDBC resultset.
Implementors
* should handle possibility of null values.
*/
public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
throws HibernateException, SQLException {
Reader clobReader = rs.getCharacterStream(names[0]);
if (clobReader == null)
return null;
StringBuffer str = new StringBuffer();
BufferedReader bufferedClobReader = new BufferedReader(clobReader);
try {
String line = null;
while ((line = bufferedClobReader.readLine()) != null)
str.append(line);
} catch (IOException e) {
throw new SQLException(e.toString());
} finally {
try {
bufferedClobReader.close();
} catch (IOException e) {
}
}
return str.toString();
}
/**
* Write an instance of the mapped class to a prepared statement.
Implementors
* should handle possibility of null values. A multi-column type should
be written
* to parameters starting from <tt>index</tt>.
*
*/
public void nullSafeSet(PreparedStatement st, Object value, int index)
throws HibernateException, SQLException {
if (value == null) {
st.setNull(index, sqlTypes()[0]);
return;
}
try {
Connection conn =
st.getConnection().getMetaData().getConnection();
if (conn instanceof WLConnection)
conn = ((WLConnection)conn).getVendorConnection();
log.info(conn.getClass().getName());
Writer tempClobWriter = null;
CLOB tempClob = CLOB.createTemporary(conn, true,
CLOB.DURATION_SESSION);
try {
tempClob.open(CLOB.MODE_READWRITE);
tempClobWriter = tempClob.getCharacterOutputStream();
tempClobWriter.write((String) value);
tempClobWriter.flush();
} finally {
if (tempClobWriter != null)
tempClobWriter.close();
tempClob.close();
}
st.setClob(index, (Clob) tempClob);
} catch (IOException e) {
throw new HibernateException(e);
}
}
/**
* Return a deep copy of the persistent state, stopping at entities and at
* collections.
*/
public Object deepCopy(Object value) {
return (String)value;
}
/**
* Are objects of this type mutable?
*/
public boolean isMutable() {
return false;
}
}
</code> |