Java 使用sql保存图像休眠(don´;t hql)
我需要使用hibernate在表中保存InputStream或byte[](配置文件图像)。 此处代码:Java 使用sql保存图像休眠(don´;t hql),java,sql,oracle,hibernate,Java,Sql,Oracle,Hibernate,我需要使用hibernate在表中保存InputStream或byte[](配置文件图像)。 此处代码: @Override public void actualizarFotoPerfil(String id, byte[] image) throws CotrafaException { Session session = this.getHibernateTemplate().getSessionFactory().getCurrentSession(); String
@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不一致:se esperaba数字se ha obtenido二进制文件
广告语:/pages/common/cambiarFoto.xhtml@34,74 fileUploadListener=“#{barraBB.realizarfoto}”:org.hibernate.exception.sqlgrammareexception:无法执行本机批量操作查询
javax.el.ELException:/pages/common/cambiarFoto.xhtml@34,74 fileUploadListener=“#{barraBB.realizarfoto}”:org.hibernate.exception.SQLGrammarException:无法执行本机批量操作查询
位于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)
在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)。但您将字符串作为参数
b.toString()
传递。尝试删除toString
并传递b
为什么要在blob上调用toString?尝试使用query.setParameter(0,b)
或query.setBinary(0,图像)代码>错误是:se esperapa NUMBER se ha obtenido BINARY
。然后看起来像FOTO
是typeNUMBER
。如果不了解数据库模式,我们将无法进一步帮助您。但一般来说,您需要在Java中设置与在数据库中相同的类型。如果FOTO
是一个数字,那么在query.setParameter(0,value)
中,value
必须是java中的一个数字类型(整型、长型等)。您可以在这里找到映射:啊,好的,CLIENTE
列是DB中的数字,但您将字符串id
作为参数传递<代码>id
必须是数字类型。