Hibernate Table Per Hierarchy using Annotation Tutorial with examples

January 18, 2015 0 Comments

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.

You need to use @Inheritance(strategy=InheritanceType.SINGLE_TABLE), @DiscriminatorColumn and @DiscriminatorValue annotations for mapping table per hierarchy strategy.

In case of table per hierarchy, only one table is required to map the inheritance hierarchy. Here, an extra column (also known as discriminator column) is created in the table to identify the class.

Let's see the inheritance hierarchy:

The table structure for this hierarchy is as shown below:
SALARY decimal(10,2),
BONUS decimal(10,2),
PAY_PER_HOUR decimal(10,2),

Example of Hibernate Table Per Hierarchy using Annotation

You need to follow following steps to create simple example:

  • Create the persistent classes
  • Create the configuration file
  • Create the class to store the fetch the data

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
package com.tutorialsdesk.inheritance;

import javax.persistence.*;


public class Employee{
@Column(name = "id")
private int id;

@Column(name = "name")
private String name;

public Employee() {

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;


File: Regular_Employee.java
package com.tutorialsdesk.inheritance;

import javax.persistence.*;

public class Regular_Employee extends Employee{
private float salary;

private int bonus;

public float getSalary() {
 return salary;

public void setSalary(float salary) {
 this.salary = salary;

public int getBonus() {
 return bonus;

public void setBonus(int bonus) {
 this.bonus = bonus;


File: Contract_Employee.java
package com.tutorialsdesk.inheritance;

import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;

public class Contract_Employee extends Employee{
 private float pay_per_hour;
 private String contract_duration;

 public float getPay_per_hour() {
  return pay_per_hour;

 public void setPay_per_hour(float pay_per_hour) {
  this.pay_per_hour = pay_per_hour;

 public String getContract_duration() {
  return contract_duration;

 public void setContract_duration(String contract_duration) {
  this.contract_duration = contract_duration;


Add the persistent classes in configuration file

Open the hibernate.cgf.xml file, and add entries of entity classes like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        <property name="hibernate.connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
        <property name="hibernate.connection.password">PASSWORD</property>
        <property name="hibernate.connection.url">jdbc:jtds:sqlserver://localhost:1433/DBNAME</property>
        <property name="hibernate.connection.username">USERNAME</property>
        <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
     <property name="connection.pool_size">1</property>
        <property name="hbm2ddl.auto">create</property>
        <property name="show_sql">true</property>  
  <property name="format_sql">true</property>  
        <mapping class="com.tutorialsdesk.inheritance.Employee"/>
        <mapping class="com.tutorialsdesk.inheritance.Contract_Employee"/>
       <mapping class="com.tutorialsdesk.inheritance.Regular_Employee"/>

Create the class that stores the persistent object

In this class, we are simply storing the employee objects in the database.

File: StoreTest.java
package com.tutorialsdesk.inheritance;

import org.hibernate.*;
import org.hibernate.cfg.*;

public class StoreData {
public static void main(String[] args) {
 AnnotationConfiguration cfg=new AnnotationConfiguration();
 Session session=cfg.configure("/conf/hibernate.cfg.xml").buildSessionFactory().openSession();
 Transaction t=session.beginTransaction();
 Employee e1=new Employee();
 Regular_Employee e2=new Regular_Employee();
 e2.setName("Vivek Kumar");
 Contract_Employee e3=new Contract_Employee();
 e3.setName("Arjun Kumar");
 e3.setContract_duration("15 hours");

Hibernate Table Per Hierarchy using Annotation Tutorial with examples

Prakash Hari Sharma
I'm a software developer and Java enthusiast. I like clean and modular code, enjoy Agile projects and have a passion for trying out new things. I try to learn and teach every day, and occasionally I'm even able to blog about it. :)