Hibernate Table Per Hierarchy using Annotation

In the previous page, we have mapped the inheritance hierarchy with one table just using xml file. Here, we are going to perform this task using annotation. You need to use


@DiscriminatorColumn and @DiscriminatorValue annotations for mapping table per hierarchy strategy. In case of table per hierarchy, one and only table is required to map the inheritance hierarchy. Here, an extra segment (also known as discriminator section) is created in the table to identify the class.

Let's see the inheritance hierarchy:

table per class hierarchy using annotation

There are three classes in this hierarchy. Employee is the super class for Regular_Employee and Contract_Employee classes.

Here the Table per class hierarchy using annotation concept.

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

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:


package com.javatportal.mypackage;
import javax.persistence.*;

@Table(name = "employee101")

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

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

//setters and getters


package com.javatportal.mypackage;

import javax.persistence.*;

public class Regular_Employee extends Employee{
private float salary;

private int bonus;

//setters and getters


package com.javatportal.mypackage;

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;

	//setters and getters

2. Add the persistent classes in configuration file

Open the hibernate.cgf.xml file, and add entries of entity classes like this:

<mapping class="com.javatportal.mypackage.Employee"/>
 <mapping class="com.javatportal.mypackage.Contract_Employee"/>
 <mapping class="com.javatportal.mypackage.Regular_Employee"/>

Now the configuration file will look like this:


<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

<!-- Generated by MyEclipse Hibernate Tools.                   -->

        <property name="hbm2ddl.auto">update</property>
        <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
        <property name="connection.username">system</property>
        <property name="connection.password">oracle</property>
        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
 		<mapping class="com.javatportal.mypackage.Employee"/>
 		<mapping class="com.javatportal.mypackage.Contract_Employee"/>
 		<mapping class="com.javatportal.mypackage.Regular_Employee"/>


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

Create the class that stores the persistent object

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


package com.javatportal.mypackage;

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

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

Topics in Hibernate Inheritance Mapping

Table Per Hierarchy using xml file
Table Per Hierarchy using Annotation
Table Per Concrete class using xml file
Table Per Concrete class using Annotation
Table Per Subclass using xml file
Table Per Subclass using Annotation