Hello everyone. In this article, we will see the differences between JPA, Hibernate, and Spring Data JPA.
ORM Stands for Object-Relational Mapping that maps the data in the database to the Java Class which is called an Entity. Not only that, but ORM also preserves the relationship between the tables at the Entity level.
There are many ORM frameworks in the market and the famous ones for Java are
What is Hibernate?
As we all know, Hibernate is an ORM framework that sits between the application and the database. Before Hibernate, developers used to write queries using JDBC and retrieve the data and manually set it to the DTO Objects and send it to the Front End. This was time-consuming and painful.
So Hibernate is a framework in the ORM layer that maps the relational data to the Java Objects. It also provides an abstraction to the developers so that they don't need to worry about the data source. It also provides configuration options to configure the data store and developers can also write queries with Hibernate
Features of Hibernate
1. Light Weight
2. Open Source
3. ORM (Object Relation Mapping)
4. High Performance
5. HQL (Hibernate Query Language)
9. Lazy Loading
10. Database Independent
Lets us see only a few important features below
Hibernate Query Language (HQL)
SQL is low-level programming where developers have to query for the database columns in a database table. But HQL is simplified for developers in such a way that the Java class names and attributes are used in the query. Internally, hibernate converts HQL into SQL and executes it in the database.
Hibernate also supports native SQL queries along with the HQL but it is recommended to use HQL as it is independent of the underlying database. Whereas if we write SQL, the syntax differs from database to database
The Query interface provides object-oriented methods and capabilities for representing and manipulating HQL queries.
Example of an HQL:
String queryStr = "SELECT e.name FROM Employee e"; Query query = session.createQuery(queryStr); List results = query.list();
The attribute name is selected from the Entity Employee
Lazy Loading in Hibernate
Hibernate supports the following loading patterns
- Eager Loading is a design pattern in which data initialization occurs on the spot.
- Lazy Loading is a design pattern that we use to defer the initialization of an object as long as it’s possible and load only on demand
Lazy Loading is the default one and it makes the application efficient by not loading all the data and exhausting the DB Connection pool.
Eg: If a table has 1 million records and the relationship tables have another 1 million records. In case of lazy loading, it will only load the main table and only if requested, it will load data from the child tables.
Caching in Hibernate
Caching is the process of storing data into cache memory and improves the speed of data access.
Hibernate supports two levels of caching, first-level and second-level caching.
First Level Cache
The first level cache is a session-level cache and it is always associated with session-level object
Second Level Cache
Second-level cache is the session factory level cache and it is available across all sessions. For a second-level cache, we have to enable the cache and provide a cache provider like Ehcache and add its dependency.
What is JPA?
JPA stands for Java Persistence API and it is the Java specification that defines how to persist java objects. It is considered as a link between an object-oriented model and a relational database system. Hibernate is the standard implementation of JPA. JPA cannot be used alone and it always needs an implementation like Hibernate, EclipseLink, iBatis, etc.
For data persistence, the java. persistence package contains the JPA classes and interfaces.
- JPA provides JPQL (Java Persistence Query Language) and HQL provided by Hibernate is a superset of it. Either can be used in the application.
2. JPA provides EntityManagerFactory interface whereas Hibernate provides SessionFactory to create the Session instances
3. For CRUD operations on instances of mapped entity classes, JPA uses EntityManager whereas Hibernate uses the Session interface
So, as seen above JPA provides its in-built stuff so that things won't break if we change to other ORM frameworks later and it will remain consistent.
But Hibernate provides advanced features and if you are sure that you will not change the ORM framework, then it's better to stick to the Hibernate specs.
What is Spring Data JPA?
There is always confusion between JPA and Spring Data JPA.
As we saw above, JPA is a standard for defining the persistence layer and Spring Data JPA is a sub-project under the Spring Framework umbrella which allows Spring applications to integrate with JPA.
Spring Data JPA is an abstraction that makes it easier to work with a JPA provider like Hibernate which is used by default. Specifically, Spring Data JPA provides a set of interfaces for easily creating data access repositories.
Before Spring Data JPA, we used to write all the CRUD methods in every single DAO and write an implementation for those. But then came Spring Data JPA, which abstracts the developer from that, and behind the scenes, it provides implementations for the basic crud methods. This avoids a lot of boilerplate code and makes it efficient for developers. We can still add custom methods and can use HQL or criteria etc.
Spring Data JPA also allows developers to use Transactional annotation to control the transaction boundaries.
Spring Data JPA comes with a concept called JPA Repository and Query methods. JPA Repository is nothing but a set of interfaces that defines query methods like findByFirstName or findByLastName etc. These methods are converted into low-level SQL queries by Spring.
Because of this cleaner approach, many Spring-based applications are using Spring Data JPA to implement their Data Access Layer or DAO Layer
The Spring Data Jpa dependency can be added as below and this will do the data source auto-configuration as well. After adding this, we just need to add a database dependency to make sure it is available in the class path.
in this article, we saw what is Hibernate, JPA, and Spring Data JPA. In a nutshell, JPA is a specification and we have to use one of its implementations to connect to the database. Spring Data JPA cannot work without a JPA provider and a JPA provider is mandatory to connect with the database. Spring Data JPA is not a mandatory one but it is for adding convenience to developers by removing the boilerplate code
Hope you all have enjoyed this article. Happy learning!!!
Please read my article on Transaction Management if you want to know more
'WEB > Spring JPA' 카테고리의 다른 글
|Hibernate vs JPA vs Spring Data JPA (0)||2022.06.07|
|JPA의 DTO와 Entity (0)||2022.01.10|
|Spring Data JPA findBy Multiple Columns with Example (0)||2022.01.09|