Retrieve image from blob via Hibernate (not JDBC)

16,372

I hope you are storing the image in the table as a BLOB type(If not try to do so, as this is the best practice). Lets assume you have a Person class with the an image of the person stored in the DB. If you want to map this, just add a property in your person POJO that holds the image.

@Column(name="image")
@Blob
private Blob image;

When you display it, convert it to a byte[] and show.

private byte[] toByteArray(Blob fromImageBlob) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
      return toByteArrayImpl(fromImageBlob, baos);
    } catch (Exception e) {
    }
    return null;
  }



private byte[] toByteArrayImpl(Blob fromImageBlob, 
      ByteArrayOutputStream baos) throws SQLException, IOException {
    byte buf[] = new byte[4000];
    int dataSize;
    InputStream is = fromImageBlob.getBinaryStream(); 

    try {
      while((dataSize = is.read(buf)) != -1) {
        baos.write(buf, 0, dataSize);
      }    
    } finally {
      if(is != null) {
        is.close();
      }
    }
    return baos.toByteArray();
  }

You can see the below examples to know more about it.

  1. http://i-proving.com/2006/08/23/blobs-and-hibernate
  2. http://snehaprashant.blogspot.com/2008/08/how-to-store-and-retrieve-blob-object.html
  3. http://viralpatel.net/blogs/2011/01/tutorial-save-get-blob-object-spring-3-mvc-hibernate.html

As you can see there are multiple ways to do this. Choose the one appropriate for you.

Share:
16,372
Cichy
Author by

Cichy

Updated on June 11, 2022

Comments

  • Cichy
    Cichy almost 2 years

    I've found very nice solution of retrieving images from db/blob thanks to How to retrieve and display images from a database in a JSP page?

    But this is solution that uses JDBC connection on every image request.

    I'm using Spring 3 annotations and Hibernate 3.

    I tried to do similar thing by my 'imageService', which is autowired by annotation in ImageServlet class, but I got nullPointerException, which means that may imageService is not set by Dependency Injection.

    Is there any way how to solve that? I don't like to make single jdbc connection on image request.

  • Code Junkie
    Code Junkie about 12 years
    I'm still confused despite this nice explanation. Blob is an interface, so how are you using it as a data type? I'm using hibernate JPA and can't figure out how to map this properly. See post here stackoverflow.com/questions/10141373/…
  • ManuPK
    ManuPK about 12 years
    @George: As users of API we should not be worried if BLOB is a interface or class. Hibernate will have some class implementing the BLOB type and they will use it to fill the data. If you follow the examples properly, you should be able to map a BLOB type properly. meanwhile, u can upvote the answer if it is useful.
  • Code Junkie
    Code Junkie about 12 years
    I'm confused, your using an @Blob annotation which isn't a valid hibernate annotation. It's resulting with a compiling error. When using java.sql.Blob as a datatype I get yet another compiling error with netbeans. basic attributes can only be of the following types: .....I'm also using entities and not hibernate mapping files, so I'm not sure how to map my attribute datatype to the blob datatype. Some links linking back to hibernate are broken as well.
  • Adrien Be
    Adrien Be over 11 years
    @George: you may find this useful stackoverflow.com/questions/3503841/…
  • Jeshurun
    Jeshurun over 11 years
    I think he meant @Lob not @Blob
  • KNU
    KNU about 10 years
    @Column(name="image", columnDefinition="LONGBLOB") is another possible way for those who don't want to use @Blob annotations.