8

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.

2
  • Can you show some code (ImageService and ImageServlet classes) ? you may forget to add the @Component or @Service annotation on your imageService class. Commented Jan 29, 2012 at 13:31
  • hibernate relies 100% on JDBC Commented Jan 30, 2012 at 2:53

1 Answer 1

7

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.

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

6 Comments

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/…
@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.
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.
@George: you may find this useful stackoverflow.com/questions/3503841/…
I think he meant @Lob not @Blob
|

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.