When my byte[] is null I am getting a nullpointer exception ? Anyone else have this problem ?
import java.io.IOException;
import java.io.OutputStream;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.sql.Blob;
import oracle.sql.BLOB;
import net.sf.hibernate.Hibernate;
public class BinaryBlobType implements net.sf.hibernate.UserType {
public int[] sqlTypes() {
return new int[] { Types.BLOB };
}
public Class returnedClass() {
return byte[].class;
}
public boolean equals(Object x, Object y) {
return (x == y)
|| (x != null
&& y != null
&& java.util.Arrays.equals((byte[]) x, (byte[]) y));
}
public void nullSafeSet(PreparedStatement st, Object value, int index)
throws SQLException {
if (st instanceof org.apache.commons.dbcp.DelegatingPreparedStatement
&& ((org.apache.commons.dbcp.DelegatingPreparedStatement) st)
.getDelegate()
instanceof oracle.jdbc.OraclePreparedStatement) {
oracle.sql.BLOB blob =
oracle.sql.BLOB.createTemporary(
((org.apache.commons.dbcp.PoolableConnection) st
.getConnection())
.getDelegate(),
false,
oracle.sql.BLOB.DURATION_SESSION);
blob.open(BLOB.MODE_READWRITE);
OutputStream out = blob.getBinaryOutputStream();
try {
out.write((byte[]) value);
out.flush();
out.close();
} catch (IOException e) {
throw new SQLException("failed write to blob" + e.getMessage());
}
blob.close();
((oracle
.jdbc
.OraclePreparedStatement)
((org
.apache
.commons
.dbcp
.DelegatingPreparedStatement) st)
.getDelegate())
.setBLOB(
index,
blob);
} else if (st instanceof oracle.jdbc.OraclePreparedStatement) {
oracle.sql.BLOB blob =
oracle.sql.BLOB.createTemporary(
st.getConnection(),
false,
oracle.sql.BLOB.DURATION_SESSION);
blob.open(BLOB.MODE_READWRITE);
OutputStream out = blob.getBinaryOutputStream();
try {
out.write((byte[]) value); // FAILS NULLPOINTER HERE
out.flush();
out.close();
} catch (IOException e) {
throw new SQLException("failed write to blob" + e.getMessage());
}
blob.close();
((oracle.jdbc.OraclePreparedStatement) (st)).setBLOB(index, blob);
} else {
st.setBlob(index, Hibernate.createBlob((byte[]) value));
}
}
// and.. note the null check, oracle drivers return a null blob...
public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
throws SQLException {
final Blob blob = rs.getBlob(names[0]);
return blob != null ? blob.getBytes(1, (int) blob.length()) : null;
}
public Object deepCopy(Object value) {
if (value == null)
return null;
byte[] bytes = (byte[]) value;
byte[] result = new byte[bytes.length];
System.arraycopy(bytes, 0, result, 0, bytes.length);
return result;
}
public boolean isMutable() {
return true;
}
}
I used the code off the wiki area . The comment in the code indicates where it fails.
Thanks
|