{"id":1944,"date":"2019-09-01T12:00:00","date_gmt":"2019-09-01T10:00:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/hibernate-relationship-tutorial-onetomany-and-manytoone-part-13-to-16\/"},"modified":"2026-07-05T03:24:21","modified_gmt":"2026-07-05T01:24:21","slug":"hibernate-relationship-tutorial-onetomany-and-manytoone-part-13-to-16","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/hibernate-relationship-tutorial-onetomany-and-manytoone-part-13-to-16\/","title":{"rendered":"Hibernate Relationship Tutorial \u2013 @OneToMany and @ManyToOne (Part 13 to 16)"},"content":{"rendered":"<p>Now we continue with Part 13.<br \/>\nWe are still working with the GET requests<\/p>\n<ul>\n<li><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/hibernate-relationship-tutorial-onetomany-and-manytoone\/\">Part 1 to 7 &#8211; Setting Up<\/a><\/li>\n<li><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/hibernate-relationship-tutorial-onetomany-and-manytoone-part-8-to\/\">Part 8 to 12 &#8211; Infinite Recursion, @JoinColumn, JsonManagedReference etc<\/a><\/li>\n<li><a href=\"#t13\">Part 13\u00a0\u2013 Get List of Users by Location from the LocationController<\/a><\/li>\n<li><a href=\"#t14\">Part 14 \u2013 Get List of Posts by User from the UserController<\/a><\/li>\n<li><a href=\"#t15\">Part 15 \u2013 List of Users By Location from UserController by Extending JPARepository<\/a><\/li>\n<li><a href=\"#t16\">Part 16 &#8211; Get List of Posts by User from PostContoller by Extending JPARepository<\/a><\/li>\n<li><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/hibernate-relationship-tutorial-onetomany-and-manytoone-part-17-to\/\">Part 17 to 20 &#8211; Inserting New Records. Using REST Client<\/a><\/li>\n<li><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/hibernate-relationship-tutorial-onetomany-and-manytoone-part-21-to-8\/\">Part 21 to 28 &#8211; Deleting, CascadeTypes and FetchTypes<\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><strong>Part 13\u00a0\u2013 Get List of Users by Location<\/strong><\/p>\n<p>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.<\/p>\n<p>This method is written as follows in the LocationController.<\/p>\n<p><strong id=\"t13\">Step 1:<\/strong> Copy the code below and paste inside the LocationController<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@GetMapping<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"\/location\/{id}\/users\"<\/span><span style=\"color: #333333;\">)<\/span>\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> List<span style=\"color: #333333;\">&lt;<\/span>User<span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: #0066bb; font-weight: bold;\">GetUsersByLocation<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #555555; font-weight: bold;\">@PathVariable<\/span> Integer id<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\n    Optional<span style=\"color: #333333;\">&lt;<\/span>Location<span style=\"color: #333333;\">&gt;<\/span> location <span style=\"color: #333333;\">=<\/span> locationService<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">findById<\/span><span style=\"color: #333333;\">(<\/span>id<span style=\"color: #333333;\">);<\/span>\t\t\n    <span style=\"color: #008800; font-weight: bold;\">if<\/span><span style=\"color: #333333;\">(<\/span>location<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">isPresent<\/span><span style=\"color: #333333;\">())<\/span> <span style=\"color: #333333;\">{<\/span>\n\t<span style=\"color: #008800; font-weight: bold;\">return<\/span> location<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">get<\/span><span style=\"color: #333333;\">().<\/span><span style=\"color: #0000cc;\">getUsers<\/span><span style=\"color: #333333;\">();<\/span>\n    <span style=\"color: #333333;\">}<\/span>\t\t\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #008800; font-weight: bold;\">null<\/span><span style=\"color: #333333;\">;<\/span>\n<span style=\"color: #333333;\">}<\/span>\n<\/pre>\n<p>Listing 1: GetUsersByLocation() method<\/p>\n<p>In Listing 1, we first get the Optional&lt;Location&gt; 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.<\/p>\n<p>Also observe, the url pattern &#8220;\/location\/{id}\/users&#8221;<\/p>\n<p><strong>Step 2:<\/strong> Relaunch the application and try to access \/location\/1\/users. List of users is returned for that location.<\/p>\n<p>&nbsp;<\/p>\n<p><strong id=\"t14\">Part 14 &#8211; Get List of Posts by User<\/strong><\/p>\n<p>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<\/p>\n<p><strong>Step 1:<\/strong> Paste the code below in the UserController file<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@GetMapping<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"\/user\/{id}\/posts\"<\/span><span style=\"color: #333333;\">)<\/span>\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> List<span style=\"color: #333333;\">&lt;<\/span>Post<span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: #0066bb; font-weight: bold;\">getPostsByUser<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #555555; font-weight: bold;\">@PathVariable<\/span> Integer id<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\n    Optional<span style=\"color: #333333;\">&lt;<\/span>User<span style=\"color: #333333;\">&gt;<\/span> user <span style=\"color: #333333;\">=<\/span> userService<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">findById<\/span><span style=\"color: #333333;\">(<\/span>id<span style=\"color: #333333;\">);<\/span>\n    <span style=\"color: #008800; font-weight: bold;\">if<\/span><span style=\"color: #333333;\">(<\/span>user<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">isPresent<\/span><span style=\"color: #333333;\">())<\/span> <span style=\"color: #333333;\">{<\/span>\n\t<span style=\"color: #008800; font-weight: bold;\">return<\/span> user<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">get<\/span><span style=\"color: #333333;\">().<\/span><span style=\"color: #0000cc;\">getPosts<\/span><span style=\"color: #333333;\">();<\/span>\n    <span style=\"color: #333333;\">}<\/span>\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #008800; font-weight: bold;\">null<\/span><span style=\"color: #333333;\">;<\/span>\n<span style=\"color: #333333;\">}<\/span>\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 2:<\/strong> Relaunch and test the application<\/p>\n<p>&nbsp;<\/p>\n<p><strong id=\"t15\">Part 15 \u2013 List of Users By Location from UserController by Extending JPARepository<\/strong><\/p>\n<p>Now you can skip this part if you want. It&#8217;s just a different way of achieving the same thing.<\/p>\n<p>Here, we would receive the Location_id as a PathVariable and use it to filter the list of users. We&#8217;ll achieve this by extending the UserRepository.<\/p>\n<p><strong>Step 1:<\/strong> Write the code below in the UserRepository.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">List<span style=\"color: #333333;\">&lt;<\/span>User<span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: #0066bb; font-weight: bold;\">findByLocationId<\/span><span style=\"color: #333333;\">(<\/span>Integer id<span style=\"color: #333333;\">);<\/span>\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 2:<\/strong> Write the following method in the UserService<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">public<\/span> List<span style=\"color: #333333;\">&lt;<\/span>User<span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: #0066bb; font-weight: bold;\">getUsersByLocation<\/span><span style=\"color: #333333;\">(<\/span>Integer id<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> userRepository<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">findByLocationId<\/span><span style=\"color: #333333;\">(<\/span>id<span style=\"color: #333333;\">);<\/span>\n<span style=\"color: #333333;\">}<\/span>\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 3:<\/strong> Write the following method in the UserController<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@GetMapping<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"\/users\/location\/{id}\/users\"<\/span><span style=\"color: #333333;\">)<\/span>\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> List<span style=\"color: #333333;\">&lt;<\/span>User<span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: #0066bb; font-weight: bold;\">getUsersByLocation<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #555555; font-weight: bold;\">@PathVariable<\/span> Integer id<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> userService<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">getUsersByLocation<\/span><span style=\"color: #333333;\">(<\/span>id<span style=\"color: #333333;\">);<\/span>\n<span style=\"color: #333333;\">}<\/span>\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Notice that the url pattern is &#8220;\/users\/location\/{id}\/users&#8221;<\/p>\n<p><strong>Step 4<\/strong>: Launch the application and test it by accessing http:\/\/localhost\/users\/location\/1\/users<\/p>\n<p>&nbsp;<\/p>\n<p><strong id=\"t16\">Part 16 &#8211; Get List of Posts by User from PostController\u00a0 by Extending JPARepository<\/strong><\/p>\n<p>Now you can skip this part if you want. It&#8217;s just a different way of achieving the same thing.<\/p>\n<p>We&#8217;ll do something similar to what we did in Part 15, but this time with the Post.<\/p>\n<p><strong>Step 1:<\/strong> Copy and paste the code below in the PostRepository<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">List<span style=\"color: #333333;\">&lt;<\/span>Post<span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: #0066bb; font-weight: bold;\">findByUserId<\/span><span style=\"color: #333333;\">(<\/span>Integer id<span style=\"color: #333333;\">);<\/span>\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 2:<\/strong> Paste the code below in the PostService<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">public<\/span> List<span style=\"color: #333333;\">&lt;<\/span>Post<span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: #0066bb; font-weight: bold;\">findByLocationId<\/span><span style=\"color: #333333;\">(<\/span>Integer id<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> postRepository<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">findByUserId<\/span><span style=\"color: #333333;\">(<\/span>id<span style=\"color: #333333;\">);<\/span>\n<span style=\"color: #333333;\">}<\/span>\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 3:<\/strong> Paste the code below in the PostController<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@GetMapping<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"\/posts\/user\/{id}\/posts\"<\/span><span style=\"color: #333333;\">)<\/span>\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> List<span style=\"color: #333333;\">&lt;<\/span>Post<span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: #0066bb; font-weight: bold;\">getPostsByUser<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #555555; font-weight: bold;\">@PathVariable<\/span> Integer id<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> postService<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">findByLocationId<\/span><span style=\"color: #333333;\">(<\/span>id<span style=\"color: #333333;\">);<\/span>\n<span style=\"color: #333333;\">}<\/span>\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 4:<\/strong> Relaunch and test the application<\/p>\n<p>If you have gotten to this point, then, thumbs up to you! Just keep moving!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Now we continue with Part 13. We are still working with the GET requests Part 1 to 7 &#8211; Setting Up Part 8 to 12 &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[414],"tags":[],"class_list":["post-1944","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1944","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=1944"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1944\/revisions"}],"predecessor-version":[{"id":2112,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1944\/revisions\/2112"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=1944"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=1944"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=1944"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}