=============
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;
}
}
tphemployee.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" table="TPH2EMPLOYEE" discriminator-value="Emp" >
<id name="empId" >
</id>
<discriminator column="type" type="string" ></discriminator>
<property name="empName" />
<subclass name="mypack.RegularEmployee" discriminator-value="Reg" >
<property name="bonus" />
<property name="salary" />
</subclass>
<subclass name="mypack.ContractEmployee" discriminator-value="Con" >
<property name="pph" />
<property name="contractDuration" />
</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="tphemployee.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