DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Architecture and Code Design, Pt. 2: Polyglot Persistence Insights To Use Today and in the Upcoming Years
  • MongoDB to Couchbase for Developers, Part 1: Architecture
  • Why Do You Need to Move From CRUD to Event Sourcing Architecture?
  • Data Fabric: What Is It and Why Do You Need It?

Trending

  • Optimize Deployment Pipelines for Speed, Security and Seamless Automation
  • Rethinking Recruitment: A Journey Through Hiring Practices
  • AI’s Role in Everyday Development
  • Performance Optimization Techniques for Snowflake on AWS
  1. DZone
  2. Data Engineering
  3. Databases
  4. Introduction to JPA Architecture

Introduction to JPA Architecture

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

By 
Ramesh Fadatare user avatar
Ramesh Fadatare
·
Updated Aug. 09, 22 · Presentation
Likes (35)
Comment
Save
Tweet
Share
37.9K Views

Join the DZone community and get the full member experience.

Join For Free

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 entity as a relational entity. 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. Class-level architecture 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 and 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 a 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 in a database as a record. They help programmers by reducing their efforts to write codes for storing data in 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.persistence package. The following diagram shows the relationship between them. Relationship between the classes and interfaces belong to the javax.persistence package

  • The relationship between EntityManagerFactory and EntityManager is one-to-many. It is a factory class to EntityManager instances.
  • The relationship between EntityManager and EntityTransaction is one-to-one. For each EntityManager operation, there is an EntityTransaction instance.
  • The relationship between EntityManager and Query is one-to-many. Many numbers of queries can execute using one EntityManager instance.
  • The relationship between EntityManager and Entity is one-to-many. One EntityManager instance can manage multiple Entities.

JPA Simple Example

Let's demonstrate 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;

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

    @Id
    @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;
    }

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


Usage of EntityManager, EntityManagerFactory, and EntityTransaction

Note that from the above class-level architecture, we are using EntityManager, EntityManagerFactory, and EntityTransaction interfaces in this snippet. In this example, we use the persist() method to store student objects in 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();
        entityTransaction.begin();
        Student student = new Student("Ramesh", "Fadatare", "rameshfadatare@javaguides.com");
        entityManager.persist(student);
        entityManager.getTransaction().commit();
        entityManager.close();
        entityManagerFactory.close();
    }
}


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.
Database Relational database Architecture

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

Opinions expressed by DZone contributors are their own.

Related

  • Architecture and Code Design, Pt. 2: Polyglot Persistence Insights To Use Today and in the Upcoming Years
  • MongoDB to Couchbase for Developers, Part 1: Architecture
  • Why Do You Need to Move From CRUD to Event Sourcing Architecture?
  • Data Fabric: What Is It and Why Do You Need It?

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!