1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;
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 java.sql.Blob;
import oracle.jdbc.OracleConnection;
import oracle.sql.BLOB;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;
import org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor;
public class BlobUserType implements 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 Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {
Blob blob = rs.getBlob(names[0]);
return blob.getBytes(1, (int) blob.length());
}
protected OracleConnection findOracleConnection(Connection orig) throws ClassNotFoundException {
if (orig instanceof OracleConnection)
return (OracleConnection) orig;
CommonsDbcpNativeJdbcExtractor extractor = new CommonsDbcpNativeJdbcExtractor();
try {
//return (OracleConnection) extractor.getNativeConnection(orig);
return (OracleConnection) extractor.getNativeConnection(orig);
} catch (SQLException sql) {
throw new HibernateException("JDBC connection object must be a oracle.jdbc.OracleConnection. " + "Connection class is " + orig.getClass().getName());
}
}
public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
try {
Connection conn = st.getConnection();
// Make sure connection object is right type
conn = this.findOracleConnection(conn);
// if (st instanceof oracle.jdbc.OraclePreparedStatement) {
oracle.sql.BLOB blob = oracle.sql.BLOB.createTemporary(conn, false, oracle.sql.BLOB.DURATION_SESSION);
blob.open(BLOB.MODE_READWRITE);
OutputStream out = blob.getBinaryOutputStream();
out.write((byte[]) value);
out.flush();
out.close();
blob.close();
//((oracle.jdbc.OraclePreparedStatement) (st)).setBLOB(index, blob);
st.setBlob( index, (Blob)blob );
// } else {
//
//st.setBlob(index, Hibernate.createBlob((byte[]) value));
// }
} catch (ClassNotFoundException e) {
// could not find the class with reflection
throw new HibernateException("Unable to find a required class.\n" + e.getMessage());
} catch (IOException e) {
throw new SQLException("failed write to blob" + e.getMessage());
}
}
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;
}
public Object assemble(Serializable arg0, Object arg1) throws HibernateException {
// TODO Auto-generated method stub
return null;
}
public Serializable disassemble(Object arg0) throws HibernateException {
// TODO Auto-generated method stub
return null;
}
public int hashCode(Object arg0) throws HibernateException {
// TODO Auto-generated method stub
return 0;
}
public Object replace(Object arg0, Object arg1, Object arg2) throws HibernateException {
// TODO Auto-generated method stub
return null;
}
} |
Partager