Followers

hb : Table Per Sub Class using Annotation(TPS)

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

package mypack;

import javax.persistence.Entity;
import javax.persistence.*;

@Entity
@Table(name = "emp1")
@Inheritance(strategy = InheritanceType.JOINED)
public class Employee {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO) //we can leave ID generation to HB/Developeer can take control.
    @Column(name = "ID1")
    private int empId;
    @Column(name = "NAME")
    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;

import javax.persistence.*;

@Entity
@Table(name = "empc1")
@PrimaryKeyJoinColumn(name = "ID")
public class ContractEmployee extends Employee{
    @Column(name = "PPH")
    private int pph;
    @Column(name = "cduration")
    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;

import javax.persistence.*;

@Entity
@Table(name = "empr1")
@PrimaryKeyJoinColumn(name = "ID")
public class RegularEmployee extends Employee{
    @Column(name = "SALARY")
    private float salary;
    @Column(name = "BONUS")
    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;
    }   
    
}



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/tpsxml</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 class="mypack.ContractEmployee" />
    <mapping class="mypack.Employee" />
    <mapping class="mypack.RegularEmployee" />
    
  </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 AnnotationConfiguration().configure().buildSessionFactory();
    Session session = factory.openSession();
    Transaction tx = session.beginTransaction();
    Employee emp = new Employee();
    RegularEmployee re = new RegularEmployee();
    ContractEmployee ce = new ContractEmployee();
    emp.setEmpName("Employee");
   // emp.setEmpId(111);
   // re.setEmpId(222);
    re.setEmpName("REmployee");
    re.setBonus(123);
    re.setSalary(1200f);
   // ce.setEmpId(3333);
    ce.setContractDuration("12 hrs");
    ce.setEmpName("CEmployee");
    ce.setPph(10);
    session.persist(emp);
    session.persist(ce);
    session.persist(re);
    tx.commit();
    System.out.println("Records updated successfully...");
    
    }
    
}

No comments:

Post a Comment