Freakyuser
Freakyuser

Reputation: 2814

Insert byte array in the place of blob using hibernate

The code in my controller

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();

The hibernate mapping file

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

My aim is to insert a file into a db table in the place of BLOB object. However I am getting this

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

I tried the above using ByteArrayInputStream instead of InputStream , but in vain. Can anyone please let me know what is my mistake in the code?
Thanks in Advance

Upvotes: 1

Views: 1737

Answers (1)

Jose Luis Martin
Jose Luis Martin

Reputation: 10709

Use byte[] directly in your model, should work. ie, fileUpload.setFilename(byteArray).

Remember that it is better to use meaningful names. Someone might expect that fileUpload.getFileName() return the file name, not the raw data.

Upvotes: 1

Related Questions