Spring Boot Jpa 简明教程

Spring Boot JPA - Architecture

Java 持久化 API 是一个将业务实体存储为关系实体的源。它展示了如何将一个普通旧 Java 对象 (POJO) 定义为实体,以及如何通过关系管理实体。

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

下图展示了 JPA 的类级架构。它展示了 JPA 的核心类和接口。

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

jpa class level architecture

下表描述了上述架构中显示的每个单元。

The following table describes each of the units shown in the above architecture.

Sr.No

Units & Description

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 factory for Query instance.

3

Entity Entities are the persistence objects, stores as records in the database.

4

EntityTransaction It has one-to-one relationship with EntityManager. For each EntityManager, operations are maintained by EntityTransaction class.

5

Persistence This class contain static methods to obtain 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

在上述架构中,类和接口之间的关系属于 javax.persistence 包。下图显示了它们之间的关系。

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.

jpa class relationships
  1. The relationship between EntityManagerFactory and EntityManager is one-to-many. It is a factory class to EntityManager instances.

  2. The relationship between EntityManager and EntityTransaction is one-to-one. For each EntityManager operation, there is an EntityTransaction instance.

  3. The relationship between EntityManager and Query is one-to-many. Many number of queries can execute using one EntityManager instance.

  4. The relationship between EntityManager and Entity is one-to-many. One EntityManager instance can manage multiple Entities.