Table Per Concrete Class Hierarchy in Hibernate ORM

Program 1

package beans;

public class Employee 
{
  private int empid;
  private String empname;
  private int empsalary;

    public Employee() {
    }

    public int getEmpid() {
        return empid;
    }

    public void setEmpid(int empid) {
        this.empid = empid;
    }

    public String getEmpname() {
        return empname;
    }

    public void setEmpname(String empname) {
        this.empname = empname;
    }

    public int getEmpsalary() {
        return empsalary;
    }

    public void setEmpsalary(int empsalary) {
        this.empsalary = empsalary;
    }
  
}

Program 2

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package beans;

/**
 *
 * @author admin
 */
public class Manager extends Employee
{
    private int hra;
    private int da;

    public Manager() {
    }

    public int getHra() {
        return hra;
    }

    public void setHra(int hra) {
        this.hra = hra;
    }

    public int getDa() {
        return da;
    }

    public void setDa(int da) {
        this.da = da;
    }
    
}

Program 3

package beans;

public class Clerk  extends Employee
{
  private int ta;

    public Clerk() {
    }

    public int getTa() {
        return ta;
    }

    public void setTa(int ta) {
        this.ta = ta;
    }
  
}

Program 4

<!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="beans.Employee" table="employee" schema="dataunion">
        <id name="empid"></id>
        <property name="empname"></property>
        <property name="empsalary"></property>
        <union-subclass name="beans.Manager" table="manager" schema="dataunion">
            <property name="hra"></property>
            <property name="da"></property>
        </union-subclass>
        <union-subclass name="beans.Clerk" table="clerk" schema="dataunion">
            <property name="ta"></property>
        </union-subclass>
        <union-subclass name="beans.Sales" table="sales" schema="dataunion">
            <property name="ta"></property>
            <property name="ea"></property>
            <property name="tfa"></property>
        </union-subclass>
        
    </class>
</hibernate-mapping>

Program 5

<!DOCTYPE hibernate-configuration PUBLIC
  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
   <session-factory>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>  
        <property name="connection.url">jdbc:mysql://localhost/dataunion</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root@data</property>
        <property name="connection.pool_size">10</property>
         <property name="hbm2ddl.auto">create</property>
        <property name="show_sql">true</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <mapping resource="resources/Employee.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

Program 6

package view;


import beans.Clerk;
import beans.Employee;
import beans.Manager;
import beans.Sales;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class TestMain 
{
    public static void main(String[] args) 
    {
        Configuration cfg=new Configuration();
        cfg.configure("resources/hibernate.cfg.xml");
        SessionFactory sf=cfg.buildSessionFactory();
        Session session=sf.openSession();
        Employee E1=new Employee();
        E1.setEmpid(100);
        E1.setEmpname("Vikas Sharma");
        E1.setEmpsalary(70000);
        
        Manager M1=new Manager();
        M1.setEmpid(101);
        M1.setEmpname("Amit Verma");
        M1.setEmpsalary(60000);
        M1.setHra(5000);
        M1.setDa(4000);
        
        Clerk C1=new Clerk();
        C1.setEmpid(102);
        C1.setEmpname("Rajesh Khanna");
        C1.setEmpsalary(50000);
        C1.setTa(6000);
        Sales S1=new Sales();
        S1.setEmpid(105);
        S1.setEmpname("Ashok Gupta");
        S1.setEmpsalary(70000);
        S1.setTa(6000);
        S1.setEa(5000);
        S1.setTfa(7000);
        session.save(E1);
        session.save(M1);
        session.save(C1);
        session.save(S1);
        session.beginTransaction().commit();
        session.close();
        sf.close();
        System.out.println("Data Save Done .............");
                
    }
    
}

Program 7

package beans;

/**
 *
 * @author admin
 */
public class Sales extends Employee
{
    private int ta;
    private int ea;
    private int tfa;

    public Sales() {
    }

    public int getTa() {
        return ta;
    }

    public void setTa(int ta) {
        this.ta = ta;
    }

    public int getEa() {
        return ea;
    }

    public void setEa(int ea) {
        this.ea = ea;
    }

    public int getTfa() {
        return tfa;
    }

    public void setTfa(int tfa) {
        this.tfa = tfa;
    }
    
}

 

courses

TechVidvan Team

TechVidvan Team provides high-quality content & courses on AI, ML, Data Science, Data Engineering, Data Analytics, programming, Python, DSA, Android, Flutter, full stack web dev, MERN, and many latest technology.

Leave a Reply

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