Differents of Hibernate ORM, Java Persistence API (JPA) and Spring Data JPA
In this article, we will see what is Hibernate ORM, Java Persistence API (JPA) and Spring Data JPA
. At the same time, how related to each other.
Hibernate ORM (or simply Hibernate) is an object–relational mapping tool for the Java programming language. It provides a framework for mapping an object-oriented domain model to a relational database. Hibernate handles object–relational impedance mismatch problems by replacing direct, persistent database accesses with high-level object handling functions.
Java Persistence API (JPA)
Java Persistence API in short JPA is the set of specifications that tells how to map and persist java objects in the relational database.
The Java Persistence API was first released as a subset of the EJB 3.0 specification (JSR 220) in Java EE 5. It has since evolved as its own spec, starting with the release of JPA 2.0 in Java EE 6 (JSR 317). At the time of writing this article, JPA 2.2 (JSR 338) is the latest version available.
JPA handles most of the complexity of JDBC-based database access and object-relational mappings. It defines the set of interfaces to allow mapping of java objects in relational DB in order to persist it and at the same time access the rows data and map it back to java objects.
The Java Persistence API (JPA) provides an object/relational mapping facility to Java developers for managing relational data in Java applications. Java Persistence consists of three areas:
The Java Persistence API
The query language
Object/relational mapping metadata
JPA is used for managing, persisting and accessing data between objects and relation database. Hibernate is an ORM (Object Relational Mapping) tool which implements JPA specification.
JPA is the Sun specification for persisting objects in the enterprise application. It is used as replacement for complex entity beans.
The implementation of JPA specification are provided by many JPA provider such as:
JPA is a source to store business entities as relational entities. It shows how to define a 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 stores 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 within 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.
Spring Data JPA
Spring Data JPA is not a JPA provider but is a specification. It is a library / framework that adds an extra layer of abstraction on the top of our JPA provider. It simply “hides” the Java Persistence API (and the JPA provider) behind its repository abstraction.
Features provided by Spring Data JPA :
Create and support repositories created with Spring and JPA
Support JPA queries
Support for batch loading, sorting, dynamical queries
Supports XML mapping for entities
Reduce code size for generic CRUD operations by using CrudRepository
What Components Do We Need?
The JDBC driver which enables java application to interact with the database.
The datasource provides all technical information needed to access data.
The JPA Provider implements the Java Persistence API. We use Hibernate because it is the most common JPA provider.
Spring Data JPA hides the used JPA provider behind its repository abstraction.
Spring Data JPA Details
Basic Spring Data JPA Flow
Once you completed reading above all articles then let’s understand basic flow at the time of accessing the database using Spring Data JPA is shown below.
Important Concepts in JPA
Spring Data JPA Hierarchy