Hibernate Table Per Hierarchy using xml file

By this inheritance strategy, we can map the whole hierarchy by single table only. Here, an extra column (also known as discriminator column) is created in the table to identify the class.
Let's understand the problem first. I want to map the whole hierarchy given below into one table of the database.
table per class hierarchy in inhertance mapping There are three classes in this hierarchy. Employee is the super class for Regular_Employee and Contract_Employee classes. Let's see the mapping file for this hierarchy.
  1. <?xml version='1.0' encoding='UTF-8'?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC  
  3.           "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4.           "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  5.   
  6. <hibernate-mapping>  
  7. <class name="com.javatpoint.mypackage.Employee" table="emp121" discriminator-value="emp">  
  8. <id name="id">  
  9. <generator class="increment"></generator>  
  10. </id>  
  11.   
  12. <discriminator column="type" type="string"></discriminator>  
  13. <property name="name"></property>  
  14.             
  15. <subclass name="com.javatpoint.mypackage.Regular_Employee" discriminator-value="reg_emp">  
  16. <property name="salary"></property>  
  17. <property name="bonus"></property>  
  18. </subclass>  
  19.             
  20. <subclass name="com.javatpoint.mypackage.Contract_Employee" discriminator-value="con_emp">  
  21. <property name="pay_per_hour"></property>  
  22. <property name="contract_duration"></property>  
  23. </subclass>  
  24.             
  25. </class>  
  26.             
  27. </hibernate-mapping>  
In case of table per class hierarchy an discriminator column is added by the hibernate framework that specifies the type of the record. It is mainly used to distinguish the record. To specify this, discriminator subelement of class must be specified.
The subclass subelement of class, specifies the subclass. In this case, Regular_Employee and Contract_Employee are the subclasses of Employee class.

The table structure for this hierarchy is as shown below:
table per class hierarchy table

Example of Table per class hierarchy

In this example we are creating the three classes and provide mapping of these classes in the employee.hbm.xml file.

1) Create the Persistent classes

You need to create the persistent classes representing the inheritance. Let's create the three classes for the above hierarchy:
File: Employee.java
  1. package com.javatpoint.mypackage;  
  2.   
  3. public class Employee {  
  4. private int id;  
  5. private String name;  
  6.   
  7. //getters and setters  
  8. }  
File: Regular_Employee.java
  1. package com.javatpoint.mypackage;  
  2.   
  3. public class Regular_Employee extends Employee{  
  4. private float salary;  
  5. private int bonus;  
  6.   
  7. //getters and setters  
  8. }  
File: Contract_Employee.java
  1. package com.javatpoint.mypackage;  
  2.   
  3. public class Contract_Employee extends Employee{  
  4.     private float pay_per_hour;  
  5.     private String contract_duration;  
  6.   
  7. //getters and setters  
  8. }  

2) Create the mapping file for Persistent class

The mapping has been discussed above for the hierarchy.
File: employee.hbm.xml
  1. <?xml version='1.0' encoding='UTF-8'?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC  
  3.           "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4.           "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  5.   
  6. <hibernate-mapping>  
  7. <class name="com.javatpoint.mypackage.Employee" table="emp121" discriminator-value="emp">  
  8. <id name="id">  
  9. <generator class="increment"></generator>  
  10. </id>  
  11.   
  12. <discriminator column="type" type="string"></discriminator>  
  13. <property name="name"></property>  
  14.             
  15. <subclass name="com.javatpoint.mypackage.Regular_Employee" discriminator-value="reg_emp">  
  16. <property name="salary"></property>  
  17. <property name="bonus"></property>  
  18. </subclass>  
  19.             
  20. <subclass name="com.javatpoint.mypackage.Contract_Employee" discriminator-value="con_emp">  
  21. <property name="pay_per_hour"></property>  
  22. <property name="contract_duration"></property>  
  23. </subclass>  
  24.             
  25. </class>  
  26.             
  27. </hibernate-mapping>  

3) Add mapping of hbm file in configuration file

Open the hibernate.cgf.xml file, and add an entry of mapping resource like this:
  1. <mapping resource="employee.hbm.xml"/>  
Now the configuration file will look like this:
File: hibernate.cfg.xml
  1. <?xml version='1.0' encoding='UTF-8'?>  
  2. <!DOCTYPE hibernate-configuration PUBLIC  
  3.           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  4.           "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
  5.   
  6. <hibernate-configuration>  
  7.   
  8.     <session-factory>  
  9.         <property name="hbm2ddl.auto">update</property>  
  10.         <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>  
  11.         <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>  
  12.         <property name="connection.username">system</property>  
  13.         <property name="connection.password">oracle</property>  
  14.         <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>  
  15.     <mapping resource="employee.hbm.xml"/>  
  16.     </session-factory>  
  17.   
  18. </hibernate-configuration>  
The hbm2ddl.auto property is defined for creating automatic table in the database.

4) Create the class that stores the persistent object

In this class, we are simply storing the employee objects in the database.
File: StoreData.java
  1. package com.javatpoint.mypackage;  
  2. import org.hibernate.*;  
  3. import org.hibernate.cfg.*;  
  4.   
  5. public class StoreData {  
  6. public static void main(String[] args) {  
  7.     Session session=new Configuration().configure("hibernate.cfg.xml")  
  8.                         .buildSessionFactory().openSession();  
  9.   
  10.     Transaction t=session.beginTransaction();  
  11.       
  12.     Employee e1=new Employee();  
  13.     e1.setName("sonoo");  
  14.       
  15.     Regular_Employee e2=new Regular_Employee();  
  16.     e2.setName("Vivek Kumar");  
  17.     e2.setSalary(50000);  
  18.     e2.setBonus(5);  
  19.       
  20.     Contract_Employee e3=new Contract_Employee();  
  21.     e3.setName("Arjun Kumar");  
  22.     e3.setPay_per_hour(1000);  
  23.     e3.setContract_duration("15 hours");  
  24.       
  25.     session.persist(e1);  
  26.     session.persist(e2);  
  27.     session.persist(e3);  
  28.       
  29.     t.commit();  
  30.     session.close();  
  31.     System.out.println("success");  
  32. }