{"id":1943,"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-8-to\/"},"modified":"2026-07-05T03:24:19","modified_gmt":"2026-07-05T01:24:19","slug":"hibernate-relationship-tutorial-onetomany-and-manytoone-part-8-to","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/hibernate-relationship-tutorial-onetomany-and-manytoone-part-8-to\/","title":{"rendered":"Hibernate Relationship Tutorial \u2013 @OneToMany and @ManyToOne (Part 8 to 12)"},"content":{"rendered":"<p>First let me say that if you have gotten to this point successfully, congrats! and thumbs up to you! You&#8217;ll sure be an expert if you keep this pace and don&#8217;t give up!<\/p>\n<p>We would continue with the Hibernate Relationship tutorial. We&#8217;ll cover the following:<\/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=\"#t8\">Part 8 \u2013 @JoinColumn Annotation<\/a><\/li>\n<li><a href=\"#t9\">Part 9 &#8211; Setup findAll() and findById() for all Entities<\/a><\/li>\n<li><a href=\"#t10\">Part 10 &#8211; Infinite Recursion(StackOverflow Error)<\/a><\/li>\n<li><a href=\"#t11\">Part 11 &#8211; Handling Infinite Recursion Using JsonManagedReference and JsonBackReference<\/a><\/li>\n<li><a href=\"#t12\">Part 12 &#8211; JsonIgnore and JsonIdentityInfo<\/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=\"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>Pay attention to how to solve the problem of <span style=\"color: #ff0000;\">Infinite recursion.<\/span><\/p>\n<p><a href=\"https:\/\/kindsonthegenius.com\/tempsite\/hibernate-relationship-tutorial-onetomany-and-manytoone\/\">This is Part 1 to 7<\/a><\/p>\n<p>&nbsp;<\/p>\n<p><strong id=\"t8\">Part 8 \u2013 @JoinColumn Annotation<\/strong><\/p>\n<p>The @JoinColumn annotation is used to specify the actual column(in the table) used for the mapping. This is done at the entity that owns the mapping.<\/p>\n<p>In the case of User-Post relationship, the Post owns the relationship and automatically creates the user_id column. This is the JoinColumn.<\/p>\n<p>Similarly, the\u00a0 Location-User relationship, the Location owns the relationship and provides the location_id column.<\/p>\n<p>Follow the steps below to add the @JoinColumn annotation<\/p>\n<p><strong>Step 1:<\/strong> In the Location field of the User entity, add @JoinColumn(name=&#8221;location_id&#8221;)<\/p>\n<p><strong>Step 2:<\/strong> In the User field of the Post entity, add @JoinColumn(name=&#8221;user_id&#8221;)<\/p>\n<p><strong>Step 3:<\/strong> Relaunch and check the tables in the h2 console (nothing changes though!)<\/p>\n<p>&nbsp;<\/p>\n<p><strong id=\"t9\">Part 9 &#8211; Setup findAll() and findById() for all Entities<\/strong><\/p>\n<p>Write the methods to in the RestController files for Post, User and Location.<\/p>\n<p>(You should be able to do this by now, else, watch the video and follow along)<\/p>\n<p>The three RestController and Services files would contain the following:<\/p>\n<p><strong>UserController<\/strong><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@Autowired<\/span>\n<span style=\"color: #008800; font-weight: bold;\">private<\/span> UserService userService<span style=\"color: #333333;\">;<\/span>\n\n<span style=\"color: #555555; font-weight: bold;\">@GetMapping<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"\/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;\">getAllUsers<\/span><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;\">findAll<\/span><span style=\"color: #333333;\">();<\/span>\n<span style=\"color: #333333;\">}<\/span>\n\n<span style=\"color: #555555; font-weight: bold;\">@GetMapping<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"\/users\/{id}\"<\/span><span style=\"color: #333333;\">)<\/span>\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> Optional<span style=\"color: #333333;\">&lt;<\/span>User<span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: #0066bb; font-weight: bold;\">getUserById<\/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;\">findById<\/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>UserService<\/strong><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@Autowired<\/span>\n<span style=\"color: #008800; font-weight: bold;\">private<\/span> UserRepository userRepository<span style=\"color: #333333;\">;<\/span>\n\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;\">getAllUsers<\/span><span style=\"color: #333333;\">()<\/span> <span style=\"color: #333333;\">{<\/span>\n\t<span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #333333;\">(<\/span>List<span style=\"color: #333333;\">&lt;<\/span>User<span style=\"color: #333333;\">&gt;)<\/span> userRepository<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">findAll<\/span><span style=\"color: #333333;\">();<\/span>\n<span style=\"color: #333333;\">}<\/span>\n\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> Optional<span style=\"color: #333333;\">&lt;<\/span>User<span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: #0066bb; font-weight: bold;\">getUserById<\/span><span style=\"color: #333333;\">(<\/span>Integer id<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\n\t<span style=\"color: #008800; font-weight: bold;\">return<\/span> userRepository<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: #333333;\">}<\/span>\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 1:<\/strong> Launch the application and visit http:\/\/localhost\/users to see the list of users.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>LocationController<\/strong><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@Autowired<\/span>\n<span style=\"color: #008800; font-weight: bold;\">private<\/span> LocationService locationService<span style=\"color: #333333;\">;<\/span>\n\t\n<span style=\"color: #555555; font-weight: bold;\">@GetMapping<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"\/locations\"<\/span><span style=\"color: #333333;\">)<\/span>\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> List<span style=\"color: #333333;\">&lt;<\/span>Location<span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: #0066bb; font-weight: bold;\">getAllLocations<\/span><span style=\"color: #333333;\">()<\/span> <span style=\"color: #333333;\">{<\/span>\n\t<span style=\"color: #008800; font-weight: bold;\">return<\/span> locationService<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">findAll<\/span><span style=\"color: #333333;\">();<\/span>\n<span style=\"color: #333333;\">}<\/span>\n\t\n<span style=\"color: #555555; font-weight: bold;\">@GetMapping<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"\/locations\/{id}\"<\/span><span style=\"color: #333333;\">)<\/span>\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> Optional<span style=\"color: #333333;\">&lt;<\/span>Location<span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: #0066bb; font-weight: bold;\">getLocationById<\/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\t<span style=\"color: #008800; font-weight: bold;\">return<\/span> locationService<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: #333333;\">}<\/span>\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>LocationService<\/strong><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@Autowired<\/span>\n<span style=\"color: #008800; font-weight: bold;\">private<\/span> LocationRepository locationRepository<span style=\"color: #333333;\">;<\/span> \n\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> List<span style=\"color: #333333;\">&lt;<\/span>Location<span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: #0066bb; font-weight: bold;\">findAll<\/span><span style=\"color: #333333;\">()<\/span> <span style=\"color: #333333;\">{<\/span>\n\t<span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #333333;\">(<\/span>List<span style=\"color: #333333;\">&lt;<\/span>Location<span style=\"color: #333333;\">&gt;)<\/span> locationRepository<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">findAll<\/span><span style=\"color: #333333;\">();<\/span>\n<span style=\"color: #333333;\">}<\/span>\n\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> Optional<span style=\"color: #333333;\">&lt;<\/span>Location<span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: #0066bb; font-weight: bold;\">findById<\/span><span style=\"color: #333333;\">(<\/span>Integer id<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\n\t<span style=\"color: #008800; font-weight: bold;\">return<\/span> locationRepository<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: #333333;\">}<\/span>\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>PostController<\/strong><\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@Autowired<\/span>\n<span style=\"color: #008800; font-weight: bold;\">private<\/span> PostService postService<span style=\"color: #333333;\">;<\/span>\n\n<span style=\"color: #555555; font-weight: bold;\">@GetMapping<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"\/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;\">getAllPosts<\/span><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;\">findAll<\/span><span style=\"color: #333333;\">();<\/span>\n<span style=\"color: #333333;\">}<\/span>\n\n<span style=\"color: #555555; font-weight: bold;\">@GetMapping<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"\/posts\/{id}\"<\/span><span style=\"color: #333333;\">)<\/span>\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> Optional<span style=\"color: #333333;\">&lt;<\/span>Post<span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: #0066bb; font-weight: bold;\">getPostById<\/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;\">findById<\/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>PostService<\/strong><\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">private<\/span> PostRepository postRepository<span style=\"color: #333333;\">;<\/span>\n\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;\">findAll<\/span><span style=\"color: #333333;\">()<\/span> <span style=\"color: #333333;\">{<\/span>\n\t<span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #333333;\">(<\/span>List<span style=\"color: #333333;\">&lt;<\/span>Post<span style=\"color: #333333;\">&gt;)<\/span> postRepository<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">findAll<\/span><span style=\"color: #333333;\">();<\/span>\n<span style=\"color: #333333;\">}<\/span>\n\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> Optional<span style=\"color: #333333;\">&lt;<\/span>Post<span style=\"color: #333333;\">&gt;<\/span> <span style=\"color: #0066bb; font-weight: bold;\">findById<\/span><span style=\"color: #333333;\">(<\/span>Integer id<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\n\t<span style=\"color: #008800; font-weight: bold;\">return<\/span> postRepository<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: #333333;\">}<\/span>\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong id=\"t10\">Part 10 &#8211; Infinite Recursion (StackOverflow Error)<\/strong><\/p>\n<p>We would now solve a problem that always arises when you have mapping between entities. Let&#8217;s see what the problem is first!<\/p>\n<p><strong>Step 1:<\/strong> In the User entity, right-click and generate Getter and Setters for the posts field<\/p>\n<p><strong>Step 2:<\/strong> In the Location entity, right-click and generate Getter and Setter for the users field<\/p>\n<p><strong>Step 3:<\/strong> Relaunch the application. Try to access the list of users via http:\/\/localhost:8080\/users.\u00a0You will notice, that it produces an infinite number of items. In you look at the console, you will now see this error.<\/p>\n<p>Let&#8217;s solve it!<\/p>\n<p>&nbsp;<\/p>\n<p><strong id=\"t11\">Part 11- Solving Infinite Recursion using @JsonManagedReference and @JsonBackReference<\/strong><\/p>\n<p>So we need a way to break the infinite recursion loop. The @JsonManagedReference is used on the OneToMany side while the @JsonBackReference is used at the @ManyToOne side.<\/p>\n<p>@JsonManagedReference is the forward part of the mapping\/reference and the data gets serialized normally.<\/p>\n<p>@JsonBackReference is is the backward side of the mapping and the data\u00a0 does not get serialized<\/p>\n<p>Follow the steps below:<\/p>\n<p><strong>Step 1:<\/strong> Add @JsonManagedReference to the getPosts method in the User entity<\/p>\n<p><strong>Step 2:<\/strong> Add @JsonBackReference to the getUser method of the Post entity<\/p>\n<p><strong>Step 3:<\/strong> Add @JsonManagedReference to the getUsers method of the Location entity<\/p>\n<p><strong>Step 4:<\/strong> Add @JsonBackReference to the getLocation method of the User entity<\/p>\n<p><strong>Step 5:<\/strong> Relaunch the application, try to access list of Users, Locations, or Posts. Notice that the problem is solved<\/p>\n<p>Other methods of handling this problem include use of @JsonIdentityInfo and JsonIgnore annotations. These are discussed below<\/p>\n<p>&nbsp;<\/p>\n<p><strong id=\"t12\">Part 12 &#8211; @JsonIdentifyInfo and @JsonIgnore<\/strong><\/p>\n<p>Now, the @JsonIgnore is an alternative\u00a0 for the @JsonBackReference. So you can used @JsonIgnore in place of @JsonBackReference<\/p>\n<p>@JsonIdentityInfo can be used in place of the both @JsonManagedReference and @JsonBackReference. However, the @JsonIdentityInfo annotation is added to the class and NOT to the methods. Also, the @JsonIdentityInfo requires some attributes as shown in the code below:<\/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;\">@JsonIdentityInfo<\/span><span style=\"color: #333333;\">(<\/span>\n\tgenerator <span style=\"color: #333333;\">=<\/span> ObjectIdGenerators<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">PropertyGenerator<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">class<\/span><span style=\"color: #333333;\">,<\/span> \n\tproperty <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"id\"<\/span><span style=\"color: #333333;\">)<\/span>\n<\/pre>\n<p>&nbsp;<\/p>\n<p>If you have gotten to this point successfully, congrats! and thumbs up to you! You&#8217;ll sure be an expert if you keep this pace and don&#8217;t give up. Feel free to watch the videos and if you have challenges, mention it in the comment box either below this page or below the video.<\/p>\n<p>Now, on to the next Part!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>First let me say that if you have gotten to this point successfully, congrats! and thumbs up to you! You&#8217;ll sure be an expert if &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-1943","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1943","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=1943"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1943\/revisions"}],"predecessor-version":[{"id":2111,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1943\/revisions\/2111"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=1943"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=1943"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=1943"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}