Now we continue with Part 13.
We are still working with the GET requests
- Part 1 to 7 – Setting Up
- Part 8 to 12 – Infinite Recursion, @JoinColumn, JsonManagedReference etc
- Part 13 – Get List of Users by Location from the LocationController
- Part 14 – Get List of Posts by User from the UserController
- Part 15 – List of Users By Location from UserController by Extending JPARepository
- Part 16 – Get List of Posts by User from PostContoller by Extending JPARepository
- Part 17 to 20 – Inserting New Records. Using REST Client
- Part 21 to 28 – Deleting, CascadeTypes and FetchTypes
Part 13 – Get List of Users by Location
Now we would try to get the list of users in a location. So we would write a method in the LocationController that takes the location_id and returns the users in that location.
This method is written as follows in the LocationController.
Step 1: Copy the code below and paste inside the LocationController
@GetMapping("/location/{id}/users") public List<User> GetUsersByLocation(@PathVariable Integer id) { Optional<Location> location = locationService.findById(id); if(location.isPresent()) { return location.get().getUsers(); } return null; }
Listing 1: GetUsersByLocation() method
In Listing 1, we first get the Optional<Location> object using findById. Then if the location exists, then we get the actual Location object. Finally we get the list of users in that location using the getUser() member function of the the Location class.
Also observe, the url pattern “/location/{id}/users”
Step 2: Relaunch the application and try to access /location/1/users. List of users is returned for that location.
Part 14 – Get List of Posts by User
We would do the same thing with the List of Posts. This is similar to what we did in part 14. I would recommend you try to do this yourself, before you use the code below
Step 1: Paste the code below in the UserController file
@GetMapping("/user/{id}/posts") public List<Post> getPostsByUser(@PathVariable Integer id) { Optional<User> user = userService.findById(id); if(user.isPresent()) { return user.get().getPosts(); } return null; }
Step 2: Relaunch and test the application
Part 15 – List of Users By Location from UserController by Extending JPARepository
Now you can skip this part if you want. It’s just a different way of achieving the same thing.
Here, we would receive the Location_id as a PathVariable and use it to filter the list of users. We’ll achieve this by extending the UserRepository.
Step 1: Write the code below in the UserRepository.
List<User> findByLocationId(Integer id);
Step 2: Write the following method in the UserService
public List<User> getUsersByLocation(Integer id) { return userRepository.findByLocationId(id); }
Step 3: Write the following method in the UserController
@GetMapping("/users/location/{id}/users") public List<User> getUsersByLocation(@PathVariable Integer id) { return userService.getUsersByLocation(id); }
Notice that the url pattern is “/users/location/{id}/users”
Step 4: Launch the application and test it by accessing http://localhost/users/location/1/users
Part 16 – Get List of Posts by User from PostController by Extending JPARepository
Now you can skip this part if you want. It’s just a different way of achieving the same thing.
We’ll do something similar to what we did in Part 15, but this time with the Post.
Step 1: Copy and paste the code below in the PostRepository
List<Post> findByUserId(Integer id);
Step 2: Paste the code below in the PostService
public List<Post> findByLocationId(Integer id) { return postRepository.findByUserId(id); }
Step 3: Paste the code below in the PostController
@GetMapping("/posts/user/{id}/posts") public List<Post> getPostsByUser(@PathVariable Integer id) { return postService.findByLocationId(id); }
Step 4: Relaunch and test the application
If you have gotten to this point, then, thumbs up to you! Just keep moving!
4 thoughts on “Hibernate Relationship Tutorial – @OneToMany and @ManyToOne (Part 13 to 16)”