sql >> Base de Datos >  >> RDS >> Oracle

Cómo persistir BLOB GRANDES (> 100 MB) en Oracle usando Hibernate

Estaba teniendo los mismos problemas que tú al intentar mapear usando el tipo "blob". Aquí hay un enlace a una publicación que hice en el sitio de hibernación:https://forum.hibernate.org/viewtopic.php?p=2452481#p2452481

Hibernate 3.6.9
Controlador Oracle 11.2.0.2.0
Base de datos Oracle 11.2.0.2.0

Para solucionar el problema, utilicé un código que tenía un tipo de usuario personalizado para Blob, tenía el tipo de retorno java.sql.Blob.

Estas son las implementaciones de métodos clave de este tipo de usuario:

public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {

   Blob blob = rs.getBlob(names[0]);
   if (blob == null)
      return null;

   return blob;
}

public void nullSafeSet(PreparedStatement st, Object value, int index)
     throws HibernateException, SQLException {
   if (value == null) {
      st.setNull(index, sqlTypes()[0]);
   }
   else {
      InputStream in = null;
      OutputStream out = null;
      // oracle.sql.BLOB
      BLOB tempBlob = BLOB.createTemporary(st.getConnection(), true, BLOB.DURATION_SESSION);
      tempBlob.open(BLOB.MODE_READWRITE);
      out = tempBlob.getBinaryOutputStream();
      Blob valueAsBlob = (Blob) value;
      in = valueAsBlob.getBinaryStream();
      StreamUtil.toOutput(in, out);
      out.flush();
      StreamUtil.close(out);
      tempBlob.close();
      st.setBlob(index, tempBlob);
      StreamUtil.close(in);
   }
}