Open In App

JPA - Introduction

Last Updated : 08 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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
  • EntityManager
  • Persistence Context
  • Criteria API
  • JPQL (Java Persistence Query Language)
  • Persistence Unit

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.

  • Importance: Entities are the core components in the JPA as they can define the structure of the data in JPA application.

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.

  • Importance: EntityManager can be essential for all the database interactions in the JPA managed application. It acts as the factory for the querying and provides the transaction management of 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", "[email protected]");
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.

  • Importance: This context can helps manage the entity states like managed, detached and it can ensures that the data is synchronized with the database at the end of the transaction 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.

  • Importance: It provides the platform for the creating dynamic queries where the structure of the query is not known until the runtime, unlike the JPQL which is static.

Example Criteria Query:

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Employee> cq = cb.createQuery(Employee.class);
Root<Employee> employee = cq.from(Employee.class);
cq.select(employee).where(cb.like(employee.get("email"), "%example.com"));
TypedQuery<Employee> query = em.createQuery(cq);
List<Employee> 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.

  • Importance: JPQL can be designed to abstract database tables as the Java classes in the queries. Making it database agnostic and more integrated with the object oriented approach of the Java.

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.

  • Importance: It specifies the entity classes that are included in the persistence context and manages how these entities are configured with database.

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.


Next Article
Article Tags :
Practice Tags :

Similar Reads