Table Per Subclass using Annotation in Hibernate

As we have specified earlier, in case of table per subclass strategy, tables are created as per persistent classes but they are reated using primary and foreign key. So there will not be duplicate columns in the relation.

We need to specify @Inheritance(strategy=InheritanceType.JOINED) in the parent class and @PrimaryKeyJoinColumn annotation in the subclasses.

Let's see the hierarchy of classes that we are going to map.

table per subclass strategy

Example of Table per subclass class using Annotation

In this example we are creating the three classes and provide mapping of these classes in the employee.hbm.xml file.

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 = "employee103")

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.*;

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

	//setters and getters

2. create configuration file

Open the hibernate.cgf.xml file, and add an entry of mapping resource 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="">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 property is defined for creating automatic table in the database.

3. 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("Neeraj Kumar");
	e3.setContract_duration("15 hours");