Spring Boot – JPA Repositories for the Social API

In this chapter, we are going to add JPA repositories to our Social API.

In the previous chapter, you learnt about JPA repositories. You also learn how to add it to the application.

You can review them:Create JPA Repository and Crud Operations with JPS Repository.

 

We would cover the following:

  1. Modify the Classes
  2. Add the JPA Repositories
  3. Add Crud Operations for Location
  4. Add Crud Operations for Users
  5. Add Crud Operations for Posts
  6. Test the Application!

 

1. Modify the Classes

To be able to use repository you need to make some changes to the classes. Open each class and do the following

First, annotate the class with @Entity annotation

Second, annotate the id with @Id annotation (this sets it as primary key)

Third, add an empty constructor (constructor with no parameters)

If you miss something, then watch the video to see how to do it.

 

 

2. Add the JPA Repositories

So you need to add JPA repositories for Location, Users and Posts. Follow the same method as you did in the previous chapter for adding JPA repository for the Student class.

Just create  an interface in the same package and name it accordingly.

For example the LocationRepository is given below:

 

package com.kindsonthegenius.social.location;
import org.springframework.data.repository.CrudRepository;

public interface LocationRepository extends CrudRepository<Location, String> {
	
}

 

So repeat the same for the User and Post classes.

 

 

3. Add Crud Operations for LocationService

Now you need to open the LocationService class. Delete all the methods inside and replace with the code similar to that in StudentService. At the end, the content of LocationService would be as shown below:

 

@Service
public class StudentService {
	
	@Autowired
	public StudentRepository studentRepository;
	
	public List<Student> getAllStudents() {
		
		List<Student> students = new ArrayList<>();
		
		studentRepository.findAll()
		.forEach(students::add);
		
		return students;		
	}
		
	public void addStudent(Student student) {
		studentRepository.save(student);
	}
		
	public Optional<Student> getStudent(String id) {
		return studentRepository.findById(id);
	}
	
	public void deleteStudent(String id) {
		studentRepository.deleteById(id);
	}
	
	public void updateStudent(String id, Student student) {
	     studentRepository.save(student);
	}
}

 

 

4. Add Crud Operations for UserService

If you get it correctly, then the UserService would  be as shown below:

@Service
public class UserService {
	
	@Autowired
	private UserRepository userRepository;	
	
	public List<User> getAllUsers() {
		List<User> users = new ArrayList<>();
		
		userRepository.findAll()
		.forEach(users::add);
		
		return users;
	}
		
	public Optional<User> getUser(String id) {
		return userRepository.findById(id);
	}

	public void addUser(User user) {
		userRepository.save(user);
	}

	public void updateUser(String id, User user) {
		userRepository.save(user);
	}
	
	public void deleteUser(String id) {
	     userRepository.deleteById(id);	
	}
}

 

 

5. Add Crud Operations for PostService

For the PostService,  we would have the content as below:

 

@Service
public class PostService {
		
	private PostRepository postRepository;
	
	public List<Post> getAllPosts() {
	   List<Post> posts = new ArrayList<>();
	   
	   postRepository.findAll()
	   .forEach(posts::add);
	   
	   return posts;	 
	}
	   	   
	public Optional<Post> getPost(String id) {
		return postRepository.findById(id);
	}

	public void addPost(Post post) {
		postRepository.save(post);
	}
	
	public void updatePost(String id, Post post) {
		postRepository.save(post);
	}
	
	public void deletePost(String id) {
	     postRepository.deleteById(id);	
	}	
}

 

 

6. Test the Application!

There’ll be errors!

Now, if you test the application, errors will occur. But it’s ok. This is because we have not created entity relationships. However, if you get up to this point, then you followed the procedure correctly. In the next chapter, we would fix these errors.