Table Per Hierarchy with XML


Let us understand about Table Per Hierarchy with XML


In this approach, as the name suggests the entire hierarchy is mapped to a single table. I.e. All attributes of all the classes in the hierarchy are stored in a single table.

A discriminator column is used to distinguish different classes.

Null values will be stored in the table for which there is no column applicable.

Let’s consider the below example



Hibernate_InheritancePerHierarchy

In this example, we have 3 classes where Employee class is the super class for both PermanentEmployee and ContractEmployee classes but we will store all the attributes of all 3 classes in a single table.

Let’s create hibernate project using this strategy

Step 1

Create hibernate project

Please refer Hibernate setup in eclipse article on how to do it.

Project Structure


TPH_XML_Proj_structure

Step 2

Update pom.xml with Hibernate and Mysql dependencies

  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4.      <modelVersion>4.0.0</modelVersion>
  5.  
  6.     <groupId>TablePerHierarchyXML</groupId>
  7.     <artifactId>TablePerHierarchyXML</artifactId>
  8.     <version>0.0.1-SNAPSHOT</version>
  9.     <packaging>jar</packaging>
  10.  
  11.     <name>TablePerHierarchyXML</name>
  12.     <url>http://maven.apache.org</url>
  13.  
  14.     <properties>
  15.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  16.     </properties>
  17.  
  18.     <dependencies>
  19.  
  20.         <dependency>
  21.             <groupId>junit</groupId>
  22.             <artifactId>junit</artifactId>
  23.             <version>3.8.1</version>
  24.             <scope>test</scope>
  25.         </dependency>
  26.  
  27.         <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
  28.         <dependency>
  29.             <groupId>org.hibernate</groupId>
  30.             <artifactId>hibernate-core</artifactId>
  31.             <version>5.2.6.Final</version>
  32.         </dependency>
  33.  
  34.         <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
  35.         <dependency>
  36.             <groupId>mysql</groupId>
  37.             <artifactId>mysql-connector-java</artifactId>
  38.             <version>6.0.5</version>
  39.         </dependency>
  40.  
  41.     </dependencies>
  42. </project>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	 <modelVersion>4.0.0</modelVersion>

	<groupId>TablePerHierarchyXML</groupId>
	<artifactId>TablePerHierarchyXML</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>TablePerHierarchyXML</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>5.2.6.Final</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>6.0.5</version>
		</dependency>

	</dependencies>
</project>

Step 3

Create Employee class

  1. package com.kb.model;
  2.  
  3. public class Employee {
  4.     private int id;
  5.     private String name;
  6.     public int getId() {
  7.         return id;
  8.     }
  9.     public void setId(int id) {
  10.         this.id = id;
  11.     }
  12.     public String getName() {
  13.         return name;
  14.     }
  15.     public void setName(String name) {
  16.         this.name = name;
  17.     }
  18. }
package com.kb.model;

public class Employee {
	private int id;
	private String name;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

Step 4

Create PermanentEmployee class

  1. package com.kb.model;
  2.  
  3. public class PermanentEmployee extends Employee{
  4.     private double salary;
  5.  
  6.     public double getSalary() {
  7.         return salary;
  8.     }
  9.  
  10.     public void setSalary(double salary) {
  11.         this.salary = salary;
  12.     }
  13.  
  14. }
package com.kb.model;

public class PermanentEmployee extends Employee{
	private double salary;

	public double getSalary() {
		return salary;
	}

	public void setSalary(double salary) {
		this.salary = salary;
	}

}

Step 5

Create ContractEmployee class

  1. package com.kb.model;
  2.  
  3. public class ContractEmployee extends Employee{
  4.     private double hourlyRate;
  5.  
  6.     public double getHourlyRate() {
  7.         return hourlyRate;
  8.     }
  9.  
  10.     public void setHourlyRate(double hourlyRate) {
  11.         this.hourlyRate = hourlyRate;
  12.     }
  13.  
  14. }
package com.kb.model;

public class ContractEmployee extends Employee{
	private double hourlyRate;

	public double getHourlyRate() {
		return hourlyRate;
	}

	public void setHourlyRate(double hourlyRate) {
		this.hourlyRate = hourlyRate;
	}

}

Step 6

Create 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 package="com.kb.model">  
  7.  
  8. <class name="Employee" table="Employee1" discriminator-value="E">  
  9. <id name="id">  
  10.        <generator class="increment"></generator>  
  11. </id>  
  12.  
  13. <discriminator column="discriminator" type="string"></discriminator>  
  14. <property name="name"></property>  
  15.            
  16.              <subclass name="PermanentEmployee" extends="Employee" discriminator-value="PE">  
  17.                     <property name="salary"></property>  
  18.              </subclass>
  19.            
  20.              <subclass name="ContractEmployee" extends="Employee" discriminator-value="CE">  
  21.                     <property name=" hourlyRate"></property>  
  22.              </subclass>  
  23.            
  24. </class>  
  25.            
  26. </hibernate-mapping>  
<?xml version='1.0' encoding='UTF-8'?>  
<!DOCTYPE hibernate-mapping PUBLIC  
          "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
          "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  
<hibernate-mapping package="com.kb.model">  

<class name="Employee" table="Employee1" discriminator-value="E">  
<id name="id">  
       <generator class="increment"></generator>  
</id>  
  
<discriminator column="discriminator" type="string"></discriminator>  
<property name="name"></property>  
            
