Followers

hb : Table Per Class (TPC) using XMl

Employee.java
=============

package mypack;

public class Employee {
    private int empId;
    private String empName;
    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;
    }
}


ContractEmployee.java
=====================

package mypack;

public class ContractEmployee extends Employee{
    private int pph;
    private String contractDuration;
    public ContractEmployee(){
    
    }

    public int getPph() {
        return pph;
    }

    public void setPph(int pph) {
        this.pph = pph;
    }

    public String getContractDuration() {
        return contractDuration;
    }

    public void setContractDuration(String contractDuration) {
        this.contractDuration = contractDuration;
    }
}



RegularEmployee.java
=====================
package mypack;
public class RegularEmployee extends Employee{
    private float salary;
    private int bonus;
    public RegularEmployee(){
    
    }

    public float getSalary() {
        return salary;
    }

    public void setSalary(float salary) {
        this.salary = salary;
    }

    public int getBonus() {
        return bonus;
    }

    public void setBonus(int bonus) {
        this.bonus = bonus;
    }
}

tpcemployee.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="mypack.Employee"  >
        <id name="empId" column="EMPID" >
            <generator class="increment" />
        </id>
        <property name="empName" column="EMPNAME" ></property>
        <union-subclass name="mypack.RegularEmployee" table="TPCREGEMPX" >
            <property name="bonus" column="BONUS" ></property>
            <property name="salary" column="SALARY" ></property>
        </union-subclass>
        <union-subclass name="mypack.ContractEmployee" table="TPCCONEMPX" >
            <property name="pph" column="PPH" ></property>
            <property name="contractDuration" column="DURATION" ></property>
        </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.DerbyDialect</property>
    <property name="hibernate.connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
    <property name="hibernate.connection.url">jdbc:derby://localhost:1527/tpc</property>
    <property name="hibernate.connection.username">app</property>
    <property name="hibernate.connection.password">app</property>
    <property name="show_sql">true</property>
    <property name="hbm2ddl.auto">create</property>
    <mapping resource="tpcemployee.hbm.xml"/>
</session-factory>
</hibernate-configuration>


Test.java
==========
package mypack;
import org.hibernate.*;
import org.hibernate.cfg.*;

public class Test {
    public static void main(String[] args){
    SessionFactory factory = new Configuration().configure().buildSessionFactory();
    Session session = factory.openSession();
    Transaction tx = session.beginTransaction();
    Employee emp = new Employee();
    RegularEmployee re = new RegularEmployee();
    ContractEmployee ce = new ContractEmployee();
   // emp.setEmpId(111);
    emp.setEmpName("Employee");
    re.setEmpName("REmployee");
   // re.setEmpId(222);
    re.setBonus(123);
    re.setSalary(1200f);
  //  ce.setEmpId(33);
    ce.setContractDuration("12 hrs");
    ce.setEmpName("CEmployee");
    ce.setPph(10);
    
    session.persist(emp);
    session.persist(ce);
    session.persist(re);
    tx.commit();
    session.close();
    System.out.println("Records updated successfully...");
    
    }
    
}

No comments:

Post a Comment