JPA - insert and retrieve clob and blob types

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.

Example

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 {
    @Id
    @Column(nullable = false, length = 20)
    private String name;
    @Column(nullable = false)
    @Lob
    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.setName("Tom");
                      p.setSex("male");
                      p.setPicture(writtingImage("Image location"));// - c:\images\test.jpg

                      sessionEJB.persistPerson(p);

//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 {
            manager.putBytesInFile("c:\\webtest.jpg",b);
        } 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.
Comments:

Hi
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 :

@Lob
@Column(name="CIR_CONFIGURATION_ITEM")
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.

Regards
Santhosh Kumar N

Posted by Santhosh on February 13, 2012 at 12:02 PM IST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Tips and Tricks from Oracle's JDeveloper & ADF QA

Search

Archives
« March 2015
SunMonTueWedThuFriSat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    
       
Today