             <subclass name="PermanentEmployee" extends="Employee" discriminator-value="PE">  
                    <property name="salary"></property>  
             </subclass> 
            
             <subclass name="ContractEmployee" extends="Employee" discriminator-value="CE">  
                    <property name=" hourlyRate"></property>  
             </subclass>  
            
</class>  
            
</hibernate-mapping>  


We have defined only one mapping file for all 3 classes.

“discriminator” tag is used to define the discriminator column which is mainly used to identify the type of the record.

We have defined discriminators as “E” to identify that record belongs to Employee class

PE” to identify that record belongs to PermanentEmployee class

CE” to identify that record belongs to ContractEmployee class

Hibernate will put the apprpariate discriminator values while persisting the record based on what record we are persisting.

subclass” tag is used to map the subclasses.

Generally we use “class” tag to map the class with table in mapping file but, if the class itself is a subclass then we use “subclass” tag.

We used “subclass” tag for both PermanentEmployee and ContractEmployee classes as both are subclasses of Employee class.

Step 7

Create 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://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  5.  
  6. <hibernate-configuration>
  7.  
  8.    <session-factory>
  9.  
  10.        <!-- Database connection properties -->
  11.        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
  12.        <property name="connection.url">jdbc:mysql://localhost/javainsimpleway</property>
  13.        <property name="connection.username">root</property>
  14.        <property name="connection.password">root</property>
  15.  
  16.        <!-- JDBC connection pool (using the built-in) -->
  17.        <property name="connection.pool_size">1</property>
  18.  
  19.        <!-- SQL dialect -->
  20.        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
  21.  
  22.        <!-- Disable the second-level cache -->
  23.        <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
  24.  
  25.        <!-- Echo all executed SQL to stdout -->
  26.        <property name="show_sql">true</property>
  27.        
  28.        <!-- Format the generated Sql -->
  29.        <property name="format_sql">true</property>
  30.  
  31.        <!-- Dont Drop and re-create the database schema on startup,Just update it -->
  32.        <property name="hbm2ddl.auto">update</property>
  33.  
  34.        <mapping resource="com/kb/mapping/employee.hbm.xml"/>
  35.  
  36.    </session-factory>
  37.  
  38. </hibernate-configuration>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
       "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
       "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
 
<hibernate-configuration>
 
   <session-factory>
 
       <!-- Database connection properties -->
       <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
       <property name="connection.url">jdbc:mysql://localhost/javainsimpleway</property>
       <property name="connection.username">root</property>
       <property name="connection.password">root</property>
 
       <!-- JDBC connection pool (using the built-in) -->
       <property name="connection.pool_size">1</property>
 
       <!-- SQL dialect -->
       <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
 
       <!-- Disable the second-level cache -->
       <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
 
       <!-- Echo all executed SQL to stdout -->
       <property name="show_sql">true</property>
       
       <!-- Format the generated Sql -->
       <property name="format_sql">true</property>
 
       <!-- Dont Drop and re-create the database schema on startup,Just update it -->
       <property name="hbm2ddl.auto">update</property>
 
       <mapping resource="com/kb/mapping/employee.hbm.xml"/>
 
   </session-factory>
 
</hibernate-configuration>


We have defined all the database configuration in this file

hbm2ddl.auto property is defined in the config file which helps in automatic creation of tables in the database based on the mapping.

We have also provided the mapping xml file location using “mapping” tag.

Step 8

Create Hibernate util class

  1. package com.kb.util;
  2.  
  3. import org.hibernate.SessionFactory;
  4. import org.hibernate.cfg.Configuration;
  5.  
  6. public class HibernateUtil {
  7.     private static final SessionFactory sessionFactory = buildSessionFactory();
  8.  
  9.     private static SessionFactory buildSessionFactory() {
  10.         try {
  11.             // Create the SessionFactory from hibernate.cfg.xml
  12.             return new Configuration().configure().buildSessionFactory();
  13.         } catch (Throwable ex) {
  14.             // Make sure you log the exception to track it
  15.             System.err.println("SessionFactory creation failed." + ex);
  16.             throw new ExceptionInInitializerError(ex);
  17.         }
  18.     }
  19.  
  20.     public static SessionFactory getSessionFactory() {
  21.         return sessionFactory;
  22.     }
  23.    
  24.     public static void shutdown() {
  25.         // Optional but can be used to Close caches and connection pools
  26.         getSessionFactory().close();
  27.     }
  28.  
  29. }
package com.kb.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
	private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            return new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Make sure you log the exception to track it
            System.err.println("SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    
    public static void shutdown() {
    	// Optional but can be used to Close caches and connection pools
    	getSessionFactory().close();
    }

}

Step 9

Create main class to interact with DB

