What is Java Persistence API and its Importance?
To persistently store large amounts of data into the database, we use a concept called Java Persistence API. It is a collection of methods, objects, and classes, provided by the Oracle Corporation. It is specially made for Java-based applications and also provides services such as JMS, JAAS, MDBs, etc. They provide a transactional and secured way of storing and retrieving of data.
Nowadays, every business or organization needs enterprise applications to store vast amounts of data. But many tools doesn’t support it well; database access is inefficient and complex then. This is when JPA came into the market. Basically, Java developers use proprietary framework for database interaction with Java objects, or they build lots of code, both of them are complex and takes lots of time. JPA forms a bridge between object models and relational models, such as the relation between Java program and database program. It also reduces the risk of interaction.
Where do we need to use JPA?
A developer uses ‘JPA Provider’ framework for relational object management to reduce the risk of writing codes. It allows easy interaction with database instance. JPA has got drastic changes in the past few years.
JPA History:
Using javax.ejb.EntityBean interface, the business logic layer is combined with defined persistence layer in earlier versions of EJB.
This persistence layer is separated from the logic and later named as Java Persistence API during the introduction of EJB 3.0, and it was termed as JPA 1.0. Along with the JAVA EE5 specifications, on May 11, 2006, specifications of this JPA were released, using JSR 220.
- As a part of Java Community Process JSR 317, on December 10, 2009, JPA 2.0 was released combined with the specifications of JAVA EE6
- It has evolved in years and on April 22, 2013, along with the specification of JAVA EE7, JPA 2.1 was released using JSR 338.
JPA Providers:
Many enterprise vendors such as Eclipse, Red Hat, and Oracle, provide a new product based on the essence of JPA since it is an open source API. Toplink, Hibernate, Spring Data, JPA, EclipseLink, etc. are some of these products.
Before persisting data into the database, it shows how to define a Plain Oriented Java Object (POJO) as an entity and management of entities with relations.
Class Level Architecture
The image below shows JPA architecture:
Entity- These are the persistence objects, which are stored as records in the database.
EntityManager- It is an interface that takes care of lifecycle events of entities. It manages the persistence operations on objects. EntityManager is similar to the factory for instance of Query.
Persistence- This class is used to obtain the instance of EntityManagerFactory by a set of static methods.
EntityManagerFactory- This is a factory consists of a number of EntityManagers. Multiple instances of EntityManager are created and managed by it.
Query- To obtain and retrieve the relational objects of a given constraints and criteria, this interface is used and implemented by each JPA vendor.
EntityTransaction- It manages the transactions of the entities.