Class Inheritance Mapping : Table per subclass

Table per subclass
In this approach every class, subclass or interfaces, that declare persistent properties has its own table. The table for each class contains column for non-inherited properties along with a primary key which is also a foreign key of the super class table.

To understand this approach consider following inheritance hierarchy as shown in figure below:

To start with, we create an abstract class Person. The code for this class is shown in example below:

package com.thecafetechno.university;

public abstract class Person {
	Long id;
	String name;
	String department;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDepartment() {
		return department;
	}

	public void setDepartment(String department) {
		this.department = department;
	}
}

We create another class Student that extends above abstract class Person.

package com.thecafetechno.university;

public class Student extends Person {
	int year;

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public String toString() {
		return name;
	}

}

Next, we create another class Teacher that too extends class Person. The code for this class is given below:

package com.thecafetechno.university;

public class Teacher extends Person {
	int experience;
	String specialization;

	public int getExperience() {
		return experience;
	}

	public void setExperience(int experience) {
		this.experience = experience;
	}

	public String getSpecialization() {
		return specialization;
	}

	public void setSpecialization(String specialization) {
		this.specialization = specialization;
	}

}

After creating all classes that we need for our inheritance hierarchy, we create mapping file Person.hbm.xml .

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="com.thecafetechno.university.Person" table="person_details_TPSC">
		<id name="id" column="person_id">
			<generator class="native"></generator>
		</id>
		<property name="name" column="person_name"></property>
		<property name="department" column="person_department"></property>
		<joined-subclass name="com.thecafetechno.university.Student"
			table="student_details_TPSC">
			<key column="student_id"></key>
			<property name="year" column="student_year"></property>
		</joined-subclass>
		<joined-subclass name="com.thecafetechno.university.Teacher"
			table="teacher_details_TPSC">
			<key column="teacher_id"></key>
			<property name="experience" column="teacher_experience"></property>
			<property name="specialization" column="teacher_specialization"></property>
		</joined-subclass>
	</class>
</hibernate-mapping>

Reader can observe from above mapping file that we start with declaring the top most class in hierarchy in <class> tag

<class name="com.thecafetechno.university.Person" table="person_details_TPSC">

After declaring all properties of this class and mapping them to the columns of the respective table, we declare all subclasses in <joined-subclass> tag along with mapping of respective properties as:

<joined-subclass name="com.thecafetechno.university.Student"
table="student_details_TPSC">
<key column="student_id"></key>
<property name="year" column="student_year"></property>
</joined-subclass>

The above mapping file shows that for entire hierarchy we create a single mapping file.