In case of Table Per Subclass, subclass mapped tables are related to parent class mapped table by primary key and foreign key relationship.
POJO CLASS
public class Emp {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
CHILD CLASS
package beans;
public class Con_Emp extends Emp{
private int pey_per_hour;
private String contract_duration;
public int getPey_per_hour() {
return pey_per_hour;
}
public void setPey_per_hour(int pey_per_hour) {
this.pey_per_hour = pey_per_hour;
}
public String getContract_duration() {
return contract_duration;
}
public void setContract_duration(String contract_duration) {
this.contract_duration = contract_duration;
}
}
POJO CLASS
public class Emp {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
CHILD CLASS
package beans;
public class Con_Emp extends Emp{
private int pey_per_hour;
private String contract_duration;
public int getPey_per_hour() {
return pey_per_hour;
}
public void setPey_per_hour(int pey_per_hour) {
this.pey_per_hour = pey_per_hour;
}
public String getContract_duration() {
return contract_duration;
}
public void setContract_duration(String contract_duration) {
this.contract_duration = contract_duration;
}
}
CHILD CLASS
package beans;
public class Reg_Emp extends Emp{
private int salary;
private int bonus;
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public int getBonus() {
return bonus;
}
public void setBonus(int bonus) {
this.bonus = bonus;
}
}
emp.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="beans.Emp" table="tps1">
<id name="id">
<generator class="increment"/></id>
<property name="name"/>
</class>
<joined-subclass name="beans.Con_Emp" table="tps2">
<key column="eid"/>
<property name="pey_per_hour"/>
<property name="contract_duration"></property>
</joined-subclass>
<joined-subclass name="beans.Reg_Emp" table="tps3">
<key column="eid"/>
<property name="bonus"/>
<property name="salary"/>
</joined-subclass>
</hibernate-mapping>
Configuration file
<?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="hbm2ddl.auto">update</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.url">jdbc:mysql://localhost:3306/db</property>
<property name="connection.username">root</property>
<property name="connection.password">lk@9616918397y</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<mapping resource="rsc/emp.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Test class
package test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import beans.Con_Emp;
import beans.Emp;
import beans.Reg_Emp;
public class Test {
public static void main(String[] args) {
Configuration cfg=new Configuration();
cfg.configure("rsc/configuration.cfg.xml");
SessionFactory sf=cfg.buildSessionFactory();
Session s=sf.openSession();
Transaction t=s.beginTransaction();
Emp employee=new Emp();
Con_Emp con=new Con_Emp();
Reg_Emp reg=new Reg_Emp();
employee.setId(1);
employee.setName("lk");
con.setId(2);
con.setName("bjvb");
con.setContract_duration("3 years");
con.setPey_per_hour(23);
reg.setId(3);
reg.setName("dbfjubgv");
reg.setBonus(2000);
reg.setSalary(4000);
t.commit();
s.close();
System.out.println("all is right");
}
}
No comments:
Post a Comment