{"id":1934,"date":"2019-07-10T12:00:00","date_gmt":"2019-07-10T10:00:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/relationships-in-hibernate-with-springbootonetomany-and-manytoone\/"},"modified":"2026-07-05T03:23:58","modified_gmt":"2026-07-05T01:23:58","slug":"relationships-in-hibernate-with-springbootonetomany-and-manytoone","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/relationships-in-hibernate-with-springbootonetomany-and-manytoone\/","title":{"rendered":"Relationships in Hibernate with SpringBoot(@OneToMany and @ManyToOne)"},"content":{"rendered":"<p>In this lesson, I would explain to you how relationships works in hibernate. Then I would give you a practical as well.<\/p>\n<p>We would be using two classes:<\/p>\n<ul>\n<li>Student: List of students with fields &#8211; Id, firstname and lastname<\/li>\n<li>Department: List of department with fields &#8211; Id, name and description<\/li>\n<\/ul>\n<p>The Student class would correspond to the Student table while the Department class would correspond to Department table.<\/p>\n<p>Now the relationship is such that:<\/p>\n<p>many student would belong to one department &#8211; that is ManyToOne (many students, one department)<\/p>\n<p>one department can contain many students &#8211; that is OneToMany(on department, many students)<\/p>\n<p>&nbsp;<\/p>\n<p><strong>How then do we implement this in Spring?<\/strong><\/p>\n<p>So let&#8217;s begin with the<strong> Student class<\/strong>. This is shown below. Notice that we have included an additional member variable: department.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@Entity<\/span>\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Student<\/span> <span style=\"color: #333333;\">{<\/span>\n\t\n\t<span style=\"color: #555555; font-weight: bold;\">@Id<\/span>\n\t<span style=\"color: #008800; font-weight: bold;\">private<\/span> Integer Id<span style=\"color: #333333;\">;<\/span>\n\t<span style=\"color: #008800; font-weight: bold;\">private<\/span> String firstname<span style=\"color: #333333;\">;<\/span>\n\t<span style=\"color: #008800; font-weight: bold;\">private<\/span> String lastname<span style=\"color: #333333;\">;<\/span>\n\t\n\t<span style=\"color: #555555; font-weight: bold;\">@ManyToOne<\/span>\n\t<span style=\"color: #008800; font-weight: bold;\">private<\/span> Department department<span style=\"color: #333333;\">;<\/span>\n\t\n\t<span style=\"color: #888888;\">\/\/constructor<\/span>\n\t<span style=\"color: #888888;\">\/\/getters and setters<\/span>\n<span style=\"color: #333333;\">}<\/span>\n<\/pre>\n<p>&nbsp;<\/p>\n<p>In the Student class, we have add the<strong> @<em style=\"font-size: 1rem;\">ManyToOne<\/em><\/strong><span style=\"font-size: 1rem;\">\u00a0annotation to the department variable. So, this simply tells hibernate that a student belongs to a department. Also, that many student can have one department.<\/span><\/p>\n<p>Now let&#8217;s look at the<strong> Department class.<\/strong> This is shown below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@Entity<\/span>\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">class<\/span> <span style=\"color: #bb0066; font-weight: bold;\">Department<\/span> <span style=\"color: #333333;\">{<\/span>\n\t\n\t<span style=\"color: #555555; font-weight: bold;\">@Id<\/span>\n\t<span style=\"color: #008800; font-weight: bold;\">private<\/span> Integer Id<span style=\"color: #333333;\">;<\/span>\n\t<span style=\"color: #008800; font-weight: bold;\">private<\/span> String name<span style=\"color: #333333;\">;<\/span>\n\t<span style=\"color: #008800; font-weight: bold;\">private<\/span> String description<span style=\"color: #333333;\">;<\/span>\n\t\n\t<span style=\"color: #555555; font-weight: bold;\">@OneToMany<\/span><span style=\"color: #333333;\">(<\/span>mappedBy <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"department\"<\/span><span style=\"color: #333333;\">)<\/span>\n\t<span style=\"color: #008800; font-weight: bold;\">private<\/span> List<span style=\"color: #333333;\">&lt;<\/span>Student<span style=\"color: #333333;\">&gt;<\/span> students<span style=\"color: #333333;\">;<\/span>\n\t\n\t<span style=\"color: #888888;\">\/\/constructor<\/span>\n\t<span style=\"color: #888888;\">\/\/getters and setter<\/span>\n<span style=\"color: #333333;\">}<\/span>\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Also note that in the Department class, we have added a private variable, students. Also notice that this variable is a List&lt;Student&gt; type. This means that we can ask the department to give us a list of students in it!. Isn&#8217;t this amazing!<\/p>\n<p>The H2 Console window is shown below;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1008  aligncenter\" src=\"https:\/\/www.kindsonthegenius.com\/wp-content\/uploads\/2020\/09\/H2-Console-Window-for-Relationships.jpg\" alt=\"H2 Console Window for Relationships\" width=\"826\" height=\"435\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Extra Table<\/strong><\/p>\n<p>Now, if you just add the @OneToMany and @ManyToOne annotations, two things would happen:<\/p>\n<ul>\n<li>the Student table would be created and additional field would be added called Department_Id<\/li>\n<li>Department table would be created together with another table( third table) called Department_Students. This is a way the Departments class tries to handle the mapping.<\/li>\n<\/ul>\n<p>Somehow, we need a way\u00a0 to tell the Department class that the mapping is handled by the Student class. And therefore it would not be necessary to have a third table.<\/p>\n<p>To do that, we used the mappedBy attribute of the @OneToMany annotation in the Deparment class. The value is set to the field in the Student table that has the @ManyToOne annotation. In our case, this field is the department field.<\/p>\n<p>&nbsp;<\/p>\n<p>I recommend you watch the video as well.<\/p>\n<p><iframe loading=\"lazy\" src=\"https:\/\/www.youtube.com\/embed\/VuE7lq9sxSw\" width=\"100%\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><span data-mce-type=\"bookmark\" style=\"display: inline-block; width: 0px; overflow: hidden; line-height: 0;\" class=\"mce_SELRES_start\">\ufeff<\/span><\/iframe><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this lesson, I would explain to you how relationships works in hibernate. Then I would give you a practical as well. We would be &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-1934","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1934","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=1934"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1934\/revisions"}],"predecessor-version":[{"id":2102,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1934\/revisions\/2102"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=1934"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=1934"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=1934"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}