JPA - insert and retrieve clob and blob types

Guest Author
This article describes about the JPA feature for handling clob and blob data types.You will learn the following in this article.

  • @Lob annotation
  • Client code to insert and retrieve the clob/blob types
  • End to End ADFaces application to retrieve the image from database table and display it in web page.

Use Case Description

Persisting and reading the image from database using JPA clob/blob type.

@Lob annotation

By default, TopLink JPA assumes that all persistent data can be represented as typical database data types.

Use the @Lob annotation with a basic mapping to specify that a persistent property or field should be persisted as a large object to a database-supported large object type.

A Lob may be either a binary or character type. TopLink JPA infers the Lob type from the type of the persistent field or property.

For string and character-based types, the default is Clob. In all other cases, the default is Blob.


Below code shows how to use this annotation to specify that persistent field picture should be persisted as a Blob.

public class Person implements Serializable {
    @Column(nullable = false, length = 20)
    private String name;
    @Column(nullable = false)
    private byte[] picture;

    @Column(nullable = false, length = 20)


Client code to insert and retrieve the clob/blob types

Reading a image file and inserting to Database table

Below client code will read the image from a file and persist to Person table in database.

                      Person p=new Person();
                      p.setPicture(writtingImage("Image location"));// - c:\images\test.jpg


//Retrieving the image from Database table and writing to a file

                      List<Person> plist=sessionEJB.getPersonFindAll();//
                      Person person=(Person)plist.get(0);//get a person object
                      retrieveImage(person.getPicture());   //get picture retrieved from Table

//Private method to create byte[] from image file 

private static byte[] writtingImage(String fileLocation) {
      System.out.println("file lication is"+fileLocation);
     IOManager manager=new IOManager();
        try {
           return manager.getBytesFromFile(fileLocation);
        } catch (IOException e) {
        return null;

//Private method to read byte[] from database and write to a image file 

  private static void retrieveImage(byte[] b) {
    IOManager manager=new IOManager();

        try {
        } catch (IOException e) {

End to End ADFaces application to retrieve the image from database table and display it in web page.

Please find the application in this link.

Following are the j2ee components used in the sample application.

  • ADFFaces(jspx page)
  • HttpServlet Class - Will make a call to EJB and retrieve the person object from person table.Read the byte[] and write to response using Outputstream.
  • SessionEJBBean - This is a session facade to make a local call to JPA entities
  • JPA Entity(Person.java) - Person java class with setter and getter method annotated with @Lob representing the clob/blob types for picture field.

Join the discussion

Comments ( 1 )
  • Santhosh Monday, February 13, 2012


    We are using JPA in our project.One if the database column is of datatype CLOB.In the entity for that table we have @Lob annotation for the CLOB field. The following is Entity code :



    private String cirConfigurationItem;

    Is it mandatory to have @Lob annotation.We tried without this annotation,still data got persisted in database.

    Note : Data is comma separated string ( eg : ABCDEFGH,HIJKLMNO ) which we store in CLOB column.

    Please let us know.


    Santhosh Kumar N

Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.