Spring Boot Is Easy: Hibernate and JPA

Rohit Satwadhar
4 min readSep 28, 2023
Hibernate

Hibernate is an Object-Relational Mapping (ORM) framework. It is used to simplify database access and data persistence of Java applications. It does this by allowing developers to interact with relational databases using object-oriented programming paradigms. In simple words hibernate can map Java objects to tables in Relational database and vice versa.

JPA (Java Persistence API) is a Java-based specification for ORM. JPA defines a standard set of interfaces and annotations to interact with relational databases. Hibernate is the most popular implementation of this specification.

In the following article, we will go over the various key features and concepts of the Hibernate ORM.

Data Access Object (DAO)

DAOs are used to abstract interaction between Java applications and relational databases. This helps make code more modular and maintainable.

We start off by creating an Entity class. This entity class represents a table in our relational database. Each variable in this class corresponds to a column of a table. JPA provides multiple annotations to specify how they map to the table column. Entity classes are annotated with @ Entity annotation.

Student Entity

For each entity, we create a DAO interface. This DAO interface declares the methods to perform common database operations on this table.

DAO interfaces need concrete implementation for abstract methods. We do that by creating a separate class and in that class, we use EntityManager to interact with databases. These implementations are annotated with @ Repository.

EntityManager provides some standard methods like save(), find() etc.

EntityManager needs DataSource with connection info. Spring boot automatically creates EntityManager and Datasource based on the properties set in the application.properties file.

@Transactional: Transactions are an important part of a relational database. Transactions ensure data consistency, integrity, and reliability, even in the presence of failures or errors. This annotation automatically starts and ends transactions in our JPA code. So you won’t have to do it manually. This annotation is required for create, update and delete operations.

We now have DAO implementation. We can now use this in our application to interact with the database. In this article, instead of creating rest endpoints, we will create a command line spring boot application. To create command line applications we use CommandLineRunner.

Here we autowire our DAO class and use the save() method to save a student.

CRUD Operations:

  1. Create Student: EntityManger.persist(). In above example, in DAO we have declared a save(…) method. And for its implementation we have used EntityManger.persist().
  2. Read Student: EntityManger.find(). In above example, in DAO we have declared find(…) method. And for its implementation we have used EntityManger.find(…).
  3. Update Student:
Update Student

Make sure the student already exists and then call this function to update the values.

4. Delete Student:

Delete Student

JPA Query Language (JPQL)

EntityManger provides some standard methods. Sometimes we might need certain complex queries. We can create queries using EntityManger itself.

JPQL is slightly different from SQL. In SQL we use column names in queries. In JPQL we will use field names from the Entity class.

we added a new method in the DAO interface and implemented this in DAOImpl class. First, we created a query using entityManager. Observe the name of table Student and of the field firstName.These names are same as that in our java class.

Parameterized JPQL Query

Parameterized JPQL query

Here observe, that in the query we have not put the final field. We put a placeholder :name. We use this placeholder to put the final values that we want. Using setParameter(…) we have put the value we want.

Conclusion:

In conclusion, Hibernate is an implementation of JPA. Hibernate helps abstract the nuisances of interacting with a relation database. With the help of Entity and EntityManger objects, we can easily interact with the database.

Here we have created our own DAO. Hibernate provides JpaRepository. Using this we don’t have to create our own DAO. The methodsprovided cover most of the common database operations. I encourage you to explore that next.

And Remember Spring Boot Is Easy.

--

--

Rohit Satwadhar

I Write about new things that I learn. That is how I remember stuff. These things are mostly tech related.