VOOZH about

URL: https://dzone.com/articles/introduction-to-jpa-architecture

⇱ Introduction to JPA Architecture


Related

  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 Updated Aug. 09, 22 · Presentation
Likes
Comment
Save
38.6K 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.

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 EntityManagerFactoryinstance.
  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.

  • 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 EntityTransactioninstance.
  • The relationship between EntityManager and Queryis one-to-many. Many numbers of queries can execute using one EntityManager instance.
  • The relationship between EntityManager and Entityis 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  @Columnannotations 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", "[email protected]");
 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

  • Zero-Latency Architecture: Database Triggers + Serverless Functions for Modern Reactive Architectures
  • 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?

Partner Resources

×

Comments

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

Let's be friends: