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)
Person.hbm.xml
hibernate.cfg.xml

Create MySQL Tables

student table
hibernate inheritance example

teacher table

hibernate inheritance example

Person.java

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;
    }

}

 

Student.java

public class Student extends Person {

    private String year;

    public Student() {
    }

    public String getYear() {
        return year;
    }

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

}

 

Teacher.java

public class Teacher extends Person {

    private String subject;

    public Teacher() {
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

}

 

Inheritance.java

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();
            session.save(s);
            session.save(t);
            tx.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
        }
    }
}

 

TestInheritance.java

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.setId(5);
        s3.setName("Student3");
        s3.setYear("3rd Year");
        t3.setId(6);
        t3.setName("Teacher3");
        t3.setSubject("Chemistry");
        ihr.savePerson(s3, t3);
        System.out.println("***Testing for Table per Concrete Class hierarchy***");
    }

}

 

Person.hbm.xml:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <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>
        <union-subclass name="in.sblog.domain.Teacher" table="teacher">
            <property name="subject" column="subject" length="50" />
        </union-subclass>
    </class>
</hibernate-mapping>

 

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">
<hibernate-configuration>
  <session-factory>
    <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"/>
  </session-factory>
</hibernate-configuration>

 

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.

Tags:

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on Roy Tutorials | TwitterFacebook Google PlusLinkedin | Reddit | Email Me

Leave a Reply

Your email address will not be published. Required fields are marked *