Table Per Subclass Example using xml file

In case of Table Per Subclass, subclass mapped tables are related to parent class mapped table by primary key and foreign key relationship.
The <joined-subclass> element of class is used to map the child class with parent using the primary key and foreign key relation.
In this example, we are going to use hb2ddl.auto property to generate the table automatically. So we don't need to be worried about creating tables in the database.
Let's see the hierarchy of classes that we are going to map.
table per concrete class Let's see how can we map this hierarchy by joined-subclass element:
  1. <?xml version='1.0' encoding='UTF-8'?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC  
  3.   
  4.   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  5.   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  6.   
  7.   
  8.   <hibernate-mapping>  
  9.   <class name="com.javatpoint.mypackage.Employee" table="emp123">  
  10.   <id name="id">  
  11.   <generator class="increment"></generator>  
  12.   </id>  
  13.   
  14.   <property name="name"></property>  
  15.   
  16.   <joined-subclass name="com.javatpoint.mypackage.Regular_Employee" table="regemp123">  
  17.   <key column="eid"></key>  
  18.   <property name="salary"></property>  
  19.   <property name="bonus"></property>  
  20.   </joined-subclass>  
  21.    
  22.   <joined-subclass name="com.javatpoint.mypackage.Contract_Employee" table="contemp123">  
  23.   <key column="eid"></key>  
  24.   <property name="pay_per_hour"></property>  
  25.   <property name="contract_duration"></property>  
  26.   </joined-subclass>  
  27.   
  28.   </class>  
  29.   </hibernate-mapping>  
In case of table per subclass class, there will be three tables in the database, each representing a particular class.
The joined-subclass subelement of class, specifies the subclass. The key subelement of joined-subclass is used to generate the foreign key in the subclass mapped table. This foreign key will be associated with the primary key of parent class mapped table.

The table structure for each table will be as follows:

Table structure for Employee class

table per subclass class

Table structure for Regular_Employee class

table per subclass class

Table structure for Contract_Employee class

table per subclass class

Example of Table per subclass class

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.   
  4.   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  5.   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  6.   
  7.   
  8.   <hibernate-mapping>  
  9.   <class name="com.javatpoint.mypackage.Employee" table="emp123">  
  10.   <id name="id">  
  11.   <generator class="increment"></generator>  
  12.   </id>  
  13.   
  14.   <property name="name"></property>  
  15.   
  16.   <joined-subclass name="com.javatpoint.mypackage.Regular_Employee" table="regemp123">  
  17.   <key column="eid"></key>  
  18.   <property name="salary"></property>  
  19.   <property name="bonus"></property>  
  20.   </joined-subclass>  
  21.    
  22.   <joined-subclass name="com.javatpoint.mypackage.Contract_Employee" table="contemp123">  
  23.   <key column="eid"></key>  
  24.   <property name="pay_per_hour"></property>  
  25.   <property name="contract_duration"></property>  
  26.   </joined-subclass>  
  27.   
  28.   </class>  
  29.   </hibernate-mapping>  

3) create 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.   
  3. import org.hibernate.*;  
  4. import org.hibernate.cfg.*;  
  5.   
  6. public class StoreData {  
  7. public static void main(String[] args) {  
  8.     Session session=new Configuration().configure("hibernate.cfg.xml")  
  9.                         .buildSessionFactory().openSession();  
  10.       
  11.     Transaction t=session.beginTransaction();  
  12.       
  13.     Employee e1=new Employee();  
  14.     e1.setName("sonoo");  
  15.       
  16.     Regular_Employee e2=new Regular_Employee();  
  17.     e2.setName("Vivek Kumar");  
  18.     e2.setSalary(50000);  
  19.     e2.setBonus(5);  
  20.       
  21.     Contract_Employee e3=new Contract_Employee();  
  22.     e3.setName("Arjun Kumar");  
  23.     e3.setPay_per_hour(1000);  
  24.     e3.setContract_duration("15 hours");  
  25.       
  26.     session.persist(e1);  
  27.     session.persist(e2);  
  28.     session.persist(e3);  
  29.       
  30.     t.commit();  
  31.     session.close();  
  32.     System.out.println("success");  
  33. }  
  34. }