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 details? 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

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 for preparing.

Overview about Spring Data JPA

You can refer to the tutorial about JPA and Spring framework at 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 at 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, which 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 which Spring Data JPA is in use. That’s the CrudRepository interface.

CrudRepository interface with the meaning of creating, reading, updating, 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 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, here 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: create, read, update, delete, paging, and sort. 😀

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 HelloRepository interface, SimpleJpaRepository, as you see in the image above to initialize this object in the Spring container.

Since the object SimpleJpaRepository using transaction, you need to declare more transactionManager bean into 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?

 

Chia sẽ bài viết này ...Share on Facebook
Facebook
0Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin

Add Comment