☰ See All Chapters |
JPA Architecture
The below diagram shows the JPA architecture with primary components, core classes and interfaces. persistence.xml is the configuration file, EntityManagerFactory and EntityManager are the important blocks of architecture. Configuration file i.e. persistence.xml has the persistence unit configured. This persistence unit will be read by EntityManager. The persistence unit of configuration file defines the database connection details. Provider element of persistence unit defines the implementation used for JPA. Below the architecture diagram we have provided a sample persistence.xml, we have used hibernate implementation of JPA, hence provider element has or.hibernate.ejb.HibernatePersistence.
Sample persistence.xml
Class Level Architecture
The below class level architecture image shows the importance core classes and interfaces of JPA.
EntityManagerFactory
EntityManagerFactory class is a factory for EntityManagers. EntityManagerFactory object represents the JDBC connection pool. EntityManagerFactory is created by reading persistence.xml and there will be only one object of EntityManagerFactory which will be immutable and also thread safe.
EntityManager
As the name tells, EntityManager manages the entities. EntityManager manages the complete transactions of entities. EntityManager is used to perform CRUD (Create, Read, Update, Delete) operations on entities. EntityManager provides sophisticated methods to perform CRUD operations. Read more about EntityManager from our chapter JPA EntityManager.
JPA Persistence Unit
JPA Persistence Unit defined inside persistence.xml defines a set of all entity classes that are managed by EntityManager instances in an application. This set of entity classes represents the data contained within a single data store. The JAR file or directory whose META-INF directory contains persistence.xml is called the root of the persistence unit. The scope of the persistence unit is determined by the persistence unit’s root. Each persistence unit must be identified with a name that is unique to the persistence unit’s scope. Persistent units can be packaged as part of a WAR or EJB JAR file, or can be packaged as a JAR file that can then be included in a WAR or EAR file. Read more about Persistence Unit from our chapter JPA Persistence Unit.
JPA Entity classes/ Persistent Classes
There should be a java class representing a database table. Such classes are called as Entity classes/ Persistent Classes. Persistence class or an entity class is a POJO (Plain Old Java Object) class, i.e. an ordinary Java class that is annotated with @Entity annotation. Each persistent instance of an entity class - each entity - represents a unique database record. Each variable/field may represent each column of table. Read more about Persistence classes from our chapter JPA Entity classes/ Persistent Classes.
To understand the architecture flow more clearly we suggest reading JPA example. Refer the example from our chapter JPA Simple Example.
JPA Query
The javax.persistence.Query interface is used to issue queries in JPA. Java Persistence Query Language (JPQL) is the query language used in JPA. The syntax of JPQL is almost same as SQL, but it is object-oriented rather than table oriented. The EntityManager.createQuery method is used to create a Query instance from a given JPQL string. getResultList() method of Query object is used to execute the query. This method returns a List containing the matching entity class objects. Read more about Query from our chapter JPA Query.
JPA Classes Relationship with EntityManager class
Below image illustrates the relationship of different classes with EntityManager class.
EntityManager with EntityManagerFactory: Many EntityManager can be created from EntityManagerFactory, hence it is many to one relationship.
EntityManager with EntityTransaction: One EntityManager can manage one EntityTransaction, hence it is one to one relationship.
EntityManager with query: One EntityManager can execute many Query objects, hence it is one to many relationship.
EntityManager with Entity: One EntityManager can use many Entity classes, hence it is one to many relationship.
JPA Exceptions Architecture
The below diagram depicts the JPA exception architecture. All exceptions are unchecked. JPA uses standard exceptions where appropriate, most notably IllegalArgumentExceptions and IllegalStateExceptions.
All Chapters