Spring JPA Beginner Tutorial

In this tutorial I will explain how to build a basic Spring JPA project using Spring Initilizr.

Core Concept

The goal of spring data repository is to significantly reduce the amount of boiler plate code required to implement the persistence/dao layer in any Spring application.

The primary interface in Spring data repository abstraction is Repository.It is a marker interface ,it takes the domain class as well as the id of the domain class as type arguments.The CRUDRepository which extends the Repository interface provided more sophisticated CRUD functionality for the entity/domain class. On top of CRUDRepository there is PagingAndSortingRepository abstraction that provides additional methods to ease pagination access to entities.

Now ,lets create a Spring JPA project using Spring Initialzr.

While creating a project through Spring Initialzr select the following options and click on generate button:-

You need to select Spring Data JPA,Spring Web and H2 Database which is an in memory database.

When you click on generate a a zip file will be saved in your machine.Unzip the folder and the maven project can be imported in IntelliJ or any other IDE.Click on Import Project ,navigate to the location where you project is saved (where the primary pom file is located).

Click on open and the project will be imported in Intellij.

Here’s the project:-

We will create a set of REST API’s for creating, updating and deleting employees.

We will first create a domain object named Employee.We will annotate with @Entity annotation. To declare the primary we annotate the variable with @Id annotation.

The Employee object has a set of variables along with the getter/setter methods.

Next we will create the Repository interface to perform operations on this Employee object.

Our custom interface extends the JpaRepository. The Jpa repository contains methods to perform CRUD operations.We need to pass the domain object and type of the primary variable in the domain object.

Next we create a service interface and the service implementation.

Next we provide the implementation for EmployeeService .We annotate the EmployeeServiceImpl with @Service annotation. In the employee service implementation we refer the EmployeeRepository using the @Autowired annotation.

In the above EmployeeServiceImpl we call the CRUD method in to perform save,update,delete and search operations on Employee entity.

Next we create the Employee controller to create the Rest API’s .We use the @RestController and the @RequestMapping annotation to specify the API URL.

We created different API’s to fetch all the employees ,save an employee, update or delete an employee or find an employee based on primary key.

Let’s test the REST api’s we just created. To start the Spring Boot Application we run the DemoApplication.

Spring application started:-

Lets add some employees using the Rest API.

In a similar way I created 4 more employees.

Lets update the first employee.

Now we fetch all the employees just created using the rest api.

Lets fetch the employee based on the employee id.

Lets delete an employee based on it.

Now, lets fetch all the employees again.As you can see the employee with id 1EMP is deleted.

So that was a basic Spring JPA tutorial .Happy coding:-)

Leave a comment

Your email address will not be published. Required fields are marked *