使用sql保存图像休眠(不要使用hql)

时间:2017-02-13 23:00:37

标签: java sql oracle hibernate

我需要使用hibernate在表中保存 InputStream或byte [] (配置文件图像)。 这里代码:

@Override
public void actualizarFotoPerfil(String id, byte[] image) throws CotrafaException {

    Session session = this.getHibernateTemplate().getSessionFactory().getCurrentSession();

    String sql = "UPDATE TS_USU_FOTOS SET FOTO = ? WHERE CLIENTE = ?"; 
    SQLQuery query = session.createSQLQuery(sql);

    Blob b = Hibernate.createBlob(image);
    query.setParameter(0, b.toString());
    query.setParameter(1, id);

    query.executeUpdate();

    session.flush();

}

BD中的表

create table TS_USU_FOTOS
(
  cliente NUMBER(8) not null,
  foto    BLOB
)

并且错误是:     引起:java.sql.SQLSyntaxErrorException:ORA-00932:tipos de dato inconsistentes:se esperaba NUMBER se ha obtenido BINARY     ADVERTENCIA:/pages/common/cambiarFoto.xhtml @ 34,74 fileUploadListener =“#{barraBB.actualizarFoto}”:org.hibernate.exception.SQLGrammarException:无法执行本机批量操作查询     javax.el.E​​LException:/pages/common/cambiarFoto.xhtml @ 34,74 fileUploadListener =“#{barraBB.actualizarFoto}”:org.hibernate.exception.SQLGrammarException:无法执行本机批量操作查询         at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:111)         在org.primefaces.component.fileupload.FileUpload.broadcast(FileUpload.java:310)         在javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:755)         在javax.faces.component.UIViewRoot.processDecodes(UIViewRoot.java:931)         at com.sun.faces.lifecycle.ApplyRequestValuesPhase.execute(ApplyRequestValuesPhase.java:78)         在com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)         在com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)         在javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)

1 个答案:

答案 0 :(得分:1)

我的猜测是你有FOTOblob列。但是您将字符串作为参数b.toString()传递。尝试删除toString,然后传递b