Table Per Hierarchy – Hibernate
Table per class hierarchy is mapping all values in one table. The code given below is tested in NetBeans.

1) In Netbeans file–>new project –>java –>java webapplication
2) Give webapplication a name(“TPH”) –> Next
3) Select Hibernate Framework and choose the database where you want to create this table

Create 3 java files,

Create a folder named “pojo” inside source packages

public class Student {
    String sname;
    String sdept;
    int id;

    public String getSname() {
        return sname;

    public void setSname(String sname) {
        this.sname = sname;

    public String getSdept() {
        return sdept;

    public void setSdept(String sdept) {
        this.sdept = sdept;

    public int getId() {
        return id;

    public void setId(int id) { = id;

public class Regular extends Student {
    private String regularfees;

    public String getRegularfees() {
        return regularfees;

    public void setRegularfees(String regularfees) {
        this.regularfees = regularfees;

public class Parttime extends Student {
    private String partimefees;

    public String getPartimefees() {
        return partimefees;

    public void setPartimefees(String partimefees) {
        this.partimefees = partimefees;


     <property name="">update</property> //update -->Checks if table exists, if table exists values get updated or new table will get created
    <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">"url"</property>
    <property name="hibernate.connection.username">"username"</property>
    <property name="hibernate.connection.password">"password"</property>
    <mapping resource="student.hbm.xml"/>

Right Click –> New–> Hibernate Mapping Wizard –>student.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

    <class name="pojo.Student" table="student" discriminator-value="stud">
        <id name="id" type="int">
            <column name="id"/>
            <generator class="increment"></generator>
        <discriminator column="type" type="string"></discriminator> 
        <property name="sname" type="string">
            <column name="Name"/>
        <property name="sdept" type="string">
            <column name="Department"/>
        <subclass name="pojo.Regular" discriminator-value="Regular">
            <property name="regularfees" type="string">
            <column name="regularfees"/>
         <subclass name="pojo.Parttime" discriminator-value="pt">
            <property name="partimefees" type="string">
            <column name="partimefees"/>

Inside pojo create a javaclass

public class Save {
    public static void main(String args[])
        Transaction trans=null;
        Session session=new Configuration().configure("hibernate.cfg.xml").buildSessionFactory().openSession(); 
            Student stud=new Student();
            Parttime pt=new Parttime();
            Regular reg=new Regular();


id type Name Department regularfees partimefees
1 pt Beta Models null 20000
2 Regular Delta Science 12000 null
3 stud Alpha Robotics null null







By Sri

One thought on “Table Per Hierarchy – Hibernate”

Leave a Reply

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