Overview about Spring Data JPA

In the previous tutorial, I introduced to you all an overview of Jakarta Persistence 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 Jakarta Persistence with the 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 25 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:

Sample data:

The entity of this table:

I will declare the EntityManagerFactory in the Spring container using the spring.xml:

Overview about Spring Data JPA

as follows:

with datasource:

OK, so we have just finished preparing. You can refer to the tutorial about Jakarta Persistence and the 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 the 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 (supports 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 2 interfaces that extend the Repository interface in which Spring Data JPA is in use. That’s the CrudRepository interface and PagingAndSortingRepository.

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). The PagingAndSortingRepository interface supports pagination and sorting for Spring Data JPA. Both the CrudRepository and PagingAndSortingRepository interfaces have extensions to support List instead of Iterable: ListCrudRepository and ListPagingAndSortingRepository. All the interfaces mentioned above are in the Spring Data Common module.

The Spring Data JPA module defines an interface called JpaRepository that inherits from the ListCrudRepository, ListPagingAndSortingRepository, and QueryByExampleExecutor interfaces.

QueryByExampleExecutor is another interface from the Spring Data Common module. It provides additional functionality, allowing us to search for entities based on their non-null properties. I will discuss QueryByExampleExecutor in more detail in later articles!

By extending the ListCrudRepository and ListPagingAndSortingRepository interfaces, you can imagine how Spring Data JPA can help us minimize the code for database-related operations, right? These include: create, read, update, delete, paging, and sorting 😀

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

Many query methods are supported by default by interfaces such as CrudRepository, PagingAndSortingRepository, ListCrudRepository, ListPagingAndSortingRepository, QueryByExampleExecutor, and JpaRepository. You just need to use them to interact with the database. Spring Data JPA will handle the tasks related to building and executing the query to retrieve the results.

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

Now we will create an interface called StudentRepository 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 students table.

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

Here, I’m using the <jpa:repositories> tag with the base package com.huongdanjava.springdatajpa to scan the StudentRepository interface. With the above declaration, Spring Data JPA will automatically create a Repository Proxy object to retrieve the SimpleJpaRepository object that implements the StudentRepository interface, as you can see in the image above, to initialize it in the Spring container.

Since the object SimpleJpaRepository uses transactions, you need to declare the transactionManager bean in the Spring container.

Okay, we’ve completed the necessary configuration. Now let’s run an example. The main class to run our application is as follows:

The result will be:

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

then when running the following example:

the result will be:

As you can see, even though there isn’t any class implementing the StudentRepository interface, we can still query the students table by column name. This significantly reduces the amount of code we need to write, doesn’t it?

5/5 - (1 vote)

Add Comment