Introduction to JPA Architecture

DZone 's Guide to

Introduction to JPA Architecture

Here's everything you need to know about the Java persistence API.

· Java Zone ·
Free Resource

In this article, we will discuss the architecture (core classes and interfaces of Java Persistence API) of the JPA specification. The Java Persistence API (JPA) is the Java standard for mapping Java objects to a relational database. Mapping Java objects to database tables and vice versa is called object-relational mapping (ORM). The Java Persistence API (JPA) is one possible approach to ORM. Via JPA, the developer can map, store, update, and retrieve data from relational databases to Java objects and vice versa. JPA can be used in Java-EE and Java-SE applications. JPA is a specification and several implementations are available. Popular implementations are Hibernate, EclipseLink, and Apache OpenJPA.

JPA Architecture

Java Persistence API is a source-to-store business entities as relational entities. It shows how to define a PLAIN OLD JAVA OBJECT (POJO) as an entity and how to manage entities with relations.

Class-Level Architecture

The following image shows the class level architecture of JPA. It shows the core classes and interfaces of JPA.

Let's describe each of the units shown in the above architecture.

  1. EntityManagerFactory — This is a factory class of EntityManager. It creates and manages multiple EntityManager instances.
  2. EntityManager — It is an interface; it manages the persistence operations on objects. It works like a factory for Query instance.
  3. Entity — Entities are the persistence objects, stored as records in the database.
  4. EntityTransaction — It has one-to-one relationship with EntityManager. For each EntityManager, operations are maintained by the EntityTransaction class.
  5. Persistence — This class contains static methods to obtain the EntityManagerFactory instance.
  6. Query - This interface is implemented by each JPA vendor to obtain relational objects that meet the criteria.

The above classes and interfaces are used for storing entities into a database as a record. They help programmers by reducing their efforts to write codes for storing data into a database so that they can concentrate on more important activities such as writing codes for mapping the classes with database tables.

JPA Class Relationships

In the above architecture, the relations between the classes and interfaces belong to the javax.persistencepackage. The following diagram shows the relationship between them.

JPA Simple Example

Let's demonstrates the usage of core classes and interfaces of javax.persistence package.

JPA Entity

Here, we are using  @Entity,  @Id,  @GeneratedValue, and  @Column annotations for mapping between Java object to database table columns.

package net.javaguides.hibernate.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Table(name = "student")
public class Student {

    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;

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

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

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

    public Student() {


    public Student(String firstName, String lastName, String email) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;

    public int getId() {
        return id;

    public void setId(int id) {
        this.id = id;

    public String getFirstName() {
        return firstName;

    public void setFirstName(String firstName) {
        this.firstName = firstName;

    public String getLastName() {
        return lastName;

    public void setLastName(String lastName) {
        this.lastName = lastName;

    public String getEmail() {
        return email;

    public void setEmail(String email) {
        this.email = email;

    public String toString() {
        return "Student [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email + "]";

Usage of EntityManager, EntityManagerFactory, and EntityTransaction

Note that from above class-level architecture, we are using EntityManager, EntityManagerFactory, and EntityTransaction interfaces in this snippet. In this example, we usepersist() method to store student object into the database.

package net.javaguides.hibernate;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

import net.javaguides.hibernate.entity.Student;

public class App {
    public static void main(String[] args) {
        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("PERSISTENCE");
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        EntityTransaction entityTransaction = entityManager.getTransaction();
        Student student = new Student("Ramesh", "Fadatare", "rameshfadatare@javaguides.com");

Read complete example at JPA 2 with Hibernate 5 Bootstrapping Example. In this post, we will show you how to create or configure a simple JPA application with Hibernate.

For more, check out this JPA example.

code, java, jpa, jpa architecture

Published at DZone with permission of Ramesh Fadatare . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}