Spring 使用hibernate在blob位置插入字节数组
我的控制器中的代码Spring 使用hibernate在blob位置插入字节数组,spring,hibernate,spring-mvc,blob,Spring,Hibernate,Spring Mvc,Blob,我的控制器中的代码 FileInsertion fileInsertion = new FileInsertion(); FileUpload fileUpload = new FileUpload(); fileUpload.setFilename((InputStream) new ByteArrayInputStream(byteArray)); //byteArray is the file converted into a byte[] fileInsertion.insert
FileInsertion fileInsertion = new FileInsertion();
FileUpload fileUpload = new FileUpload();
fileUpload.setFilename((InputStream) new ByteArrayInputStream(byteArray));
//byteArray is the file converted into a byte[]
fileInsertion.insertFile(fileUpload);
//the following happens in a separate method
trns = session.beginTransaction();
session.save(fileUpload);
session.getTransaction().commit();
hibernate映射文件
<hibernate-mapping>
<class name="com.sort.process.FileUpload" table="fileupload">
<meta attribute="class-description">
This class contains the file upload detail.
</meta>
<id name="Id" type="int" column="Id">
<generator class="increment" />
</id>
<property name="filename">
<column name="filename" />
</property>
</class>
</hibernate-mapping>
我用ByteArrayInputStream
而不是InputStream
尝试了上述方法,但没有成功。
谁能告诉我代码中的错误是什么吗?提前感谢在您的模型中直接使用byte[],应该可以。例如,
fileUpload.setFilename(byteArray)
记住,最好使用有意义的名称。有人可能希望
fileUpload.getFileName()
返回文件名,而不是原始数据。直接在模型中使用byte[]应该可以。例如,fileUpload.setFilename(byteArray)
记住,最好使用有意义的名称。有人可能希望
fileUpload.getFileName()
返回文件名,而不是原始数据。太好了,谢谢,它工作得很好。此外,我还遵循了将方法命名为setFile(…)
和getFile()
的技巧,非常好,谢谢,它工作得很好。我还遵循了命名方法setFile(…)
和getFile()
Initial SessionFactory creation failed.org.hibernate.MappingException: Could not determine type for: java.io.InputStream, at table: fileupload, for columns: [org.hibernate.mapping.Column(filename)]