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

@Inheritance(strategy=InheritanceType.SINGLE_TABLE),

@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:

Employee.java


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

@Entity
@Table(name = "employee101")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="type",discriminatorType=DiscriminatorType.STRING)
@DiscriminatorValue(value="employee")

public class Employee {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
    
@Column(name = "id")
private int id;

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

//setters and getters
}

Regular_Employee.java


package com.javatportal.mypackage;

import javax.persistence.*;

@Entity
@DiscriminatorValue("regularemployee")
public class Regular_Employee extends Employee{
	
@Column(name="salary")	
private float salary;

@Column(name="bonus")	
private int bonus;

//setters and getters
}

Contract_Employee.java


package com.javatportal.mypackage;

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

@Entity
@DiscriminatorValue("contractemployee")
public class Contract_Employee extends Employee{
	
	@Column(name="pay_per_hour")
	private float pay_per_hour;
	
	@Column(name="contract_duration")
	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:

hibernate.cfg.xml

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

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

    <session-factory>
        <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"/>
    </session-factory>

</hibernate-configuration>

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.

StoreTest.java


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();
	e1.setName("mithilesh");
	
	Regular_Employee e2=new Regular_Employee();
	e2.setName("rahul Kumar");
	e2.setSalary(60000);
	e2.setBonus(5);
	
	Contract_Employee e3=new Contract_Employee();
	e3.setName("neha kumari");
	e3.setPay_per_hour(2000);
	e3.setContract_duration("15 hours");
	
	session.persist(e1);
	session.persist(e2);
	session.persist(e3);
	
	t.commit();
	session.close();
	System.out.println("success");
}
}


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