我有问题将一个字节流数组保存到我的Informix数据库中,我将向您展示我的尝试:
在Fichero.java文件中,除其他部分外,我有以下代码:
@Column(name="fichero", columnDefinition="blob")
private byte[] contenido;
我不得不这样做,因为我不能让Informix+Hibernate接受@Lob注释(是的,我尝试过扩展Hibernate的方言解决方法,但对我来说不起作用)
然后我按照以下方式读取文件并将其转换为字节数组:
FileManager.javaprivate byte[] fromFile2ByteArray(String nombreFichero) throws FileNotFoundException, IOException{
File file = new File(nombreFichero);
InputStream is = new FileInputStream(file);
byte[] contenido = new byte[(int)file.length()];
is.read(contenido);
is.close();
return contenido;
}
接下来我尝试使用以下方式将其保存到我的数据库中:
FicheroDAO.java
public void save(Object object) {
getHibernateTemplate().save(object);
}
需要翻译的内容为:
这里的对象是我的Fichero对象。
但是,我得到了以下异常。
Hibernate:
/* insert es.cosas.modelo.datos.dominio.fichero.Fichero
*/ insert
into
cosas_fichero
(fichero, idEnvio, nombrefichero, respuesta)
values
(?, ?, ?, ?)
WARN : org.hibernate.util.JDBCExceptionReporter - SQL Error: -841, SQLState: S0000
ERROR: org.hibernate.util.JDBCExceptionReporter - Name must start with a letter or '_' and contain letters, digits, or '_'.
22-jun-2012 9:25:32 org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() para servlet Spring MVC Dispatcher Servlet lanzó excepción
java.sql.SQLException: Name must start with a letter or '_' and contain letters, digits, or '_'.
at com.informix.util.IfxErrMsg.getSQLException(IfxErrMsg.java:379)
at com.informix.jdbc.IfxSqli.addException(IfxSqli.java:3109)
at com.informix.jdbc.IfxSqli.receiveError(IfxSqli.java:3419)
at com.informix.jdbc.IfxSqli.dispatchMsg(IfxSqli.java:2282)
at com.informix.jdbc.IfxSqli.receiveMessage(IfxSqli.java:2202)
at com.informix.jdbc.IfxSqli.executeFastPath(IfxSqli.java:5475)
at com.informix.jdbc.IfxSqli.executeFastPath(IfxSqli.java:5400)
at com.informix.jdbc.IfxSmartBlob.IfxLoColInfo(IfxSmartBlob.java:306)
at com.informix.jdbc.IfxSmBlob.createSmartLob(IfxSmBlob.java:751)
at com.informix.jdbc.IfxSmBlob.createBlobOnServer(IfxSmBlob.java:497)
at com.informix.jdbc.IfxResultSet.executeUpdate(IfxResultSet.java:301)
at com.informix.jdbc.IfxStatement.executeUpdateImpl(IfxStatement.java:885)
at com.informix.jdbc.IfxPreparedStatement.executeUpdate(IfxPreparedStatement.java:279)
at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
at org.hibernate.id.insert.AbstractSelectingDelegate.performInsert(AbstractSelectingDelegate.java:57)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2329)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2836)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:267)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:321)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:705)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:693)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:689)
at org.springframework.orm.hibernate3.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:686)
at org.springframework.orm.hibernate3.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:1)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
at org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:683)
下一个异常行指向FicheroDAO.java文件以及尝试保存对象的行号... 有什么想法吗?