Pages Navigation Menu

Coding is much easier than you think

Table per Subclass Example using XML file

Table per Sub Class
In case of Table per Subclass, subclass mapped tables are related to parent class table by primary key and foreign key relationship.
The <joined-subclass> element of class is used to map the child class with parent using the primary key and foreign key relation.
In this example, we are going to use hb2ddl.auto property to generate the table automatically. So we don’t need to worry about creating tables in the database.
Let’s see the hierarchy of classes that we are going to map.
The above is the Hierarchy of classes involved. Here Employee is the super class for PermanentEmployee and ContractEmployee classes. Now Let us create Java classes for the above hierarchy to implement

Model class

File: Employee.java

package model;

public class Employee {
	private int empID;
	private String empName;

	public int getEmpID() {
		return empID;

	public String getEmpName() {
		return empName;

	public void setEmpID(int empID) {
		this.empID = empID;

	public void setEmpName(String empName) {
		this.empName = empName;


File: PermanentEmployee.java

package model;

public class PermanentEmployee extends Employee {
	private String companyName;

	public String getCompanyName() {
		return companyName;

	public void setCompanyName(String companyName) {
		this.companyName = companyName;


File: ContractEmployee.java

package model;

public class ContractEmployee extends Employee {
	String contractorName;

	public String getContractorName() {
		return contractorName;

	public void setContractorName(String contractorName) {
		this.contractorName = contractorName;


Hibernate Mapping xml


	<class name="model.Employee" table="EMPLOYEE">
		<id name="empID" type="int" column="EMPID">
			<generator class="assigned" />
		<property name="empName" type="java.lang.String" column="EMP_NAME" />

		<joined-subclass name="model.PermanentEmployee"
			table="P_EMPLOYEE" dynamic-update="true">
			<key column="PID" />
			<property name="companyName" column="COMPANY_NAME" />

		<joined-subclass name="model.ContractEmployee"
			table="T_EMPLOYEE" dynamic-update="true">
			<key column="TID" />
			<property name="contractorName" column="CONTRACTOR_NAME" />


In case of table per subclass class, there will be three tables in the database, each representing a particular class.
The joined-subclass sub element of class, specifies the subclass. The key sub element of joined-subclass is used to generate the foreign key in the subclass mapped table. This foreign key will be associated with the primary key of parent class mapped table.

Hibernate Configuration file



		<!-- Database connection settings -->
		<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
		<property name="hibernate.connection.username">system</property>
		<property name="hibernate.connection.password">admin</property>
		<property name="hibernate.connection.url">jdbc:oracle:thin:@</property>

		<!-- JDBC connection pool (use the built-in) -->
		<property name="connection.pool_size">2</property>

		<!-- SQL dialect -->
		<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>

		<!-- Enable Hibernate's current session context -->
		<property name="current_session_context_class">thread</property>

		<!-- Disable the second-level cache -->
		<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

		<!-- Echo all executed SQL to sysout -->
		<property name="show_sql">true</property>

		<!-- Drop and re-create the database schema on startup -->
		<property name="hibernate.hbm2ddl.auto">create</property>
		<mapping resource="Employee.hbm.xml" />



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

Client program

Now let us write one client program for three bean programs – Employee, PermanentEmployee, ContractEmployee

package util;

import model.ContractEmployee;
import model.PermanentEmployee;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class HibernateUtil {

	public static void main(String[] args) {

		Configuration cf = new Configuration().configure("hibernate.cfg.xml");

		StandardServiceRegistryBuilder srb = new StandardServiceRegistryBuilder();
		ServiceRegistry sr = srb.build();
		SessionFactory sf = cf.buildSessionFactory(sr);

		Session session = sf.openSession();

		PermanentEmployee p1 = new PermanentEmployee();

		PermanentEmployee p2 = new PermanentEmployee();

		// create two objects of ContractEmployee
		ContractEmployee t1 = new ContractEmployee();
		t1.setContractorName("ABD Consultancy");

		ContractEmployee t2 = new ContractEmployee();
		t2.setContractorName("MN Consultancy");

		Transaction tx = session.beginTransaction();


		System.out.println("Object saved successfully !");


Eclipse Console

Table per sub class

Database output

PID and TID are the foreign keys for EMPID of super class.

About Mohaideen Jamil

I'm a Full stack developer of IT exp in J2EE, AngularJs, MicroServices, Docker, Spring (Boot, MVC, Cloud), Bluemix, DevOps. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.

%d bloggers like this: