JPA Introduction (original) (raw)

Last Updated : 08 Jul, 2024

**JPA can be defined as **Java Persistence API. It is the Java specification that can provide a standardized way to manage the relational data in Java applications. JPA facilitates the management of the database operations and mapping of the Java objects to database tables by defining the concepts and APIs. It serves as the bridge between the object-oriented domain models and relational database systems.

JPA (Java Persistence API)

The Java Persistence API (JPA) is the Java API specification that bridges the gap between relational databases and object-oriented programming by handling the mapping of the Java objects to the database tables and vice-versa. This process is known as the Object Relational Mapping (ORM). JPA can define the way Java classes (entities) are mapped to the database tables and how they can interact with the database through the EntityManager, the Persistence context, and transactions.

Key Terminologies in JPA

Entity

An entity is the lightweight and persistent domain object. It represents the table in the database and each entity instance corresponds to the row in that table.

**Example Entity Class:

import javax.persistence.*;

@Entity @Table(name = "employees") public class Employee { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id;

@Column(name = "name")
private String name;

@Column(name = "email")
private String email;

// Constructors, getters, and setters

}

EntityManager

The EntityManager is an interface that can be used to interact with the persistence context. It provides the operations to create, read, update and delete entities of the JPA application.

**Example of Using EntityManager:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("my-persistence-unit"); EntityManager em = emf.createEntityManager();

em.getTransaction().begin(); Employee emp = new Employee("John Doe", "john.doe@example.com"); em.persist(emp); em.getTransaction().commit();

em.close(); emf.close();

Persistence Context

The persistence context is the set of the entity instances in which for any persistent entity identity and there is the unique entity instance. Within the persistence context, entity instances and their lifecycle are managed of the application.

Criteria API

The Criteria API is the programmatically defined the type safe query API used to define the queries for the entities and their persistent state of the JPA application.

**Example Criteria Query:

CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery cq = cb.createQuery(Employee.class); Root employee = cq.from(Employee.class); cq.select(employee).where(cb.like(employee.get("email"), "%example.com")); TypedQuery query = em.createQuery(cq); List results = query.getResultList();

JPQL (Java Persistence Query Language)

JPQL is the query language defined as the part of the JPA specification for the making queries against the entities stored in the database of JPA application.

Persistence Unit

The persistence unit can defines the set of all the entity classes that are managed by the EntityManager in the application. It can configured in the persistence.xml file of the JPA application.

Conclusion

JPA is the powerful specification that can simplifies the interaction between the Java applications and relational databases. By the abstracting database interactions and it can providing the rich API for the handling of their applications while reducing the boilerplate code associated with the database operations.