  1. package com.kb.db;
  2.  
  3. import org.hibernate.Session;
  4. import org.hibernate.SessionFactory;
  5. import org.hibernate.Transaction;
  6.  
  7. import com.kb.model.ContractEmployee;
  8. import com.kb.model.Employee;
  9. import com.kb.model.PermanentEmployee;
  10. import com.kb.util.HibernateUtil;
  11.  
  12. public class Main {
  13.     public static void main(String[] args) {
  14.         //Get session factory using Hibernate Util class
  15.         SessionFactory sf = HibernateUtil.getSessionFactory();
  16.         //Get session from Sesson factory
  17.         Session session = sf.openSession();
  18.        
  19.         //Begin transaction
  20.         Transaction t=session.beginTransaction();  
  21.  
  22.         //Creating Employee base class record
  23.         Employee employee=new Employee();  
  24.         employee.setName("John");  
  25.        
  26.         //Creating Permanent Employee subclass record
  27.         PermanentEmployee permanentEmployee=new PermanentEmployee();  
  28.         permanentEmployee.setName("Jacob");  
  29.         permanentEmployee.setSalary(30000);  
  30.        
  31.        //Creating Contract Employee subclass record
  32.         ContractEmployee contractEmployee=new ContractEmployee();  
  33.         contractEmployee.setName("Raj");  
  34.         contractEmployee.setHourlyRate(2000);  
  35.        
  36.         //persist all the employee records
  37.         session.persist(employee);  
  38.         session.persist(permanentEmployee);  
  39.         session.persist(contractEmployee);  
  40.        
  41.         //Commit the transaction and close the session
  42.         t.commit();  
  43.         session.close();  
  44.         System.out.println("successfully persisted all the Employee records");  
  45.     }
  46.  
  47. }
package com.kb.db;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.kb.model.ContractEmployee;
import com.kb.model.Employee;
import com.kb.model.PermanentEmployee;
import com.kb.util.HibernateUtil;

public class Main {
	public static void main(String[] args) {
		//Get session factory using Hibernate Util class
		SessionFactory sf = HibernateUtil.getSessionFactory();
		//Get session from Sesson factory
		Session session = sf.openSession();
		
		//Begin transaction
		Transaction t=session.beginTransaction();  

		//Creating Employee base class record
		Employee employee=new Employee();  
		employee.setName("John");  
	    
		//Creating Permanent Employee subclass record
	    PermanentEmployee permanentEmployee=new PermanentEmployee();  
	    permanentEmployee.setName("Jacob");  
	    permanentEmployee.setSalary(30000);  
	    
	   //Creating Contract Employee subclass record
	    ContractEmployee contractEmployee=new ContractEmployee();  
	    contractEmployee.setName("Raj");  
	    contractEmployee.setHourlyRate(2000);  
	    
	    //persist all the employee records
	    session.persist(employee);  
	    session.persist(permanentEmployee);  
	    session.persist(contractEmployee);  
	    
	    //Commit the transaction and close the session
	    t.commit();  
	    session.close();  
	    System.out.println("successfully persisted all the Employee records");  
	}

}


We have created 3 records, one for each Employee, PermanentEmployee and ContractEmployee class.

When we persist these 3 records of different objects, It will be saved in a Single table.

Step 10

Run the above class to check the output

Hibernate: 
    
    create table Employee1 (

        id integer not null,

        discriminator varchar(255) not null,

        name varchar(255),

        salary double precision,

        hourlyRate double precision,

        primary key (id)
    )


Hibernate: 

    select
        max(id)  

    from
        Employee1


Hibernate: 

    insert 
    into
        Employee1
        (name, discriminator, id) 
    values
        (?, 'E', ?)


Hibernate: 

    insert 
    into
        Employee1
        (name, salary, discriminator, id) 
    values
        (?, ?, 'PE', ?)


Hibernate: 

    insert 
    into
        Employee1
        (name, hourlyRate, discriminator, id) 
    values
        (?, ?, 'CE', ?)

successfully persisted all the Employee records


We can see that Create statement is executed only once as it creates a Single table for all the classes.

3 insert statements one for each object we persisted.

Check Table in MYSQL workbench

SELECT * FROM javainsimpleway.employee1;

TPH_Output1

Check Table in MYSQL console

E:\MySql_Install\bin
Mysql –u root –p
Enter password

SELECT * FROM javainsimpleway.employee1;

TPH_Output2

We can see NULL values are stored in Salary column for Contract employee and NULL values are stored in HourlyRate column for Permanent employees.

We can see NULL values are stored in salary and hourlyRate columns for Employee record.

We can see that discriminator column can be used to identify the type of the record.

Advantage

This hierarchy provides best performance when the hierarchy depth is high like multi level as there is only single select query is executed.

Since there are no joins or sub selects are involved, its performance will be good.

Very simple to implement.

Disadvantage

Tables will have unnecessary columns for some of the records as all the columns are not applicable for each record.

If most of the column values are NULL then it is difficult to put a constraint.

Note :This hierarchy is not normalized as many irrelevant columns will be present in each record.

Download this project TablePerHierarchyXML.zip

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on