1

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Excellent, thank you it worked just fine. Also I followed the tip of naming the method setFile(...) and getFile()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.