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.

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 5.3//EN"  
  5.   "http://hibernate.sourceforge.net/hibernate-mapping-5.3.dtd">  
  6.   
  7.   
  8.   <hibernate-mapping>  
  9.   <class name="com.javacodegeeks.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.javacodegeeks.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.javacodegeeks.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, indicates the subclass.

The key sub-component of joined-subclass is utilized to produce the outside key in the subclass mapped table. This remote key will be related with the essential key of parent class mapped table.


The table structure for each table will be as follows:

Table structure for Employee 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.javacodegeeks.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.javacodegeeks.mypackage;  
  2. public class Regular_Employee extends Employee{  
  3. private float salary;  
  4. private int bonus;  
  5.   
  6. //getters and setters  
  7. }  
File: Contract_Employee.java
  1. package com.javacodegeeks.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 5.3//EN"  
  5.   "http://hibernate.sourceforge.net/hibernate-mapping-5.3.dtd">  
  6.   
  7.   
  8.   <hibernate-mapping>  
  9.   <class name="com.javacodegeeks.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.javacodegeeks.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.javacodegeeks.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 5.3//EN"  
  4.           "http://hibernate.sourceforge.net/hibernate-configuration-5.3.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">jtp</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.javacodegeeks.mypackage;  
  2.   
  3. import org.hibernate.Session;  
  4. import org.hibernate.SessionFactory;  
  5. import org.hibernate.Transaction;  
  6. import org.hibernate.boot.Metadata;  
  7. import org.hibernate.boot.MetadataSources;  
  8. import org.hibernate.boot.registry.StandardServiceRegistry;  
  9. import org.hibernate.boot.registry.StandardServiceRegistryBuilder;  
  10.   
  11. public class StoreData {  
  12. public static void main(String[] args) {  
  13.           
  14. StandardServiceRegistry ssr=new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();  
  15. Metadata meta=new MetadataSources(ssr).getMetadataBuilder().build();  
  16.   
  17. SessionFactory factory=meta.buildSessionFactory();  
  18. Session session=factory.openSession();  
  19.   
  20. Transaction t=session.beginTransaction();    
  21.   
  22. Employee e1=new Employee();    
  23. e1.setName("Gaurav Chawla");    
  24.     
  25. Regular_Employee e2=new Regular_Employee();    
  26. e2.setName("Vivek Kumar");    
  27. e2.setSalary(50000);    
  28. e2.setBonus(5);    
  29.     
  30. Contract_Employee e3=new Contract_Employee();    
  31. e3.setName("Arjun Kumar");    
  32. e3.setPay_per_hour(1000);    
  33. e3.setContract_duration("15 hours");    
  34.     
  35. session.persist(e1);    
  36. session.persist(e2);    
  37. session.persist(e3);    
  38.     
  39. t.commit();    
  40. session.close();    
  41. System.out.println("success");    
  42.   
  43. }  
  44. }   ackage com.javacodegeeks.mypackage; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.boot.Metadata; import org.hibernate.boot.MetadataSources; import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; public class StoreData { public static void main(String[] args) { StandardServiceRegistry ssr=new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build(); Metadata meta=new MetadataSources(ssr).getMetadataBuilder().build(); SessionFactory factory=meta.buildSessionFactory(); Session session=factory.openSession(); Transaction t=session.beginTransaction(); Employee e1=new Employee(); e1.setName("Gaurav Chawla"); Regular_Employee e2=new Regular_Employee(); e2.setName("Vivek Kumar"); e2.setSalary(50000); e2.setBonus(5); Contract_Employee e3=new Contract_Employee(); e3.setName("Arjun Kumar"); e3.setPay_per_hour(1000); e3.setContract_duration("15 hours"); session.persist(e1); session.persist(e2); session.persist(e3); t.commit(); session.close(); System.out.println("success"); } }





javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition