JPA - insert and retrieve clob and blob types
Posted
by pachunoori.vinay.kumar(at)oracle.com
on Oracle Blogs
See other posts from Oracle Blogs
or by pachunoori.vinay.kumar(at)oracle.com
Published on Fri, 25 Feb 2011 10:24:33 +0530
Indexed on
2011/02/25
7:29 UTC
Read the original article
Hit count: 717
- @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.
© Oracle Blogs or respective owner