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.
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 pictureshould be persisted as a Blob.
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.
Retrieving the image from Database table and writing to a file
Private method to create byte from image file
Private method to read byte from database and write to a image file