{"id":1299,"date":"2016-06-22T11:34:03","date_gmt":"2016-06-22T11:34:03","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=1299"},"modified":"2016-06-23T05:25:03","modified_gmt":"2016-06-23T05:25:03","slug":"hibernate-inheritance","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/hibernate-inheritance\/","title":{"rendered":"Hibernate Inheritance"},"content":{"rendered":"<p><strong>Image Showing Values in Table with and Without DiscriminatorColumn defined<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/codethataint.com\/blog\/wp-content\/uploads\/2016\/06\/InheritanceSingleTable.png\" alt=\"\" width=\"704\" height=\"507\"\/><\/p>\n<ul>\n<li>By Default Hibernate follows Single Table Inheritance<\/li>\n<li>DiscriminatorColumn tells hibernate the name in which DiscriminatorColumn should be save or else it would be saved as DType<\/li>\n<\/ul>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@Inheritance(strategy=InheritanceType.SINGLE_TABLE)\r\n@DiscriminatorColumn(name=&quot;VEHICLE_TYPE&quot;,\r\n\t\t     discriminatorType=DiscriminatorType.STRING)\r\n<\/pre>\n<p><strong>Vehicles.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@Entity\r\n@Inheritance(strategy=InheritanceType.SINGLE_TABLE)\r\n@DiscriminatorColumn(name=&quot;VEHICLE_TYPE&quot;,\r\n\t\t     discriminatorType=DiscriminatorType.STRING)\r\npublic class Vehicles {\r\n\t@Id @GeneratedValue(strategy=GenerationType.IDENTITY)\r\n\tprivate int VehicleId;\t\r\n\tprivate String name;\r\n\t\t\r\n\tpublic int getVehicleId() {\r\n\t\treturn VehicleId;\r\n\t}\r\n\tpublic void setVehicleId(int vehicleId) {\r\n\t\tVehicleId = vehicleId;\r\n\t}\r\n\tpublic String getName() {\r\n\t\treturn name;\r\n\t}\r\n\tpublic void setName(String name) {\r\n\t\tthis.name = name;\r\n\t}\t\r\n}\r\n<\/pre>\n<p><strong>TwoWheelers.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@Entity\r\n@DiscriminatorValue(&quot;Bike&quot;)\r\npublic class TwoWheelers extends Vehicles{\r\n\tprivate String steeringHolder;\r\n\r\n\tpublic String getSteeringHolder() {\r\n\t\treturn steeringHolder;\r\n\t}\r\n\r\n\tpublic void setSteeringHolder(String steeringHolder) {\r\n\t\tthis.steeringHolder = steeringHolder;\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>FourWheelers.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@Entity\r\n@DiscriminatorValue(&quot;Car&quot;)\r\npublic class FourWheelers extends Vehicles{\r\n\t\r\n\tprivate String steeringWheel;\r\n\r\n\tpublic String getSteeringWheel() {\r\n\t\treturn steeringWheel;\r\n\t}\r\n\r\n\tpublic void setSteeringWheel(String steeringWheel) {\r\n\t\tthis.steeringWheel = steeringWheel;\r\n\t}\r\n\t\r\n}\r\n<\/pre>\n<p>By Using the below annotation individual tables would be created for all the subclasses instead of placing all the values getting placed in a single class.<\/p>\n<p><strong>InheritanceType.TABLE_PER_CLASS<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/codethataint.com\/blog\/wp-content\/uploads\/2016\/06\/TablePerClass.png\" alt=\"\" height=\"592\" width=\"418\"\/><\/p>\n<p><strong>InheritanceType.JOINED<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@Inheritance(strategy=InheritanceType.JOINED)\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/codethataint.com\/blog\/wp-content\/uploads\/2016\/06\/JoinedInhertance.png\" alt=\"\" height=\"568\" width=\"541\"\/><\/p>\n<p><strong>Using InheritanceType.JOINED gets more normalized table compared to InheritanceType.TABLE_PER_CLASS<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Image Showing Values in Table with and Without DiscriminatorColumn defined By Default Hibernate follows Single Table Inheritance DiscriminatorColumn tells hibernate the name in which DiscriminatorColumn should be save or else it would be saved as DType @Inheritance(strategy=InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name=&quot;VEHICLE_TYPE&quot;, discriminatorType=DiscriminatorType.STRING) Vehicles.java @Entity @Inheritance(strategy=InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name=&quot;VEHICLE_TYPE&quot;, discriminatorType=DiscriminatorType.STRING) public class Vehicles { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int VehicleId; private String&hellip; <a href=\"https:\/\/codethataint.com\/blog\/hibernate-inheritance\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[74],"tags":[],"class_list":["post-1299","post","type-post","status-publish","format-standard","hentry","category-hibernate"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1299","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=1299"}],"version-history":[{"count":8,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1299\/revisions"}],"predecessor-version":[{"id":1311,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1299\/revisions\/1311"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=1299"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=1299"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=1299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}