Pages Navigation Menu

Coding is much easier than you think

Table per Class Hierarchy Example using XML

Table per class hierarchy
In our previous tutorial we got introduced to Inheritance Mapping In Hibernate, In this article I will explain you about Table per Class Hierarchy Inheritance mapping. By this inheritance strategy, we can map the whole hierarchy in a single table. Here, an extra column otherwise known as discriminator column is created in the table to identify the class. In the table, for each record some columns will be empty; those columns for which the particular Java class does not have fields.
The above is the Hierarchy of classes involved. Here Employee is the super class for PermanentEmployee and ContractEmployee classes. Now Let us create Java classes for the above hierarchy to implement

Model class


package model;

public class Employee {
	private int empID;
	private String empName;

	public int getEmpID() {
		return empID;

	public String getEmpName() {
		return empName;

	public void setEmpID(int empID) {
		this.empID = empID;

	public void setEmpName(String empName) {
		this.empName = empName;



package model;

public class PermanentEmployee extends Employee {
	private String companyName;

	public String getCompanyName() {
		return companyName;

	public void setCompanyName(String companyName) {
		this.companyName = companyName;



package model;

public class ContractEmployee extends Employee {
	String contractorName;

	public String getContractorName() {
		return contractorName;

	public void setContractorName(String contractorName) {
		this.contractorName = contractorName;


Hibernate Mapping xml


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	<class name="model.Employee" discriminator-value="Employee"
		<id name="empID" type="int" column="EMPID">
			<generator class="assigned" />
		<discriminator column="DTYPE" type="string" />
		<property name="empName" type="java.lang.String" column="EMP_NAME" />

		<subclass name="model.PermanentEmployee" discriminator-value="Permanent employee">
			<property name="companyName" column="COMPANY_NAME" />
		<subclass name="model.ContractEmployee" discriminator-value="Contract Employee">
			<property name="contractorName" column="CONTRACTOR_NAME" />


In case of table per class hierarchy a discriminator column is added by the hibernate framework that specifies the type of the record. It is mainly used to identify which derived class object have been saved in the table (see Database screen shot for better understanding). To specify this, discriminator sub element (Line no 10) of class must be specified.
The subclass sub element of class, specifies the subclass. In this case, PermanentEmployee and ContractEmployee are the subclasses of Employee class.

Hibernate Configuration file



		<!-- Database connection settings -->
		<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
		<property name="hibernate.connection.username">system</property>
		<property name="hibernate.connection.password">admin</property>
		<property name="hibernate.connection.url">jdbc:oracle:thin:@</property>

		<!-- JDBC connection pool (use the built-in) -->
		<property name="connection.pool_size">2</property>

		<!-- SQL dialect -->
		<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>

		<!-- Enable Hibernate's current session context -->
		<property name="current_session_context_class">thread</property>

		<!-- Disable the second-level cache -->
		<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

		<!-- Echo all executed SQL to sysout -->
		<property name="show_sql">true</property>

		<!-- Drop and re-create the database schema on startup -->
		<property name="">create</property>
		<mapping resource="Employee.hbm.xml" />



The property is defined for creating automatic table in the database.

Client program

Create the class that stores the persistent object in this class, we are simply storing the employee objects in the database.

package util;

import model.ContractEmployee;
import model.PermanentEmployee;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class HibernateUtil {

	public static void main(String[] args) {

		Configuration cf = new Configuration().configure("hibernate.cfg.xml");

		StandardServiceRegistryBuilder srb = new StandardServiceRegistryBuilder();
		ServiceRegistry sr =;
		SessionFactory sf = cf.buildSessionFactory(sr);

		Session session = sf.openSession();

		PermanentEmployee p1 = new PermanentEmployee();

		PermanentEmployee p2 = new PermanentEmployee();

		// create two objects of ContractEmployee
		ContractEmployee t1 = new ContractEmployee();
		t1.setContractorName("ABD Consultancy");

		ContractEmployee t2 = new ContractEmployee();
		t2.setContractorName("MN Consultancy");

		Transaction tx = session.beginTransaction();;;;;

		System.out.println("Object saved successfully !");


Eclipse Console

eclipse console

Database output


About Mohaideen Jamil

Am currently working as a Struts 2 Developer in a reputed IT Organisations. I can help you with teaching Core java and Struts 2. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.