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

Note

  • 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);

E.g.

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()

E.g

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()

E.g

EntityTransaction entTransaction = entManager.getTransaction();

Note

EntityTransaction contains the following methods in order to complete Transaction.

public void commit()
public void rollback()

Note

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)

Note

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
 *
 */
@Entity
@Table(name = "employee")
public class Employee {
   @Column(name = "emp_name")
   private String empName;
   @Id
   @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;
   }

   @Override
   public String toString() {
      return "Employee [empName=" + empName + ", empId=" + empId + ", empAddress=" + empAddress + ", salary=" + "]";
   }
}
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
   http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
   version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
   <persistence-unit name="employee">
   <provider>org.hibernate.ejb.HibernatePersistence</provider>
   <mapping-file>com.ashok.spring.jpa.beans.Employee</mapping-file>
   <properties>
      <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" />
   </properties>
</persistence-unit>
</persistence>
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.setEmpId("0087"); 
      emp.setEmpName("Ashok Kumar"); 
      emp.setEmpAddress("Bhimavaram"); 
      EntityTransaction tx = entManager.getTransaction(); 
      tx.begin();
      entManager.persist(emp); 
      tx.commit(); 
      System.out.println("Employee Inserted Succssfully");
   }
}
Java Persistence API
Scroll to top