JPA is a Java specification(Jakarta Persistence API) and it manages relational data in Java applications. To access and persist data between Java object(Plain Old Java object)/ class and relational database, we can use JPA. Upon Object-Relation Mapping (ORM), it follows the mechanisms. It has the runtime EntityManager API and it is responsible for processing queries and transactions on the Java objects against the database. The main highlight is it uses JPQL (Java Persistent Query Language) which is platform-independent. JPA mainly covers persistence in terms of
The Java Persistence API
Moreover under the persistence package API is defined.
We cannot say that JPA is a framework, but It defines a concept and it can be implemented by any framework.
Advantages of Using JPA
No need to write DDL/DML queries, instead we can map by using XML/annotations.
JPQL is used and since it is platform-independent, we no need to depend on any native SQL table. Complex expressions and filtering expressions are all handled via JPQL only.
Entity can be partially stored in one database like MySQL and the rest can be in Graph database Management System.
Dynamic generation of queries is possible.
Integration with Spring framework is easier with a custom namespace.
Units comprised in JPA are available under javax persistence package:
It has static methods to obtain an EntityManagerFactory instance
Factory class for EntityManager and responsible for managing multiple instances of EntityManager
It is an interface that works for the Query instance
They are persistent objects and stored as records in the database
Set of all entity classes
It has one-to-one relationship with EntityManager.
To get relation objects that meet the criteria.
Many have a confusion between JPA and HIbernate. Let us see few key differences
JPA : It is a Java specification for mapping relational data in Java application. It is not a framework
Hibernate is an ORM framework and in that way data persistence is possible.
In JPA, no implementation classes are provided.
In Hibernate, implementation classes are provided.
Main advantage is It uses JPQL (Java Persistence Query Language) and it is platform-independent query language.
Here it is using HQL (Hibernate Query Language).
It is available under javax.persistence package.
It is available under org.hibernate package.
In Hibernate, EclipseLink, etc. we can see its implementation.
Hibernate is the provider of JPA.
Persistence of data is handled by EntityManager.
Persistence of data is handled by Session.
Let us see a sample application for a spring boot application with JPA.
Right-click on the file and run the file as a Java application, we can see the output in the console.
The server started at port 8080
Initially as there are no records, when we hit http://localhost:8080, we wont be seeing any data. Let’s add the data by adding via the Postman client. Postman client has to be installed for doing this operation. URL to add users: http://localhost:8080/add-geekuser(Remember that this URL matches the controller file requestmapping).
Now a user is added. Hence we can verify the same by using http://localhost:8080
If we check with the added user detail and display user detail, they are same. If we have added more than 1 record, those records also will be displayed. In the whole project, we have not seen anywhere about the SQL statement like Insert(for adding)/Select(for retrieving). We need to have a POJO class. We have to possess the code with the important annotations as mentioned in the code. Automatically JPA (Java Persistence API) will take care of it.