I have a SibImage object which contains the URL of my image. My datamodel contains an attribute: SibImage image;
I thus create a hibernate type to parse my address of image in BLOB (oracle). Here is my SibImageBlobType class (only the méthoe Set interests us):
Code:
public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
if (value == null) {
// pas d'image
st.setNull(index, Types.BLOB);
return;
}
InputStream inStream = null;
try {
// création d'une connexion vers l'image
URLConnection urlCnx = ((SibImage) value).getImageUrl().openConnection();
//récupération du flux
inStream = urlCnx.getInputStream();
// lecture du flux et écriture dans un tableau de bytes
ByteArrayOutputStream byteArrayStream = new ByteArrayOutputStream(1024);
for (int val; (val = inStream.read()) != -1;) {
byteArrayStream.write(val);
}
st.setBlob(index, BLOB.empty_lob());
}
This code is right so I add this:
Code:
public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
if (value == null) {
// pas d'image
st.setNull(index, Types.BLOB);
return;
}
InputStream inStream = null;
try {
// création d'une connexion vers l'image
URLConnection urlCnx = ((SibImage) value).getImageUrl().openConnection();
//récupération du flux
inStream = urlCnx.getInputStream();
// lecture du flux et écriture dans un tableau de bytes
ByteArrayOutputStream byteArrayStream = new ByteArrayOutputStream(1024);
for (int val; (val = inStream.read()) != -1;) {
byteArrayStream.write(val);
}
Connection conn = st.getConnection().getMetaData().getConnection();
BLOB blob = BLOB.createTemporary(conn, false, oracle.sql.BLOB.DURATION_SESSION);
blob.open(BLOB.MODE_READWRITE);
OutputStream out = blob.getBinaryOutputStream();
try {
out.write(byteArrayStream.toByteArray());
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);
}
I obtain this error :
Code:
Caused by: java.lang.NoSuchMethodError: oracle.sql.BLOB.createTemporary(Ljava/sql/Connection;ZI)Loracle/sql/BLOB;
at fr.ggl.fwk.service.hibernate.types.SibImageBlobType.nullSafeSet(SibImageBlobType.java:119)