我在将一个字节数组保存到Informix数据库时遇到了麻烦,我将告诉你我是如何尝试的:
在Fichero.java中我还有其他的东西:
@Column(name="fichero", columnDefinition="blob")
private byte[] contenido;
我必须这样做,因为我不能让Informix + Hibernate接受@Lob注释(是的,我试过扩展Hibernate的方言解决方法,但它对我不起作用)
然后我读取一个文件并以这种方式将其转换为bytearray: FileManager.java
private 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对象。
Buuuuut,我得到以下例外。
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文件以及它尝试保存对象的行号...
有什么想法吗?
答案 0 :(得分:1)
最后,我想出了一个更简单的解决方法:我只是将数据库中的列定义为字节...并且它的工作方式就像魅力...但是反正方式......