Spring Boot – Spring Data JPA

In this Chapter, we would learn about the Spring Data JPA.

 

  1. Overview of Spring Data JPA
  2. What we would do
  3. Add Dependencies
  4. Create the Student Class
  5. Add the Controller Class
  6. Add Business Service Class

 

1. Overview of Spring Data JPA

JPA stands for Java Persistence API. You use JPA to add data access to your application. Remember that in the previous tutorial, we hardcoded some list of items in our application. Therefore, these items are not persistent. With Spring Data JPA, you can actually save your data in a database. Spring Data JPA also provides use with out-of-the-box classes and methods for managing data. For instance, we have methods for getting list of items, updating and deleting items.

 

 

2. What we would do

In this demo, we are going to create a database of a single table, Students. We would use Apache Derby in-memory database. This means that this database would live in our classpath. We would see how to connect to external database server in a later tutorial.

Then we use Spring Data JPA to select, insert, update and delete records from this table. Actually, this table will be created automatically for us by Spring Data JPA!. All we need to to is to add then necessary dependencies and define our classes. Then we give a few annotations. Let’s see!

 

 

3. Add the Dependencies

The first thing we would do is to add the necessary dependencies. There are two dependencies we would add:

  • the Spring Data JPA dependency
  • the Apache Derby in-memory database dependency.

 

Open the pom.xml file and add the following dependencies

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

 

This next dependency if the the Apache Derby in-memory database

<dependency>
    <groupId>org.apache.derby</groupId>
    <artifactId>derby</artifactId>
    <scope>runtime</scope>
</dependency>

 

 

4. Create the Student Class

We would add a class that holds student records. To keep it simple, the class would contain only three fields

  • Id (String)
  • Name (String)
  • Department (String)

Also remember to place this class in a separate package. After creating this class, the content would be:

 

package com.kindsonthegenius.social.student;

public class Student {
	
	private String id;
	private String name;
	private String department;
	
	//add contructors, getter and setters here
}

 

Then generate the Constructor. (Source > Generate constructor using fields)

Finally generate the getters and setters (Source > Generate getters and setters)

 

5. Add the Controller Class

Inside the same package with the Student class, add a new class. Name it StudentController.(you already know about controllers by now).

Annotate the controller class with the @RestController annotation.  The StudenController class would be as shown below:

 

package com.kindsonthegenius.social.student;

import org.springframework.web.bind.annotation.RestController;

@RestController
public class StudentController {

	//our methods goes here
	
}

Listing 1.0: The StudentController Class

 

 

6. Add the Business Service Class

Inside the same package with the Student class, add a new class. Name it StudentService.(you already know about business services by now).

Annotate the controller class with the @Service annotation.  The StudenService class would be as shown below:

 

package com.kindsonthegenius.social.student;

import org.springframework.stereotype.Service;

@Service
public class StudentService {
	
	//our methods goes here
	
}

Listing 1.1: The StudentService Class

 

If you have done everything correctly, then the content of the student package would be as shown below:

 

At this point, you have completed the first part. Next we would add the Spring jpaRepositiory