Spring Boot – Understanding Entity Relationships

In this lesson, we are going to add entity relationships between the classes.

First lets look ate the relationship between the classes. This is shown in Figure 1.0

 

Figure 1.0: Relationship diagram for our SocialAPI

  1. User-Location Relationship
  2. Post-User Relationship
  3. Get User by Location
  4. Get Post by User

 

1. User-Location Relationship

This is an example of has-a relationship. This means that a user object has a location. Also a Location can have more than on user. Or many users can share one location. Therefore we call this relationship many-to-one.

 

Add the @ManyToOne Annotation to the User class

Add the @ManyToOne annotation to the User class under the Location member variable. This is as shown below:

 

@Entity
public class User {
	
    @Id
    private String id;
    private String firstname;
    private String lastname;
    
    @ManyToOne
    private Location location;  
    
    private String email;
    ...
    ...

 

 

2. Post-User Relationship

This is also an example of has-a relationship. This means that a user object has a location. Also a User can have more than on Post. Or many Post can be created by one User. In. Therefore we also call this relationship many-to-one.

 

Add the @ManyToOne Annotation to the Post class

Add the @ManyToOne annotation to the Post class under the User member variable.

This is as shown below:

 

@Entity
public class Post {
	
   @Id
   private String id;
   private String postdate;
   
   @ManyToOne
   private User user;
   
   private String details;

 

 

3. Getting Users by Location

Since many Users can have one Location, we would like to get List of User by Location. To do this, we need to write the definition in the UserRepository interface.

Open the UserRepository class and write add the line below.

 

   public List<User> findByLocationId(String locationId); 

 

Now we need to add similar method to the UserService.

The method is similar to getAllUsers, but this time we use getUsersByLocationId. The code is given below:

 

public List<User> getUsersByLocation(String id) {
    List<User> users = new ArrayList<>();
	
    userRepository.findByLocationId(id)
    .forEach(users::add);
	
    return users;
}

 

 

4. Getting Posts By User

Since many Posts can be made by on user, we would like to get List of Post by User. To do this, we need to write the definition in the PostRepository interface.

Open the PostRepository class and write add the line below.

 

   public List<Post> findByUserId(String userId);

 

Then we add a similar method to the PostService. The method would be getPostsByUserId. The code is given below:

 

public List<Post> getPostsByUser(String id) {
   List<Post> posts = new ArrayList<>();
   
   postRepository.findByUserId(id)
   .forEach(posts::add);
   
   return posts;	 
}

 

Now we’ve come this far. But we are not done yet. We need to write the controller methods. But before do that I would like to explain a bit about RequestMappings. This we would look cover in the next Chapter.