Overview about Spring Data JPA

In the previous tutorial, I introduced to you all an overview of JPA Query Language to manipulate database types using entities. Whenever you need information from the database, you will use the SELECT or UPDATE, or DELETE statements and the entity name to build into the query, then retrieve the Query object from the Entity Manager using this query, and finally, call the appropriate method according to your needs from this query object to get the result you want. Many steps need to do, don’t you? If you use JPA with Spring framework in your project then just try to use Spring Data JPA! It will help you minimize the work to be done in some cases. How is it in detail? In this tutorial, let’s learn about Spring Data JPA.

First, I will create a new Maven project as an example:

Overview about Spring Data JPA

We will use Java 17 for this example:

Spring Data JPA dependency:

Here, I will use JPA with the implementation of Hibernate so I will add Hibernate dependency as follows:

Project Lombok:

In this tutorial, I will use the MySQL database:

For simplicity, I just defined a table containing student information with 2 columns as follows:

The entity of this table:

To work with JPA, we need a persistence.xml configuration file for it. Let’s create a new file called persistence.xml located in /src/main/resources/META-INF.

The contents of this file are as follows:

Declaring EntityManagerFactory in Spring container:

with datasource:

OK, so we have just finished preparing.

Overview about Spring Data JPA

You can refer to the tutorial about JPA and Spring framework here.

Now we go to the main topic of this tutorial.

First of all, you need to know that Spring Data JPA is a small module in a large project called Spring Data project. The purpose of the Spring Data project is to minimize the repetitive code involved in accessing and manipulating data management systems when developing applications that use the Spring framework. In addition to Spring Data JPA support JPA for minimizing code to access and manipulate database management systems, we also have Spring Data JDBC (like Spring Data JPA), Spring Data LDAP (support Spring LDAP), Spring Data MongoDB (support for MongoDB), etc. You can find all Spring Data modules here.

In order to achieve the purpose of minimizing code, Spring Data defines the main interface called Repository, which is part of the Spring Data Common module, and will be used for all the other modules in the Spring Data project. The content of this interface is as simple as:

This interface uses two generic types:

  • T is the domain class that the repository will manage.
  • ID is the data type of the ID of the domain class that the repository manages.

Because this interface is so simple, it must have other interfaces that extend it to represent what the Spring Data project does for you. 😀

Here, we will have more interfaces that extend from the Repository interface depending on the module we use, but since we are discussing the Spring Data JPA then I will only list here an interface that extends the Repository interface in which Spring Data JPA is in use. That’s the CrudRepository interface.

CrudRepository interface with the meaning of creating, reading, updating, and deleting allows us to perform basic operations on data systems, data systems here you have to understand in broad meaning that it is not just a database.

To support paging and sorting for Spring Data JPA, we have to mention another interface called PagingAndSortingRepository.

All of the above-mentioned interfaces are in the Spring Data Common module.

In Spring Data JPA, we have only one interface, the JpaRepository interface that inherits the PagingAndSortingRepository interface. With extending from the PagingAndSortingRepository interface, you can also imagine that the Spring Data JPA can help us minimize the code for database-related operations, right? These include: creating, reading, updating, deleting, paging, and sorting. 😀

You can clearly see the extended structure for these interfaces in the following image:

Overview about Spring Data JPA

OK, so let’s take an example with the JpaRepository interface to see how it works.

Now we will create an interface called HelloRepository extend from JpaRepository with the following content:

The domain class here is the Student entity and Long is the data type of the properties in the Student mapping entity with the primary key column in our student table.

Now we will declare this interface in the Spring container as follows:

Here, I use the java namespace jpa:repositories with the base package com.huongdanjava.springdatajpa to scan the HelloRepository interface. With this declaration, the Spring Data JPA automatically retrieves the object that implements the HelloRepository interface, SimpleJpaRepository, as you see in the image above to initialize this object in the Spring container.

One point that you guys should note is that we need to declare the <context:annotation-config/> tag as I did above so that Spring can configure the object of the EntityPathResolver class when initializing the bean for the JpaRepositoryFactoryBean class! Otherwise, you will get the error “EntityPathResolver must not be null”. You can see the setEntityPathResolver() method with the @Autowired annotation in the JpaRepositoryFactoryBean class to better understand this.

Since the object SimpleJpaRepository uses transaction, you need to declare more transactionManager bean into the Spring container.

OK, here we have completed the necessary configurations. Now, let’s try it out.

Suppose in the database you have the following data:

Overview about Spring Data JPA

then when running the following example:

The result will be:

Overview about Spring Data JPA

Suppose, we now add a method in the HelloRepository interface with the following contents:

then when running the following example:

the result will be:

Overview about Spring Data JPA

As you can see, although no class implements the HelloRepository interface, we still query the student table by column name. Reduce a lot of code that we need to do, don’t you?

5/5 - (1 vote)

Add Comment