Implementing Inheritance in Hibernate (Single Table Strategy, With Table Per Class Strategy, With Joined Strategy)

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on">&NewLine;<div style&equals;"font-family&colon; Times&comma;&quot&semi;Times New Roman&quot&semi;&comma;serif&semi;">This tutorial discusses what are the types of inheritance models in Hibernate and describes how they work like vertical inheritance and horizontal&period;<&sol;div>&NewLine;<p>There are three types of inheritance mapping in hibernate <br &sol;>&NewLine;<b>1&period; Table per concrete class with unions <&sol;b><br &sol;>&NewLine;<b>2&period; Table per class hierarchy&lpar;<&sol;b>Single Table Strategy<b>&rpar; <&sol;b><br &sol;>&NewLine;<b>3&period; Table per subclass <&sol;b><&sol;p>&NewLine;<div align&equals;'center' id&equals;"ads-id"><&sol;div>&NewLine;<p>Example&colon; <br &sol;>&NewLine;Let us take the simple example of 3 java classes&period; <br &sol;>&NewLine;Class <b>TwoWheelerVehicle <&sol;b>and <b>FourWheelerVehicle <&sol;b>are inherited from <b>Vehicle <&sol;b>Abstract class&period;<&sol;p>&NewLine;<div style&equals;"font-family&colon; Times&comma;&quot&semi;Times New Roman&quot&semi;&comma;serif&semi;">&NewLine;1&period; Table per concrete class with unions <br &sol;>&NewLine;In this case there will be 2 tables <br &sol;>&NewLine;Tables&colon; <b>TwoWheelerVehicle<&sol;b>&comma;<b> FourWheelerVehicle<&sol;b>&lbrack;all common attributes will be duplicated&rsqb; <&sol;p>&NewLine;<p>2&period; Table per class hierarchy <br &sol;>&NewLine;Single Table can be mapped to a class hierarchy <br &sol;>&NewLine;There will be only one table in database called &&num;8216&semi;<b>Vehicle<&sol;b>&&num;8216&semi; that will represent all the attributes required for all 3 classes&period; <br &sol;>&NewLine;But it needs some <b>discriminating <&sol;b>column to differentiate between <b>TwoWheelerVehicle <&sol;b>and&nbsp&semi;<b> FourWheelerVehicle<&sol;b>&semi;<&sol;p>&NewLine;<p>3&period; Table per subclass <br &sol;>&NewLine;In this case there will be 3 tables represent <b>TwoWheelerVehicle <&sol;b>&comma; <b> FourWheelerVehicle <&sol;b>and <b>Vehicle<&sol;b><&sol;p>&NewLine;<div style&equals;"background-color&colon; &num;f2f9fc&semi; border&colon;1px solid &num;c9e6f2&semi;border-radius&colon;3px&semi;padding&colon;16px&semi;line-height&colon;1&period;45&semi;"><b>Popular Tutorials<&sol;b><&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-tutorial&sol;"><em><strong>Spring Tutorial<&sol;strong> <&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-web-mvc-framework-chapter-38&sol;"><strong><em>Spring MVC Web Tutorial <&sol;em><&sol;strong><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;introduction-to-spring-boot-a-spring-boot-complete-guide&sol;"><em><strong>Spring Boot Tutorial<&sol;strong> <&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-security-take-baby-step-to-secure&sol;"><em>Spring Security Tutorial<&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint&sol;"><em>Spring AOP Tutorial<&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;using-spring-jdbc-framework-chapter-32&sol;"><em>Spring JDBC Tutorial<&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-hateoas-hypermedia-driven-restful-web-service&sol;"><em><strong>Spring HATEOAS <&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;microservices-with-spring-boot&sol;"><em><strong>Microservices with Spring Boot<&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;jax-rs-web-service-tutorial&sol;"><strong><em>REST Webservice<&sol;em> <&sol;strong><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;core-java-baby-step-to-be-best-java-ian&sol;"><em><strong>Core Java <&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hibernate-3-on-baby-steps&sol;"><em><strong>Hibernate Tutorial<&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-batch-process-with-example&sol;"><strong><em>Spring Batch<&sol;em> <&sol;strong><&sol;a><&sol;b><&sol;li>&NewLine;<&sol;ul>&NewLine;<&sol;div>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"https&colon;&sol;&sol;i1&period;wp&period;com&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2017&sol;04&sol;inheritance&period;jpg"&sol;><&sol;div>&NewLine;<p>&NewLine;Inheritance is one of the most visible facets of Object-relational mismatch&period; Object oriented systems can model both &OpenCurlyDoubleQuote;is a” and &OpenCurlyDoubleQuote;has a” relationship&period; Relational model supports only &OpenCurlyDoubleQuote;has a” relationship between two entities&period; Hibernate can help you map such Objects with relational tables&period; But you need to choose certain mapping strategy based on your needs&period; There are three possible strategies to use&period;<&sol;p>&NewLine;<ol style&equals;"text-align&colon; left&semi;">&NewLine;<li>&NewLine;<h2>Single Table Strategy&comma;<&sol;h2>&NewLine;<&sol;li>&NewLine;<li>&NewLine;<h2>With Table Per Class Strategy&comma;<&sol;h2>&NewLine;<&sol;li>&NewLine;<li>&NewLine;<h2>With Joined Strategy&nbsp&semi; <&sol;h2>&NewLine;<&sol;li>&NewLine;<&sol;ol>&NewLine;<&sol;div>&NewLine;<&sol;div>&NewLine;<h2>Single Table Strategy<&sol;h2>&NewLine;<p>In Single table per subclass&comma; the union of all the properties from the inheritance hierarchy is mapped to one table&period; As all the data goes in one table&comma; a discriminator is used to differentiate between different type of data&period;<br &sol;>&NewLine;<b>Advantages of Single Table per class hierarchy<&sol;b><&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li>Simplest to implement&period;<&sol;li>&NewLine;<li>Only one table to deal with&period;<&sol;li>&NewLine;<li>Performance wise better than all strategies because no joins or sub-selects need to be performed&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>&NewLine;<b>Disadvantages&colon;<&sol;b><&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li>Most of the column of table are nullable so the NOT NULL constraint cannot be applied&period;<&sol;li>&NewLine;<li>Tables are not normalized&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>Lets see the following example code&period;<br &sol;>&NewLine;<b>Vehicle&period;java<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">package com&period;sdnext&period;hibernate&period;tutorial&period;dto&semi; &NewLine; &NewLine;import javax&period;persistence&period;Column&semi; &NewLine;import javax&period;persistence&period;Entity&semi; &NewLine;import javax&period;persistence&period;GeneratedValue&semi; &NewLine;import javax&period;persistence&period;GenerationType&semi; &NewLine;import javax&period;persistence&period;Id&semi; &NewLine;import javax&period;persistence&period;Inheritance&semi; &NewLine;import javax&period;persistence&period;InheritanceType&semi; &NewLine;import javax&period;persistence&period;Table&semi; &NewLine; &NewLine;&commat;Entity &NewLine;&commat;Table&lpar;name&equals;"VEHICLE"&rpar; &NewLine;&commat;Inheritance&lpar;strategy&equals;InheritanceType&period;SINGLE&lowbar;TABLE&rpar; &sol;&sol;Least normalisation strategy &NewLine;public class Vehicle &NewLine;&lbrace; &NewLine; &commat;Id &NewLine; &commat;GeneratedValue&lpar;strategy&equals;GenerationType&period;AUTO&rpar; &NewLine; &commat;Column&lpar;name&equals;"VEHICLE&lowbar;ID"&rpar; &NewLine; private int vehicleId&semi; &NewLine; &NewLine; &commat;Column&lpar;name&equals;"VEHICLE&lowbar;NAME"&rpar; &NewLine; private String vehicleName&semi; &NewLine; &NewLine; public int getVehicleId&lpar;&rpar; &lbrace; &NewLine; return vehicleId&semi; &NewLine; &rcub; &NewLine; public void setVehicleId&lpar;int vehicleId&rpar; &lbrace; &NewLine; this&period;vehicleId &equals; vehicleId&semi; &NewLine; &rcub; &NewLine; public String getVehicleName&lpar;&rpar; &lbrace; &NewLine; return vehicleName&semi; &NewLine; &rcub; &NewLine; public void setVehicleName&lpar;String vehicleName&rpar; &lbrace; &NewLine; this&period;vehicleName &equals; vehicleName&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine; &NewLine;<&sol;pre>&NewLine;<&sol;div>&NewLine;<p><b>TwoWheeler&period;java<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">package com&period;sdnext&period;hibernate&period;tutorial&period;dto&semi; &NewLine; &NewLine;import javax&period;persistence&period;Column&semi; &NewLine;import javax&period;persistence&period;Entity&semi; &NewLine;import javax&period;persistence&period;Table&semi; &NewLine; &NewLine;&commat;Entity &NewLine;&commat;Table&lpar;name&equals;"TWO&lowbar;WHEELER"&rpar; &NewLine;&sol;&sol;&commat;DiscriminatorValue&lpar;"Bike"&rpar; &NewLine;public class TwoWheeler extends Vehicle &NewLine;&lbrace; &NewLine; &commat;Column&lpar;name&equals;"STEERING&lowbar;TYPE"&rpar; &NewLine; private String steeringTwoWheeler&semi; &NewLine; &NewLine; public String getSteeringTwoWheeler&lpar;&rpar; &NewLine; &lbrace; &NewLine; return steeringTwoWheeler&semi; &NewLine; &rcub; &NewLine; &NewLine; public void setSteeringTwoWheeler&lpar;String steeringTwoWheeler&rpar; &NewLine; &lbrace; &NewLine; this&period;steeringTwoWheeler &equals; steeringTwoWheeler&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine; &NewLine;<&sol;pre>&NewLine;<&sol;div>&NewLine;<p><b>FourWheeler&period;java<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">package com&period;sdnext&period;hibernate&period;tutorial&period;dto&semi; &NewLine; &NewLine;import javax&period;persistence&period;Column&semi; &NewLine;import javax&period;persistence&period;Entity&semi; &NewLine;import javax&period;persistence&period;Table&semi; &NewLine; &NewLine;&commat;Entity &NewLine;&commat;Table&lpar;name&equals;"FOUR&lowbar;WHEELER"&rpar; &NewLine;&sol;&sol;&commat;DiscriminatorValue&lpar;"Car"&rpar; &NewLine;public class FourWheeler extends Vehicle &NewLine;&lbrace; &NewLine; &commat;Column&lpar;name&equals;"STEERING&lowbar;TYPE"&rpar; &NewLine; private String steeringFourWheeler&semi; &NewLine; &NewLine; public String getSteeringFourWheeler&lpar;&rpar; &NewLine; &lbrace; &NewLine; return steeringFourWheeler&semi; &NewLine; &rcub; &NewLine; &NewLine; public void setSteeringFourWheeler&lpar;String steeringFourWheeler&rpar; &NewLine; &lbrace; &NewLine; this&period;steeringFourWheeler &equals; steeringFourWheeler&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p>&NewLine;<b>hibernate&period;cfg&period;xml<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">&lt&semi;hibernate-configuration&gt&semi; &NewLine; &lt&semi;session-factory&gt&semi; &NewLine; &lt&semi;&excl;-- Database connection settings --&gt&semi; &NewLine; &lt&semi;property name&equals;"connection&period;driver&lowbar;class"&gt&semi;com&period;mysql&period;jdbc&period;Driver&lt&semi;&sol;property&gt&semi; &NewLine; &lt&semi;property name&equals;"connection&period;url"&gt&semi;jdbc&colon;mysql&colon;&sol;&sol;localhost&colon;3306&sol;vehicleDB2&lt&semi;&sol;property&gt&semi; &NewLine; &lt&semi;property name&equals;"connection&period;username"&gt&semi;root&lt&semi;&sol;property&gt&semi; &NewLine; &lt&semi;property name&equals;"connection&period;password"&gt&semi;root&lt&semi;&sol;property&gt&semi; &NewLine; &NewLine; &lt&semi;&excl;-- JDBC connection pool &lpar;use the built-in&rpar; --&gt&semi; &NewLine; &lt&semi;property name&equals;"connection&period;pool&lowbar;size"&gt&semi;1&lt&semi;&sol;property&gt&semi; &NewLine; &NewLine; &lt&semi;&excl;-- SQL dialect --&gt&semi; &NewLine; &lt&semi;property name&equals;"dialect"&gt&semi;org&period;hibernate&period;dialect&period;MySQLDialect&lt&semi;&sol;property&gt&semi; &NewLine; &NewLine; &lt&semi;&excl;-- Enable Hibernate's automatic session context management --&gt&semi; &NewLine; &lt&semi;property name&equals;"current&lowbar;session&lowbar;context&lowbar;class"&gt&semi;thread&lt&semi;&sol;property&gt&semi; &NewLine; &NewLine; &lt&semi;&excl;-- Disable the second-level cache --&gt&semi; &NewLine; &lt&semi;property name&equals;"cache&period;provider&lowbar;class"&gt&semi;org&period;hibernate&period;cache&period;NoCacheProvider&lt&semi;&sol;property&gt&semi; &NewLine; &NewLine; &lt&semi;&excl;-- Echo all executed SQL to stdout --&gt&semi; &NewLine; &lt&semi;property name&equals;"show&lowbar;sql"&gt&semi;true&lt&semi;&sol;property&gt&semi; &NewLine; &NewLine; &lt&semi;&excl;-- Drop and re-create the database schema on startup --&gt&semi; &NewLine; &lt&semi;property name&equals;"hbm2ddl&period;auto"&gt&semi;create&lt&semi;&sol;property&gt&semi; &NewLine; &NewLine; &NewLine; &lt&semi;mapping class&equals;"com&period;sdnext&period;hibernate&period;tutorial&period;dto&period;Vehicle"&gt&semi; &NewLine; &lt&semi;mapping class&equals;"com&period;sdnext&period;hibernate&period;tutorial&period;dto&period;TwoWheeler"&gt&semi; &NewLine; &lt&semi;mapping class&equals;"com&period;sdnext&period;hibernate&period;tutorial&period;dto&period;FourWheeler"&gt&semi; &NewLine; &NewLine; &lt&semi;&sol;mapping&gt&semi;&lt&semi;&sol;mapping&gt&semi;&lt&semi;&sol;mapping&gt&semi;&lt&semi;&sol;session-factory&gt&semi; &NewLine; &lt&semi;&sol;hibernate-configuration&gt&semi; &NewLine;<&sol;pre>&NewLine;<&sol;div>&NewLine;<p>Now run the following test class <br &sol;>&NewLine;<b>HibernateTestDemo&period;java<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">package com&period;sdnext&period;hibernate&period;tutorial&semi; &NewLine; &NewLine;import org&period;hibernate&period;Session&semi; &NewLine;import org&period;hibernate&period;SessionFactory&semi; &NewLine;import org&period;hibernate&period;cfg&period;AnnotationConfiguration&semi; &NewLine; &NewLine;import com&period;sdnext&period;hibernate&period;tutorial&period;dto&period;FourWheeler&semi; &NewLine;import com&period;sdnext&period;hibernate&period;tutorial&period;dto&period;TwoWheeler&semi; &NewLine;import com&period;sdnext&period;hibernate&period;tutorial&period;dto&period;Vehicle&semi; &NewLine; &NewLine;public class HibernateTestDemo &lbrace; &NewLine; &NewLine; &sol;&ast;&ast; &NewLine; &ast; &commat;param args &NewLine; &ast;&sol; &NewLine; public static void main&lpar;String&lbrack;&rsqb; args&rpar; &NewLine; &lbrace; &NewLine; SessionFactory sessionFactory &equals; new AnnotationConfiguration&lpar;&rpar;&period;configure&lpar;&rpar;&period;buildSessionFactory&lpar;&rpar;&semi; &NewLine; Session session &equals; sessionFactory&period;openSession&lpar;&rpar;&semi; &NewLine; session&period;beginTransaction&lpar;&rpar;&semi; &NewLine; &NewLine; Vehicle vehicle &equals; new Vehicle&lpar;&rpar;&semi; &NewLine; vehicle&period;setVehicleName&lpar;"Car"&rpar;&semi; &NewLine; &NewLine; TwoWheeler twoWheeler &equals; new TwoWheeler&lpar;&rpar;&semi; &NewLine; twoWheeler&period;setVehicleName&lpar;"Bike"&rpar;&semi; &NewLine; twoWheeler&period;setSteeringTwoWheeler&lpar;"Bike Steering Handle"&rpar;&semi; &NewLine; &NewLine; FourWheeler fourWheeler &equals; new FourWheeler&lpar;&rpar;&semi; &NewLine; fourWheeler&period;setVehicleName&lpar;"Alto"&rpar;&semi; &NewLine; fourWheeler&period;setSteeringFourWheeler&lpar;"Alto Steering Wheel"&rpar;&semi; &NewLine; &NewLine; session&period;save&lpar;vehicle&rpar;&semi; &NewLine; session&period;save&lpar;twoWheeler&rpar;&semi; &NewLine; session&period;save&lpar;fourWheeler&rpar;&semi; &NewLine; &NewLine; session&period;getTransaction&lpar;&rpar;&period;commit&lpar;&rpar;&semi; &NewLine; session&period;close&lpar;&rpar;&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<&sol;div>&NewLine;<p><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"https&colon;&sol;&sol;i1&period;wp&period;com&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2017&sol;04&sol;output&lowbar;inheritance&period;jpg" &sol;><&sol;div>&NewLine;<p>&NewLine;<&sol;div>&NewLine;<p>In the above table <b>Vehicle<&sol;b> there are four columns &lpar;DTYPE&comma; VEHICLE&lowbar;ID&comma; VEHICLE&lowbar;NAME&comma; STEERING&lowbar;TYPE&rpar;&period;<br &sol;>&NewLine;The first column has the value of discriminator type&lpar;DTYPE&rpar; is <b><b>Vehicle<&sol;b>&comma; TwoWheeler&comma; FourWheeler<&sol;b> as its entity name by default&period;<&sol;p>&NewLine;<p>For user convenience we can override the default value of column as well as column name by using the following annotation&period;<br &sol;>&NewLine;<b>&commat;DiscriminatorColumn<&sol;b><br &sol;>&NewLine;Target&colon;<br &sol;>&NewLine;&nbsp&semi;&nbsp&semi;Classes<&sol;p>&NewLine;<p>Specifies the discriminator column for the SINGLE&lowbar;TABLE and JOINED Inheritance mapping strategies&period;<br &sol;>&NewLine;The strategy and the discriminator column are only specified in the root of an entity class hierarchy or subhierarchy in which a different inheritance strategy is applied <br &sol;>&NewLine;If the DiscriminatorColumn annotation is missing&comma; and a discriminator column is required&comma; the name of the discriminator column defaults to &&num;8220&semi;DTYPE&&num;8221&semi; and the discriminator type to DiscriminatorType&period;STRING&period;<br &sol;>&NewLine;<b><br &sol;>&NewLine;&commat;DiscriminatorValue<&sol;b><br &sol;>&NewLine;Target&colon;<br &sol;>&NewLine;&nbsp&semi;&nbsp&semi;Classes<&sol;p>&NewLine;<p>Specifies the value of the discriminator column for entities of the given type&period;<&sol;p>&NewLine;<p>The DiscriminatorValue annotation can only be specified on a concrete entity class&period;<&sol;p>&NewLine;<p>If the DiscriminatorValue annotation is not specified and a discriminator column is used&comma; a provider-specific function will be used to generate a value representing the entity type&period; If the DiscriminatorType is STRING&comma; the discriminator value default is the entity name&period;<&sol;p>&NewLine;<p>The inheritance strategy and the discriminator column are only specified in the root of an entity class hierarchy or subhierarchy in which a different inheritance strategy is applied&period; The discriminator value&comma; if not defaulted&comma; should be specified for each entity class in the hierarchy&period;<br &sol;>&NewLine;<b><br &sol;>&NewLine;&commat;Inheritance<&sol;b><&sol;p>&NewLine;<p>Target&colon; <br &sol;>&NewLine;&nbsp&semi;&nbsp&semi;Classes<&sol;p>&NewLine;<p>Defines the inheritance strategy to be used for an entity class hierarchy&period; It is specified on the entity class that is the root of the entity class hierarchy&period; If the Inheritance annotation is not specified or if no inheritance type is specified for an entity class hierarchy&comma; the SINGLE&lowbar;TABLE mapping strategy is used&period;<&sol;p>&NewLine;<p>Now adding the following annotation to the Vehicle class is<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">&commat;Entity &NewLine;&commat;Table&lpar;name&equals;"VEHICLE"&rpar; &NewLine;&commat;Inheritance&lpar;strategy&equals;InheritanceType&period;SINGLE&lowbar;TABLE&rpar; &sol;&sol;Least normalisation strategy &NewLine;&commat;DiscriminatorColumn&lpar; &NewLine; name&equals;"VEHICLE&lowbar;TYPE"&comma; &NewLine; discriminatorType&equals;DiscriminatorType&period;STRING &NewLine; &rpar; &NewLine;public class Vehicle &NewLine;&lbrace; &NewLine;<&sol;pre>&NewLine;<p>Now adding following annotation to the TwoWheeler class<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">&commat;DiscriminatorValue&lpar;"Bike"&rpar; &NewLine;public class TwoWheeler extends Vehicle &NewLine;&lbrace; &NewLine;<&sol;pre>&NewLine;<p>Now adding following annotation to the FourWheeler class<&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">&commat;DiscriminatorValue&lpar;"Car"&rpar; &NewLine;public class FourWheeler extends Vehicle &NewLine;&lbrace; &NewLine;<&sol;pre>&NewLine;<p>After these above modification we run the code then we will get the following output&period;<&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"https&colon;&sol;&sol;i1&period;wp&period;com&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2017&sol;04&sol;out-inheritance&period;jpg" &sol;><&sol;div>&NewLine;<p>&NewLine;<&sol;div>&NewLine;<h2>With Table Per Class Strategy<&sol;h2>&NewLine;<p>In this case every entity class has its own table i&period;e&period; table per class&period; The data for Vehicle is duplicated in both the tables&period;<br &sol;>&NewLine;This strategy is not popular and also have been made optional in Java Persistence API&period;<br &sol;>&NewLine;Advantage&colon;<&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li>Possible to define NOT NULL constraints on the table&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>&NewLine;Disadvantage&colon;<&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li>Tables are not normalized&period;<&sol;li>&NewLine;<li>To support polymorphism either container has to do multiple trips to database or use SQL UNION kind of feature&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><b>In this case there no need for the discriminator column because all entity has own table&period;<&sol;b><br &sol;>&NewLine;The Vehicle entity in this case is<br &sol;>&NewLine;<b>Vehicle&period;java<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">package com&period;sdnext&period;hibernate&period;tutorial&period;dto&semi; &NewLine; &NewLine;import javax&period;persistence&period;Column&semi; &NewLine;import javax&period;persistence&period;DiscriminatorColumn&semi; &NewLine;import javax&period;persistence&period;DiscriminatorType&semi; &NewLine;import javax&period;persistence&period;Entity&semi; &NewLine;import javax&period;persistence&period;GeneratedValue&semi; &NewLine;import javax&period;persistence&period;GenerationType&semi; &NewLine;import javax&period;persistence&period;Id&semi; &NewLine;import javax&period;persistence&period;Inheritance&semi; &NewLine;import javax&period;persistence&period;InheritanceType&semi; &NewLine;import javax&period;persistence&period;Table&semi; &NewLine; &NewLine;&commat;Entity &NewLine;&commat;Table&lpar;name&equals;"VEHICLE"&rpar; &NewLine;&commat;Inheritance&lpar;strategy&equals;InheritanceType&period;TABLE&lowbar;PER&lowbar;CLASS&rpar; &sol;&sol;slightly more normalized &NewLine;public class Vehicle &NewLine;&lbrace; &NewLine; &commat;Id &NewLine; &commat;GeneratedValue&lpar;strategy&equals;GenerationType&period;AUTO&rpar; &NewLine; &commat;Column&lpar;name&equals;"VEHICLE&lowbar;ID"&rpar; &NewLine; private int vehicleId&semi; &NewLine; &NewLine; &commat;Column&lpar;name&equals;"VEHICLE&lowbar;NAME"&rpar; &NewLine; private String vehicleName&semi; &NewLine; &NewLine; public int getVehicleId&lpar;&rpar; &lbrace; &NewLine; return vehicleId&semi; &NewLine; &rcub; &NewLine; public void setVehicleId&lpar;int vehicleId&rpar; &lbrace; &NewLine; this&period;vehicleId &equals; vehicleId&semi; &NewLine; &rcub; &NewLine; public String getVehicleName&lpar;&rpar; &lbrace; &NewLine; return vehicleName&semi; &NewLine; &rcub; &NewLine; public void setVehicleName&lpar;String vehicleName&rpar; &lbrace; &NewLine; this&period;vehicleName &equals; vehicleName&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p>And there no need to the discriminator value for the TwoWheeler and FourWheeler Entity so in this case the <br &sol;>&NewLine;<b>TwoWheeler&period;java<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">package com&period;sdnext&period;hibernate&period;tutorial&period;dto&semi; &NewLine; &NewLine;import javax&period;persistence&period;Column&semi; &NewLine;import javax&period;persistence&period;DiscriminatorValue&semi; &NewLine;import javax&period;persistence&period;Entity&semi; &NewLine;import javax&period;persistence&period;Table&semi; &NewLine; &NewLine;&commat;Entity &NewLine;&commat;Table&lpar;name&equals;"TWO&lowbar;WHEELER"&rpar; &NewLine;public class TwoWheeler extends Vehicle &NewLine;&lbrace; &NewLine; &commat;Column&lpar;name&equals;"STEERING&lowbar;TYPE"&rpar; &NewLine; private String steeringTwoWheeler&semi; &NewLine; &NewLine; public String getSteeringTwoWheeler&lpar;&rpar; &NewLine; &lbrace; &NewLine; return steeringTwoWheeler&semi; &NewLine; &rcub; &NewLine; &NewLine; public void setSteeringTwoWheeler&lpar;String steeringTwoWheeler&rpar; &NewLine; &lbrace; &NewLine; this&period;steeringTwoWheeler &equals; steeringTwoWheeler&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p><b>FourWheeler&period;java<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">package com&period;sdnext&period;hibernate&period;tutorial&period;dto&semi; &NewLine; &NewLine;import javax&period;persistence&period;Column&semi; &NewLine;import javax&period;persistence&period;DiscriminatorValue&semi; &NewLine;import javax&period;persistence&period;Entity&semi; &NewLine;import javax&period;persistence&period;Table&semi; &NewLine; &NewLine;&commat;Entity &NewLine;&commat;Table&lpar;name&equals;"FOUR&lowbar;WHEELER"&rpar; &NewLine;public class FourWheeler extends Vehicle &NewLine;&lbrace; &NewLine; &commat;Column&lpar;name&equals;"STEERING&lowbar;TYPE"&rpar; &NewLine; private String steeringFourWheeler&semi; &NewLine; &NewLine; public String getSteeringFourWheeler&lpar;&rpar; &NewLine; &lbrace; &NewLine; return steeringFourWheeler&semi; &NewLine; &rcub; &NewLine; &NewLine; public void setSteeringFourWheeler&lpar;String steeringFourWheeler&rpar; &NewLine; &lbrace; &NewLine; this&period;steeringFourWheeler &equals; steeringFourWheeler&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"https&colon;&sol;&sol;i1&period;wp&period;com&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2017&sol;04&sol;table-per-class&period;jpg" &sol;><&sol;div>&NewLine;<p>&NewLine;<&sol;div>&NewLine;<p><&sol;p>&NewLine;<h2>With Joined Strategy <&sol;h2>&NewLine;<p>It&&num;8217&semi;s highly normalized but performance is not good&period;<br &sol;>&NewLine;Advantage&colon;<&sol;p>&NewLine;<ul>&NewLine;<li> Tables are normalized&period;<&sol;li>&NewLine;<li> Able to define NOT NULL constraint&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>Disadvantage&colon;<&sol;p>&NewLine;<ul>&NewLine;<li> Does not perform as well as SINGLE&lowbar;TABLE strategy<&sol;li>&NewLine;<&sol;ul>&NewLine;<&sol;div>&NewLine;<p>Using Join Strategy with the vehicle entity<br &sol;>&NewLine;<b>Vehicle&period;java<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">package com&period;sdnext&period;hibernate&period;tutorial&period;dto&semi; &NewLine; &NewLine;import javax&period;persistence&period;Column&semi; &NewLine;import javax&period;persistence&period;DiscriminatorColumn&semi; &NewLine;import javax&period;persistence&period;DiscriminatorType&semi; &NewLine;import javax&period;persistence&period;Entity&semi; &NewLine;import javax&period;persistence&period;GeneratedValue&semi; &NewLine;import javax&period;persistence&period;GenerationType&semi; &NewLine;import javax&period;persistence&period;Id&semi; &NewLine;import javax&period;persistence&period;Inheritance&semi; &NewLine;import javax&period;persistence&period;InheritanceType&semi; &NewLine;import javax&period;persistence&period;Table&semi; &NewLine; &NewLine;&commat;Entity &NewLine;&commat;Table&lpar;name&equals;"VEHICLE"&rpar; &NewLine;&commat;Inheritance&lpar;strategy&equals;InheritanceType&period;JOINED&rpar;&sol;&sol;Highly normalized &NewLine;public class Vehicle &NewLine;&lbrace; &NewLine; &commat;Id &NewLine; &commat;GeneratedValue &NewLine; &commat;Column&lpar;name&equals;"VEHICLE&lowbar;ID"&rpar; &NewLine; private int vehicleId&semi; &NewLine; &NewLine; &commat;Column&lpar;name&equals;"VEHICLE&lowbar;NAME"&rpar; &NewLine; private String vehicleName&semi; &NewLine; &NewLine; public int getVehicleId&lpar;&rpar; &lbrace; &NewLine; return vehicleId&semi; &NewLine; &rcub; &NewLine; public void setVehicleId&lpar;int vehicleId&rpar; &lbrace; &NewLine; this&period;vehicleId &equals; vehicleId&semi; &NewLine; &rcub; &NewLine; public String getVehicleName&lpar;&rpar; &lbrace; &NewLine; return vehicleName&semi; &NewLine; &rcub; &NewLine; public void setVehicleName&lpar;String vehicleName&rpar; &lbrace; &NewLine; this&period;vehicleName &equals; vehicleName&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p>Now run the code we will get the following output&period;<&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"https&colon;&sol;&sol;i1&period;wp&period;com&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2017&sol;04&sol;join-inherit&period;jpg"&sol;><&sol;div>&NewLine;<&sol;div>&NewLine;<p>We have seen the three strategies about inheritance in the hibernate&period;<br &sol;>&NewLine;A comparison of three strategies is as follows&colon;<&sol;p>&NewLine;<table class&equals;"wikitable">&NewLine;<tbody>&NewLine;<tr>&NewLine;<td class&equals;"wikicell rtecenter"><b>Criteria<&sol;b><&sol;td>&NewLine;<td class&equals;"wikicell rtecenter"><b>Single Table<&sol;b><&sol;td>&NewLine;<td class&equals;"wikicell rtecenter"><b>Table per subclass&lpar;Join Strategy&rpar;<&sol;b><&sol;td>&NewLine;<td class&equals;"wikicell rtecenter"><b>Table per Class<&sol;b><&sol;td>&NewLine;<&sol;tr>&NewLine;<tr>&NewLine;<td class&equals;"wikicell rtecenter"><b>Table Support<&sol;b><&sol;td>&NewLine;<td class&equals;"wikicell rtecenter">&NewLine;<ul>&NewLine;<li class&equals;"rteleft"> Data not normalized&period;<&sol;li>&NewLine;<li class&equals;"rteleft"> Constraint for mandatory columns to be not nullable cannot applied&period;<&sol;li>&NewLine;<li class&equals;"rteleft"> Change in any subclass leads to change in structure of Table<&sol;li>&NewLine;<&sol;ul>&NewLine;<&sol;td>&NewLine;<td class&equals;"wikicell">&NewLine;<ul>&NewLine;<li> Normalized&period;<&sol;li>&NewLine;<li> Mandatory column constraint can be applied<&sol;li>&NewLine;<&sol;ul>&NewLine;<&sol;td>&NewLine;<td class&equals;"wikicell">&NewLine;<ul>&NewLine;<li> One table for each concrete class&period;<&sol;li>&NewLine;<li> Not maintainable&period;<&sol;li>&NewLine;<li> Change in base class leads to changes in all tables of derived class<&sol;li>&NewLine;<&sol;ul>&NewLine;<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr>&NewLine;<td class&equals;"wikicell rtecenter"><b>Discriminator Column<&sol;b><&sol;td>&NewLine;<td class&equals;"wikicell rtecenter">Present<&sol;td>&NewLine;<td class&equals;"wikicell rtecenter">Absent<&sol;td>&NewLine;<td class&equals;"wikicell rtecenter">Absent<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr>&NewLine;<td class&equals;"wikicell rtecenter"><b>Retrieving data<&sol;b><&sol;td>&NewLine;<td class&equals;"wikicell">simple SELECT&period; All data is in one table&period; Using discriminator type&comma; individual types can be selected<&sol;td>&NewLine;<td class&equals;"wikicell">Joins among table&period; For example fetching FourWheeler will require a join on FourWheeler and Vehicle table&period; If all user needs to be fetched than it will put a join for all three tables<&sol;td>&NewLine;<td class&equals;"wikicell">Separate Select or Union Select<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr>&NewLine;<td class&equals;"wikicell rtecenter"><b>Updating and Inserting<&sol;b><&sol;td>&NewLine;<td class&equals;"wikicell">Single INSERT or UPDATE<&sol;td>&NewLine;<td class&equals;"wikicell">Multiple&period; For Vehicle type one insert on Vehicle table&period; For FourWheeler type one insert on Vehicle table and another on FourWheeler table&period;<&sol;td>&NewLine;<td class&equals;"wikicell">One insert or update for each subclass<&sol;td>&NewLine;<&sol;tr>&NewLine;<tr>&NewLine;<td class&equals;"wikicell rtecenter"><b>JPA Support<&sol;b><&sol;td>&NewLine;<td class&equals;"wikicell rtecenter">Mandatory<&sol;td>&NewLine;<td class&equals;"wikicell rtecenter">Optional <&sol;td>&NewLine;<&sol;tr>&NewLine;<&sol;tbody>&NewLine;<&sol;table>&NewLine;<&sol;div>&NewLine;<p>&NewLine;<b>In the <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;p&sol;crud-operations-using-hibernate-3&period;html">Next Chapter<&sol;a> we will learn about the <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;p&sol;crud-operations-using-hibernate-3&period;html">CRUD Operation <&sol;a>using Hibernate&period;<&sol;b><br &sol;>&NewLine;<b><br &sol;>&NewLine;<&sol;b> <b>&nbsp&semi;&nbsp&semi;&nbsp&semi;&nbsp&semi;&nbsp&semi;&nbsp&semi;&nbsp&semi;&nbsp&semi;&nbsp&semi;&nbsp&semi;&nbsp&semi;&nbsp&semi;&nbsp&semi;&nbsp&semi;&nbsp&semi;&nbsp&semi;&nbsp&semi;&nbsp&semi;&lt&semi;&lt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;p&sol;cascadetypes-and-other-things&period;html" style&equals;"font-family&colon; Times&comma; 'Times New Roman'&comma; serif&semi;">Previous Chapter 21<&sol;a>&lt&semi;&lt&semi; &nbsp&semi;&nbsp&semi; &gt&semi;&gt&semi;N<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;p&sol;crud-operations-using-hibernate-3&period;html" style&equals;"font-family&colon; Times&comma; 'Times New Roman'&comma; serif&semi;">ext Chapter23&gt&semi;&gt&semi;<&sol;a><&sol;b><&sol;p>&NewLine;<&sol;div>&NewLine;

View Comments

  • Bro could you please tell me the process how can we retrieve entity based on discriminator value,i know this approach is not industry standard but still want to know.

  • "Class TwoWheelerVehicle and FourWheelerVehicle are inherited from Vehicle Abstract class."
    I think it isn't really abstract, is it?

  • Hi,

    I was reading the following link (http://www.baeldung.com/hibernate-inheritance). I could not understand what happens in the retrieve and insertion of records in the different cases. Can you kindly write a blog post or explain it below with respect to the entities in the given link.

    Best regards
    Rajasekhar

  • In the Table per class strategy only child tables will be created not the parent. Hence we will see only two tables getting created in database i.e. TWO_WHEELER and FOUR_WHEELER.