Ola,
Estou com problemas para persisitr objetos no oracle um uma coluna do tipo blob usando hibernate.
O procedimento funcionava normalmente usando postgres, porem com no oracle nao funciona.
Eu difini o tipo to campo como blob no arquivo *.hbm.xml e na classe eu defini como array de bytes, eu tb tentei definir como java.sql.blob e mesmo assim o erro continuou.
Eu achei varios artigos na internet sobre blob e hibernate, mas parece que são para o hibernate antes da versao da 3, e eu estou o hibernate na versao 3
Desde ja muito obrigado
Segue abaixo como eu estou procedendo
public class Beta {
private Session session;
private void startHibernate() {
Configuration cfg = new Configuration().addResource("br/com/verano/Teste.hbm.xml")
.setProperty("hibernate.connection.driver_class","oracle.jdbc.driver.OracleDriver")
.setProperty("hibernate.connection.url","jdbc:oracle:thin:@sm01:1523:tsor10")
.setProperty("hibernate.connection.username", "portexec")
.setProperty("hibernate.connection.password", "portexec")
.setProperty("hibernate.dialect", "org.hibernate.dialect.OracleDialect")
.setProperty("hibernate.cache.provider_class","org.hibernate.cache.OSCacheProvider")
.setProperty("hibernate.show_sql", "true")
.setProperty("hibernate.format_sql", "false")
.setProperty("hibernate.cache.provider_class","org.hibernate.cache.EhCacheProvider");
SessionFactory sessions = cfg.buildSessionFactory();
session = sessions.openSession();
}
public void makePersistent() throws Exception {
startHibernate();
Transaction transaction = session.beginTransaction();
Teste entity = new Teste();
entity.setPk(1);
entity.setNome("file1");
entity.setMyfile(ImagemUtil.fileToBlob(new File("c:/devicetable.log")));
session.update(entity);
transaction.commit();
}
public static void main(String args[]) {
try {
new Beta().makePersistent();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class Teste {
private long pk;
private String nome = null;
private Blob myfile = null;
}
<hibernate-mapping>
<class name="br.com.verano.Teste" table="teste">
<id name="pk" type="long">
<column name="pk" />
<generator class="assigned"></generator>
</id>
<property name="nome" type="string">
<column name="nome" not-null="true" />
</property>
<property name="myfile" type="blob">
<column name="myfile"/>
</property>
</class>
</hibernate-mapping>
public class ImagemUtil {
public static byte[] fileToByte(File imagem) throws Exception {
FileInputStream fis = new FileInputStream(imagem);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[8192];
int bytesRead = 0;
while ((bytesRead = fis.read(buffer, 0, 8192)) != -1) {
baos.write(buffer, 0, bytesRead);
}
return baos.toByteArray();
}
public static InputStream byteToInputStream(byte[] bytes) throws Exception {
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
return bais;
}
public static Blob fileToBlob(File file) throws Exception{
byte[] fileInByte = fileToByte(file);
return Hibernate.createBlob(fileInByte);
}
}