Java Persistence API

Java Persistence API

In this tutorial, we are going to discuss Java Persistence API. The Java Persistence API provides Java developers with an object/relational mapping facility for managing relational data in Java applications.

  • JPA is an API, and it can be used to perform database operations in enterprise applications with ORM implementation tools.
  • J2EE provided JPA along with the EJB3.0 version as a persistence mechanism.
  • JPA is a specification provided by SUN Microsystems, and it is implemented by some third-party vendors like JBOSS, Eclipse Foundations, Apache, etc.
  • JPA follows ORM rules regulations to achieve data persistency in enterprise applications, and the following tools implement it.
Hibernate   -------> JBOSS
EclipseLink -------> Eclipse Foundation
Open JPA    -------> Apache Software Foundations


  • If we want to use JPA in enterprise applications, we must use either of the JPA implementations.
  • If we want to prepare JPA applications with a “Hibernate” JPA provider, we must use the following steps.
  1. Create Java project in Eclipse with JPA library which includes all Hibernate JARs.
  2. Create Entity class under src folder.
  3. Create mapping File or Use JPA annotations in POJO class.
  4. Create JPA configuration File[persistence.xml]
  5. Create Test Application.

To prepare Test application in JPA we have to use the following steps.

  1. Create EntityManagerFactory Object.
  2. Create EntityManager Object.
  3. Create EntityTransaction Object as per the requirement
  4. Perform Persistence operation
  5. Perform Commit or rollback operations if we use EntityTransaction.
1. Create EntityManagerFactory Object

javax.persistence.EntityManagerFactory is a Factory class. It can manage the number of EntityManager object. To get the EntitymanagerFactory class object, we have to use the following method from javax.persistence.Persistence class.

public static EntityManagerFactory createEntityManagerFactory(String persistence_Unit_Name);


EntityManagerFactory factory = Persistence.createEntitymanagerFactory("std");
2. Create EntityManager Object

javax.persistence.EntityManager is an interface, it able to provide predefined Library to perform persistence operations. To get EntityManager object we have to use the following method from EntiotyManagerFactory.

public EntityManager createEntityManager()


EntityManager entManager = factory.createEntitymanager();
3. Create EntityTransaction Object as per the requirement

javax.persistence.EntityTransaction is a class, it able to provide Transaction support in JPA applications inorder to perform persistence operations. To get EntityTramsaction object we have to use the following method from EntityManager.

public EntityTransaction getTransaction()


EntityTransaction entTransaction = entManager.getTransaction();


EntityTransaction contains the following methods in order to complete Transaction.

public void commit()
public void rollback()


EntityTranmsaction is required for only non-select operations only, not for select operations.

4. Perform Persistence operation

To perform Persistence operations, we have to use the following methods from EntityManager object.

  • public Object find (Class entity_Class, Serializable pk_Value)
  • public void persist (Object obj)
  • public void remove (Object obj)


To perform Updations, first we have to get Entity object from Database table by using find() method then we have to use set New data to Entity Object then perform commit operation.

package com.ashok.spring.jpa.beans;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

 * @author ashok.mariyala
@Table(name = "employee")
public class Employee {
   @Column(name = "emp_name")
   private String empName;
   @Column(name = "emp_id")
   private String empId;
   @Column(name = "emp_address")
   private String empAddress;

   public String getEmpName() {
      return empName;

   public void setEmpName(String empName) {
      this.empName = empName;

   public String getEmpId() {
      return empId;

   public void setEmpId(String empId) {
      this.empId = empId;

   public String getEmpAddress() {
      return empAddress;

   public void setEmpAddress(String empAddress) {
      this.empAddress = empAddress;

   public String toString() {
      return "Employee [empName=" + empName + ", empId=" + empId + ", empAddress=" + empAddress + ", salary=" + "]";
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns:xsi=""
   version="2.0" xmlns="">
   <persistence-unit name="employee">
      <property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver" />
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/employee" />
      <property name="javax.persistence.jdbc.user" value="root" />
      <property name="javax.persistence.jdbc.password" value="ashok" />
      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
      <property name="hibernate.show_sql" value="true" />
      <property name="hibernate.format_sql" value="true" />
package com.ashok.spring.jpa.test;

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

import com.ashok.spring.jpa.beans.Employee;
 * @author ashok.mariyala
public class TestJpaApplication {
   public static void main(String[] args) {
      EntityManagerFactory factory = Persistence.createEntityManagerFactory("employee"); 
      EntityManager entManager = factory.createEntityManager();
      Employee emp = new Employee(); 
      emp.setEmpName("Ashok Kumar"); 
      EntityTransaction tx = entManager.getTransaction(); 
      System.out.println("Employee Inserted Succssfully");
Java Persistence API
Scroll to top