Hibernate Inheritance strategy: Table Per Concrete Class Hierarchy

In Table Per Concrete Class Hierarchy will have the number of tables in the database equals to the number of derived classes. Once we save the derived class object, then derived class data and base class data will be saved in the derived class related table in the database. We need the tables only for derived classes. For this in the hibernate mapping file we need to use one new element <union-subclass/> under <class/> tag.
So data will be saved only in student and teacher tables.

Files used in this example
Person.java (Base class)
Student.java (Derived class)
Teacher.java (Derived class)
Inheritance.java (Executing Query helper class)
TestInheritance.java (for our logic)
Create MySQL Tables
student table
hibernate inheritance example
teacher table
hibernate inheritance example

public abstract class Person implements Serializable {
    private int id;
    private String name;
    public Person() {
    public int getId() {
        return id;
    public void setId(int id) {
        this.id = id;
    public String getName() {
        return name;
    public void setName(String name) {
        this.name = name;


public class Student extends Person {
    private String year;
    public Student() {
    public String getYear() {
        return year;
    public void setYear(String year) {
        this.year = year;


public class Teacher extends Person {
    private String subject;
    public Teacher() {
    public String getSubject() {
        return subject;
    public void setSubject(String subject) {
        this.subject = subject;


import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class Inheritance {
    Session session = null;
    public Inheritance() {
        session = HibernateUtil.getSessionFactory().getCurrentSession();
    public void savePerson(Student s, Teacher t) {
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
        } catch (HibernateException e) {


public class TestInheritance {
     * @param args the command line arguments
    public static void main(String[] args) {
        Inheritance ihr = new Inheritance();
        System.out.println("***Testing for Table per Concrete Class hierarchy***");
        Student s3 = new Student();
        Teacher t3 = new Teacher();
        s3.setYear("3rd Year");
        ihr.savePerson(s3, t3);
        System.out.println("***Testing for Table per Concrete Class hierarchy***");


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    <class name="in.sblog.domain.Person" table="person">
        <id name="id" column="id" />
        <property name="name" column="name" />
        <union-subclass name="in.sblog.domain.Student" table="student">
            <property name="year" column="year" length="50" />
        <union-subclass name="in.sblog.domain.Teacher" table="teacher">
            <property name="subject" column="subject" length="50" />


<?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">
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/cdcol?zeroDateTimeBehavior=convertToNull</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.format_sql">true</property>
    <property name="hibernate.current_session_context_class">thread</property>
    <property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
    <mapping resource="in/sblog/domain/Person.hbm.xml"/>


Once the data have been successfully saved into the database the output will be

student table
hibernate inheritance example
teacher table
hibernate inheritance example
That’s all. Thanks for your patience. Please do not forget to leave a comment.

Leave a Comment