Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4049792
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:59:20+00:00 2026-05-20T13:59:20+00:00

I am trying to persist a uploaded file to database using JPA. The most

  • 0

I am trying to persist a uploaded file to database using JPA. The most “natural” way (to me) is to define domain object as:

@Entity
class UploadFile {
  ...
  public CommonsMultipartFile getFileData()
  {
    return fileData;
  }
}

But this won’t work since there is not such a database mapping type. Searching online, I found that people seems to adapt one of the two methods:

  • Define the field as java.sql.blob;
  • Define the field as byte[]

In the @Controller class, the incoming HttpServletRequest gets cast to MultipartHttpServletRequest in order to access the MultipartFile and convert it back to byte[] stream.

However, with this scheme, I have “random” results which puzzles me: from time to time, I ran into “bean property not readable” error, possible mismatch of return type of getter method on the byte[] field. I double and triple checked my Bean definition, and couldn’t find anything wrong.

I guess my questions are two folds: (1) Any idea why this error can happen in this context? (2) more importantly, what are the “recommended” way of handling uploaded file like this?

thanks

Oliver

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T13:59:21+00:00Added an answer on May 20, 2026 at 1:59 pm

    You are correct that either a java.sql.blob or a byte[] would be the most appropriate way to store an uploaded file in a database.

    You should not store a MultipartFile or a CommonsMultipartFile in a database because these are intended to be temporary objects. Note this from the MultipartFile javadoc:

    The file contents are either stored in memory or temporarily on disk.
    In either case, the user is responsible for copying file contents to a
    session-level or persistent store as and if desired. The temporary storages
    will be cleared at the end of request processing.

    I don’t quite follow what you’re doing with your HttpServletRequest but it doesn’t sound like the easiest way to handle the upload. The easiest way to handle an upload is to make the MultipartFile a parameter on your controller method (if you’re using Spring 3.0 or later, I think):

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public void exampleFileUpload(@RequestParam("file") MultipartFile file) {
        if (!file.isEmpty()) {
            try {
                byte[] fileBytes = file.getBytes();
    
                // Persist those bytes using JPA here
    
            } catch (IOException e) {
                // Exception handling here
            }
        }
    }
    

    This should work reliably because you’ve extracted the bytes into your byte[] array before the request has finished — and thus before the any temporary files used to handle the upload have been deleted.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to persist a string object in Mysql(5.0.67) using hibernate. My entity
I'm trying to setup Spring using Hibernate and JPA, but when trying to persist
I'm trying to persist an XML file to disk using LINQ. I have a
I am trying to persist an object with a collection of child objects. I
Hi I am trying to Persist a Map in Hibernate as follows: public class
I am trying to persist an Entity with a @Lob annotated String field. The
I am trying to persist the entity with constraint validation, when invoke persist -
Trying to insert values with Unicode Chars into a MySQL-database using Delphi 2010 and
I am trying to get LINQ to SQL to persist changes to an attached
Trying to find some simple SQL Server PIVOT examples. Most of the examples that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.