【问题标题】:Insert byte array in the place of blob using hibernate使用休眠插入字节数组代替blob
【发布时间】:2012-12-19 20:42:35
【问题描述】:

我的控制器中的代码

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-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>

我的目标是将文件插入到数据库表中以代替 BLOB 对象。 但是我得到了这个

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)]

我尝试使用 ByteArrayInputStream 而不是 InputStream 进行上述操作,但徒劳无功。 谁能告诉我我在代码中的错误是什么?
提前致谢

【问题讨论】:

    标签: spring hibernate spring-mvc blob


    【解决方案1】:

    在您的模型中直接使用 byte[] 应该可以。即fileUpload.setFilename(byteArray)

    请记住,最好使用有意义的名称。有人可能认为fileUpload.getFileName() 返回的是文件名,而不是原始数据。

    【讨论】:

    • 太好了,谢谢它工作得很好。我也遵循了命名方法setFile(...)getFile()的提示
    猜你喜欢
    • 1970-01-01
    • 2015-05-24
    • 1970-01-01
    • 2015-02-08
    • 1970-01-01
    • 2015-05-23
    • 2013-02-28
    • 1970-01-01
    • 2011-08-24
    相关资源
    最近更新 更多