Table of contents
Jakarta Persistence API (formerly known as Java Persistence API) provides developers with an object/relational mapping facility to manage relational data in Java applications.
Persistence means any mechanism by which Java objects outlive the application process that created them. The JPA specification lets you define which objects should be persisted and how they are persisted in Java applications.
Popular JPA implementations are:
Hibernate
EclipseLink
Every JPA implementation provides some kind of ORM layer. The ORM layer converts Java classes and objects so that they can be stored and managed in a relational database.
Configuring JPA
To use JPA in the project, include the required dependencies
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.2.Final</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.197</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
</dependencies>
Configure the datastore and the JPA provider in persistence.xml.
<persistence xmlns="http://java.sun.com/xml/ns/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">
<persistence-unit name="Movies" transaction-type="RESOURCE_LOCAL">
<!-- Persistence provider -->
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<!-- Entity classes -->
<class>com.example.jpa.model.Movie</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:bookstore" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="show_sql" value="true"/>
<property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.use_sql_comments" value="true"/>
</properties>
</persistence-unit>
</persistence>
Data Persistence with JPA
Use annotations to inform JPA which objects should be persisted and how they should be persisted.
@Entity
@Table(name = "MOVIE")
public class Movie {
@Id
@GeneratedValue
private Integer id;
//class body
}
@Entity annotation to specify that this class and its objects should be persisted.
@Table annotation to specify the table name where this class and its objects should be stored.
@Id annotation to specify the id field as a primary key
There are many object/relational mapping annotations to map the classes to database tables.
Once you have mapped a class to a database table and its fields to the columns in the table, you have everything you need to create, retrieve, update, and delete that class in the database.
Configure EntityManager
EntityManager is the class that performs database interactions in JPA. This class is initialized through a configuration file named persistence.xml. This file is found in the META-INF folder which is packaged in the jar/war file.
First, we create an EntityManager
using the EntityManagerFactory
retrieved from the Persistence
class:
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("Movies");
EntityManager entityManager = entityManagerFactory.createEntityManager();
//code to persist movie object
entityManager.persist(movie);
entityManager.persist(movie) instructs the JPA to persist the object.