Monday, January 4, 2010

How to use Java Persistence API (JPA)?

The Java Persistence API, sometimes referred to as JPA, is a Java programming language framework that allows developers to manage relational data in applications. A persistence entity is a lightweight Java class whose state is typically persisted to a table in a relational database. Instances of such an entity correspond to individual rows in the table. Entities typically have relationships with other entities, and these relationships are expressed through object/relational metadata. Object/relational metadata can be specified directly in the entity class file by using annotations, or in a separate XML descriptor file distributed with the application.

Let's start with Simple Java Code.

Consider following table in any rational database.

Table Structure:

CREATE TABLE `persistencemsg` (
`messageCode` INT(10) NOT NULL,
`sessionId` VARCHAR(500) NULL,
`message` BLOB(4294967295),
PRIMARY KEY (`messageCode`),
INDEX `messageCode` (`messageCode`)

Now, we will write corresponding entity class ( for the same. @Table annotation represents table name of the table, in our case it is 'PERSISTENCEMSG'. This entity class is always implements Serializable to write thread safe code. It contains getter and setter method for each column in the table. @Column annotation represents column name corresponding to the each column in the table. Go through below Java code.


import java.nio.ByteBuffer;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

* @author naman
@Table(name = "PERSISTENCEMSG")
public class JPIPersistenceMessage implements Serializable {

private byte [] message;
private int messageCode;
private String sessionId;

* @return the message
@Column(name = "MESSAGE")
public byte[] getMessage() {
return message;

* @param message the message to set
public void setMessage(byte[] message) {
this.message = message;

* @return the messageCode
@Column(name = "MESSAGECODE")
public int getMessageCode() {
return messageCode;

* @param messageCode the messageCode to set
public void setMessageCode(int messageCode) {
this.messageCode = messageCode;

* @return the sessionId
@Column(name = "SESSIONID")
public String getSessionId() {
return sessionId;

* @param sessionId the sessionId to set
public void setSessionId(String sessionId) {
this.sessionId = sessionId;

Now, how do we use this class for add/edit/update/find operation? First, we need to connect to the database so we have to write one XML file called 'persistence.xml' which is part of META-INF folder in your application. It contains properties to connect to the database. Please change as per your requirement.

XML File: persistence.xml

<persistence-unit name="persist_name">
<property name="toplink.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="toplink.jdbc.password" value="password"/>
<property name="toplink.jdbc.url" value="jdbc:mysql://localhost:3306/databasename"/>
<property name="toplink.jdbc.user" value="username"/>
<property name="" value="3"/>
<property name="" value="1"/>
<property name="" value="true"/>
<property name="toplink.jdbc.write-connections.max" value="5"/>
<property name="toplink.jdbc.write-connections.min" value="2"/>
<property name="toplink.cache.type.default" value="Full"/>
<property name="" value="MySQL4"/>

Let's write manager class ( for database operations. We need to load EntityManagerFactory using persistence-unit name given in persistence.xml file, in this case it is 'persist_name'. Now load EntityManager based on EntityManagerFactory. Now you can call all database operations like add/delete/find on this EntityManager object. User must have to start and close the transaction during each operation. Please go through below code.


import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

* @author naman
public class JPIPersistenceManager {

private static EntityManagerFactory emf;
private static EntityManager em;

public void createEntityManagerFactory(String persistenceUnitName) {
emf = Persistence.createEntityManagerFactory(persistenceUnitName);

private static void createEntityManager() {
em = emf.createEntityManager();

private static void closeEntityManager() {

private static void createTransactionalEntityManager() {

private static void closeTransactionalEntityManager() {

public void storeMessage(JPIPersistenceMessage persistenceMessage) {


public void deleteMessage(long messageCode) {

JPIPersistenceMessage persistenceMessageDelete = findMessage(messageCode);

public JPIPersistenceMessage findMessage(long messageCode) {

JPIPersistenceMessage jpipm = em.find(JPIPersistenceMessage.class, Long.toString(messageCode));
return jpipm;


public JPIPersistenceMessage updateMessage(byte[] message, long messageCode) {

Query query = em.createQuery(
"UPDATE JPIPersistenceMessage p SET p.message = :message where p.messageCode = :messageCode");
query.setParameter("message", message);
return findMessage(new Long(messageCode));

public static void main(String args[]) {
JPIPersistenceManager persistenceManager = new JPIPersistenceManager();

// Now call storeMessage,deleteMessage, updateMessage and findMessage on persistenceManager object.

Hope it should be useful to you. Please let me know any comment on this...

1 comment:

  1. Hi Naman,
    Thanks. It is useful.