{"id":1945,"date":"2019-09-02T12:00:00","date_gmt":"2019-09-02T10:00:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/hibernate-relationship-tutorial-onetomany-and-manytoone-part-17-to\/"},"modified":"2026-07-05T03:24:23","modified_gmt":"2026-07-05T01:24:23","slug":"hibernate-relationship-tutorial-onetomany-and-manytoone-part-17-to","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/hibernate-relationship-tutorial-onetomany-and-manytoone-part-17-to\/","title":{"rendered":"Hibernate Relationship Tutorial \u2013 @OneToMany and @ManyToOne (Part 17 to 20 )"},"content":{"rendered":"<p>Good to know you&#8217;ve completed Parts 1 to 16! Thumbs up to you! Otherwise, you can see the links below and follow.<\/p>\n<p>Now we&#8217;ll continue with Part 17, focusing on inserting new item to the database using the repository.<\/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=\"https:\/\/kindsonthegenius.com\/tempsite\/hibernate-relationship-tutorial-onetomany-and-manytoone-part-13-to-16\/\">Part 13 to 16 &#8211; Retrieving Child Records<\/a><\/li>\n<li><a href=\"#t17\">Part 17 &#8211;\u00a0 Set up Advanced Rest Client<\/a><\/li>\n<li><a href=\"#t18\">Part 18 &#8211; Inserting a New Location<\/a><\/li>\n<li><a href=\"#t19\">Part 19 &#8211; Inserting a New User<\/a><\/li>\n<li><a href=\"#t20\">Part 20 &#8211; Inserting a New Post<\/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 id=\"t17\">Part 17 &#8211; Setup Advanced REST Client<\/strong><\/p>\n<p>Before you can make\u00a0 a POST request to the application, you need to have a REST Client (you can&#8217;t use the browser!).\u00a0 For me I use Advanced REST Client. Postman REST Client is ok too.\u00a0 ARC\u00a0 is an Add-on to google Chrome. To set it up Advanced REST Client on Google Chrome, follow the steps<\/p>\n<p><strong>Step 1:<\/strong> Follow the link below to setup Advanced REST Client<\/p>\n<p>https:\/\/chrome.google.com\/webstore\/detail\/advanced-rest-client\/hgmloofddffdnphfgcellkdfbfbjeloo<\/p>\n<p><strong>Step 2:<\/strong> Install it and then launch it. The interface is a shown below<\/p>\n<p>&nbsp;<\/p>\n<figure id=\"attachment_1178\" aria-describedby=\"caption-attachment-1178\" style=\"width: 807px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Advanced-REST-Client.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1178\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/Advanced-REST-Client.jpg\" alt=\"Advanced REST Client\" width=\"807\" height=\"594\" \/><\/a><figcaption id=\"caption-attachment-1178\" class=\"wp-caption-text\">Advanced REST Client GUI<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p><strong id=\"t18\">Part 18: Add New Location<\/strong><\/p>\n<p>To Insert a new Location we need to make a POST request.<\/p>\n<p><strong>Step 1<\/strong>: Write the AddLocation method in the LocationService. Copy and paste in the LocationService<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">AddLocation<\/span><span style=\"color: #333333;\">(<\/span>Location location<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\n     locationRepository<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">save<\/span><span style=\"color: #333333;\">(<\/span>location<span style=\"color: #333333;\">);<\/span>\n<span style=\"color: #333333;\">}<\/span>\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 2<\/strong>: Write the AddLocation method in the LocationController. The method is given below. Copy and paste in the LocationController<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@PostMapping<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"\/locations\/addNew\"<\/span><span style=\"color: #333333;\">)<\/span>\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">AddLocation<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #555555; font-weight: bold;\">@RequestBody<\/span> Location location<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\n    locationService<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">AddLocation<\/span><span style=\"color: #333333;\">(<\/span>location<span style=\"color: #333333;\">);<\/span>\n<span style=\"color: #333333;\">}<\/span>\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Step 3:<\/strong> Relaunch the application.<\/p>\n<p><strong>Step 4:<\/strong> Make a POST request to the\u00a0http:\/\/localhost:8080\/locations\/addNew url using Advanced Rest Client. The body of the request is as shown below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">{\n    <span style=\"color: #007700;\">\"id\"<\/span>: <span style=\"color: #0000dd; font-weight: bold;\">5<\/span>,\n    <span style=\"color: #007700;\">\"name\"<\/span>: <span style=\"background-color: #fff0f0;\">\"KanysTown\"<\/span>\n}\n<\/pre>\n<p>&nbsp;<\/p>\n<p>S<strong>tep 5:<\/strong> Now access the http:\/\/localhost\/locations url to ensure the location is inserted. Then add a couple more locations.<\/p>\n<p>&nbsp;<\/p>\n<p><strong id=\"t19\">Part 19 &#8211; Inserting a New User<\/strong><\/p>\n<p>We would also do the same as we did with adding new Location, but with a little variation. We&#8217;ll make some modifications to the User entity and to our data.sql file.<\/p>\n<p><strong>Step 1:<\/strong> Launch the application and access the H2 Console to check\u00a0 what columns are there in the User table. Notice that there is a column in the User table called location_Id.<\/p>\n<p><strong>Step 2:<\/strong> Open the data.sql file. In the code to insert location, change the location_id to locationid (remove the underscore)<\/p>\n<p><strong>Step 3:<\/strong> Open the User model class. Add a new field named locationid.<\/p>\n<p><strong>Step 4:<\/strong> Add the getters and setters for the new locationid field<\/p>\n<p><strong>Step 5:<\/strong> In the @JoinColumn annotation, change the attribute location_id to locationid.<\/p>\n<p><strong>Step 6:<\/strong> Add new attributes to the @JoinColumn: insertable= false, updatable= false<\/p>\n<p>At the end all the changes you have made is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">...\n...\n@ManyToOne<\/span>\n<span style=\"color: #555555; font-weight: bold;\">@JoinColumn<\/span><span style=\"color: #333333;\">(<\/span>name<span style=\"color: #333333;\">=<\/span><span style=\"background-color: #fff0f0;\">\"locationid\"<\/span><span style=\"color: #333333;\">,<\/span> insertable <span style=\"color: #333333;\">=<\/span> <span style=\"color: #008800; font-weight: bold;\">false<\/span><span style=\"color: #333333;\">,<\/span> updatable<span style=\"color: #333333;\">=<\/span><span style=\"color: #008800; font-weight: bold;\">false<\/span><span style=\"color: #333333;\">)<\/span>\n<span style=\"color: #008800; font-weight: bold;\">private<\/span> Location location<span style=\"color: #333333;\">;<\/span>\n\n<span style=\"color: #008800; font-weight: bold;\">private<\/span> Integer locationid<span style=\"color: #333333;\">;<\/span>\n\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> Integer <span style=\"color: #0066bb; font-weight: bold;\">getLocationid<\/span><span style=\"color: #333333;\">()<\/span> <span style=\"color: #333333;\">{<\/span>\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> locationid<span style=\"color: #333333;\">;<\/span>\n<span style=\"color: #333333;\">}<\/span>\n\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">setLocationid<\/span><span style=\"color: #333333;\">(<\/span>Integer locationid<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\n    <span style=\"color: #008800; font-weight: bold;\">this<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">locationid<\/span> <span style=\"color: #333333;\">=<\/span> locationid<span style=\"color: #333333;\">;<\/span>\n<span style=\"color: #333333;\">}\n<\/span>...\n...<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 7:<\/strong> Open the UserService and add the following code to insert new User<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">UpdateUser<\/span><span style=\"color: #333333;\">(<\/span>User user<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\n\tuserRepository<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">save<\/span><span style=\"color: #333333;\">(<\/span>user<span style=\"color: #333333;\">);<\/span>\n<span style=\"color: #333333;\">}<\/span>\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 8:<\/strong> Open the UserController and add the following code<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@PostMapping<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"\/users\/addNew\"<\/span><span style=\"color: #333333;\">)<\/span>\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">AddUser<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #555555; font-weight: bold;\">@RequestBody<\/span> User user<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\n    userService<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">AddUser<\/span><span style=\"color: #333333;\">(<\/span>user<span style=\"color: #333333;\">);<\/span>\n<span style=\"color: #333333;\">}<\/span>\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 10:<\/strong> Launch the application and an try to insert a new user using Advanced REST Client. If it works, thumbs up to you! Otherwise, let me know.<\/p>\n<p>&nbsp;<\/p>\n<p><strong id=\"t20\">Part 20 &#8211; Inserting a New Post<\/strong><\/p>\n<p>Try to make the changes similar to the ones we made to insert a User. Try it and see if you get it, before following the procedure. The procedures are below:<\/p>\n<p><strong>Step 1:<\/strong> Open the data.sql file. Change the post_id to postid for the insert into POST queries.<\/p>\n<p><strong>Step 2<\/strong>: In the Post model, add a new field named postid.<\/p>\n<p><strong>Step 3:<\/strong> Add the getters and setters for the new postid field<\/p>\n<p><strong>Step 4:<\/strong> In the @JoinColumn annotation, change the post_id to postid.<\/p>\n<p><strong>Step 5:<\/strong> Add updatable and insertable attribute and set them to false<\/p>\n<p>At the end, the changes to the\u00a0 Post model would be as follows:<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@ManyToOne<\/span>\n<span style=\"color: #555555; font-weight: bold;\">@JoinColumn<\/span><span style=\"color: #333333;\">(<\/span>name<span style=\"color: #333333;\">=<\/span><span style=\"background-color: #fff0f0;\">\"userid\"<\/span><span style=\"color: #333333;\">,<\/span> insertable<span style=\"color: #333333;\">=<\/span><span style=\"color: #008800; font-weight: bold;\">false<\/span><span style=\"color: #333333;\">,<\/span> updatable<span style=\"color: #333333;\">=<\/span><span style=\"color: #008800; font-weight: bold;\">false<\/span><span style=\"color: #333333;\">)<\/span>\n<span style=\"color: #008800; font-weight: bold;\">private<\/span> User user<span style=\"color: #333333;\">;<\/span>\n\n<span style=\"color: #008800; font-weight: bold;\">private<\/span> Integer userid<span style=\"color: #333333;\">;<\/span>\t\n\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> Integer <span style=\"color: #0066bb; font-weight: bold;\">getUserid<\/span><span style=\"color: #333333;\">()<\/span> <span style=\"color: #333333;\">{<\/span>\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> userid<span style=\"color: #333333;\">;<\/span>\n<span style=\"color: #333333;\">}<\/span>\n\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">setUserid<\/span><span style=\"color: #333333;\">(<\/span>Integer userid<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\n    <span style=\"color: #008800; font-weight: bold;\">this<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">userid<\/span> <span style=\"color: #333333;\">=<\/span> userid<span style=\"color: #333333;\">;<\/span>\n<span style=\"color: #333333;\">}<\/span>\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 6:<\/strong> Launch the application and make a request using the Advanced REST Client. The Json file is as shown below<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">{\n<span style=\"color: #007700;\">\"id\"<\/span>: <span style=\"color: #0000dd; font-weight: bold;\">4<\/span>,\n<span style=\"color: #007700;\">\"postDate\"<\/span>: <span style=\"background-color: #fff0f0;\">\"2019-09-02T17:42:03.878\"<\/span>,\n<span style=\"color: #007700;\">\"userid\"<\/span>: <span style=\"color: #0000dd; font-weight: bold;\">2<\/span>,\n<span style=\"color: #007700;\">\"details\"<\/span>: <span style=\"background-color: #fff0f0;\">\"Kindson The Genius\"<\/span>\n}\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Check the H2 Console to ensure that the data inserted correctly.<\/p>\n<p>At this point, you have completed this Part. Thumbs up to you! You are a genius!<\/p>\n<p>Let&#8217;s now see how to perform updates on the entities.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Good to know you&#8217;ve completed Parts 1 to 16! Thumbs up to you! Otherwise, you can see the links below and follow. Now we&#8217;ll continue &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[414],"tags":[],"class_list":["post-1945","post","type-post","status-publish","format-standard","hentry","category-programming"],"acf":[],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1945","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=1945"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1945\/revisions"}],"predecessor-version":[{"id":2113,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1945\/revisions\/2113"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=1945"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=1945"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=1945"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}