JPA (Java Persistence API) is a Java specification for managing relational data in Java applications. It allows to access and persist data between Java object/class and relational database. JPA follows Object-Relational Mapping (ORM). It is a set of interfaces. It also provides a runtime EntityManager API for processing queries and transactions on the objects against the database. It uses a platform-independent object-oriented query language JPQL (Java Persistent Query Language).
In the context of persistence, it covers three areas :
- The Java Persistence API
- Object-relational metadata
- The API itself, defined in the persistence package
JPA is not a framework. It defines a concept that can be implemented by any framework.
Why JPA should be used ?
JPA is simpler, cleaner, and less labor-intensive than JDBC, SQL and hand-written mapping. JPA is suitable for non-performance oriented complex applications. The main advantage of JPA over JDBC is that in JPA data is represented by objects and classes while in JDBC data is represented by tables and records. It uses POJO to represent persistent data that simplifies databse programming. There are some other advantages of JPA :
- JPA avoids writing DDL in a database-specific dialect of SQL. Instead of this, it allows mapping in XML or using Java annotations
- JPA allows to avoid writing data manipulation language (DML) in the database-specific dialect of SQL
- JPA allows to save and load Java objects and graphs without any DML language at all
- When there is a need to perform queries JPQL, it allows to express queries in terms of Java entities rather than native SQL table and columns
There are following features of JPA :
- It is a powerful repository and custom object-mapping abstraction
- It dynamically generates queries from queries methods name
- Domain base classes provide basic properties
- Possibility to integrate custom repository code
- It is easy to integrate with Spring Framework with the custom namespace
JPA is a source to store business entities as relational entities. It shows how to define POJO as an entity and how to manage entities with relation.
The following figure describes the class-level architecture of JPA that describes the core classes and interfaces of JPA that is defined in the javax.persistence package. The JPA architecture contains the following units :
- Persistence : it is a class that contains static methods to obtain an EntityManagerFactory instance
- EntityManagerFactory : it is a factory class of EntityManager. It creates and manages multiple instances of EntityManager
- EntityManager : it is an interface. It controls the persistence operations on objects. It works for the Query instance
- Entity : the entities are the persistence objects stored as a record in the database
- Persistence Unit : it defines a set of all entity classes. In an application, EntityManager instances manage it. The set of entity classes represents the data contained witnin a single data store
- EntityTransaction : it has a one-to-one relationship with the EntityManager class. For each EntityManager, operations are maintained by EntityTransaction class
- Query : it is an interface that is implemented by each JPA vendor to obtain relation objects that meet the criteria