![]() |
VOOZH | about |
JPA (Java Persistence API, sometimes also referred to as Jakarta Persistence API) is a tool to connect Java applications with databases for optimizing memory storage for data, thus providing a smoother User Experience (UX). In simple terms, JPA simplifies database access from within the Java application, by allowing to work with objects rather than SQL statements. JPA allows working with POJOs (Plain Old Java Objects) right from desktop apps, providing developers with an ORM (Object Relational Mapping) facility for managing relational data.
JPA has 6 layers in its class-level architecture:
Defining the Entity class (the Entity class maps to the required table schema present in the linked database):
@Entity
@Table(name = "employee")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String department;
// getters and setters
public Long getId()
{
return id;
}
public void setId(Long id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getDepartment()
{
return department;
}
public void setDepartment(String department)
{
this.department = department;
}
}Explanation of the annotations:
The keywords which starting with @ and containing useful inbuilt functionality are termed as annotations.
Annotation | Description |
|---|---|
@Entity | Used to tell Java to treat this class as the Entity (or model) class |
@Id | Sets the following variable as the primary key of the table in the database to which this Entity class is mapped |
@GeneratedValue(strategy = GenerationType.IDENTITY) | Generates unique, auto-incrementing values for the variable mapped to the primary key of the table in the database |
@Column | Specifies to the Java class that the following variable is mapped to the corresponding position column in the table of the mapped database |
The Persistence Unit manages entity classes, typically defined in a persistence.xml file:
<persistence xmlns="http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/persistence/index.html" version="2.2">
<persistence-unit name="myPersistenceUnit">
<class>com.example.Employee</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/mydb"/>
<property name="javax.persistence.jdbc.user" value="user"/>
<property name="javax.persistence.jdbc.password" value="password"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL8Dialect"/>
</properties>
</persistence-unit>
</persistence>The EntityManager serves as the primary interface for interactions with the Persistence Context, allowing CRUD (Create-Read-Update-Delete) operations on entities:
The EntityManagerFactory is a factory for EntityManager instances. The createEntityManager() method creates a new EntityManager instance.
JPQL is used to execute queries for CRUD operations on the database:
List<Employee> employees = em.createQuery("SELECT e FROM Employee e", Employee.class).getResultList();The Database Layer contains the relational database that stores all the data of the application. The tables in the database are mapped to entity classes in the Java application.
SQL Code Example for creating a database and a table in it:
CREATE DATABASE mydb;
USE mydb;
CREATE TABLE users (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE
);