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:

Employee.java


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

@Entity
@Table(name = "employee103")
@Inheritance(strategy=InheritanceType.JOINED)

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
@Table(name="regularemployee103")
@PrimaryKeyJoinColumn(name="ID")
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.*;

@Entity
@Table(name="contractemployee103")
@PrimaryKeyJoinColumn(name="ID")
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. 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:


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.


3. Create the class that stores the persistent object

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


StoreData.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("Neeraj Kumar");
	e3.setPay_per_hour(1000);
	e3.setContract_duration("15 hours");
	
	session.persist(e1);
	session.persist(e2);
	session.persist(e3);
	
	t.commit();
	session.close();
	System.out.println("success");
}
}