Table Per Concrete by using xml file
In the table per concrete we create table for each class super as well as child class with the help of mapping file.
some of the steps we follow to create tpc .
In the table per concrete we create table for each class super as well as child class with the help of mapping file.
some of the steps we follow to create tpc .
- Create persistent class as well as child class
- Create mapping file and provide connection between child by using union_subclass
- create configuration file
- create test class
- and run it .
- persistent super class as Emp
package beans;
public class Emp {
private int id;
private String name;
public Emp() {
}
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;
}
}
persistent child class as Con_Emp
package beans;
public class Con_Emp extends Emp{
private int pey_per_hour;
private String Contract_duration;
public Con_Emp() {
super();
}
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) {
Contract_duration = contract_duration;
}
}
persistent child class as Reg_Emp
package beans;
public class Reg_emp extends Emp {
private int salary;
private int bonus;
public Reg_emp() {
super();
}
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;
}
}
public class Emp {
private int id;
private String name;
public Emp() {
}
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;
}
}
persistent child class as Con_Emp
package beans;
public class Con_Emp extends Emp{
private int pey_per_hour;
private String Contract_duration;
public Con_Emp() {
super();
}
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) {
Contract_duration = contract_duration;
}
}
persistent child class as Reg_Emp
package beans;
public class Reg_emp extends Emp {
private int salary;
private int bonus;
public Reg_emp() {
super();
}
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;
}
}
mapping file as emp.hibernate.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="tpc">
<id name="id">
<generator class="increment"/>
</id>
<discriminator type="string" column="type"/>
<property name="name"/>
</class>
<union-subclass name="beans.Con_Emp" table="tpc_con" >
<property name="pey_per_hour"/>
<property name="Contract_duration"/>
</union-subclass>
<union-subclass name="beans.Reg_emp" table="tpc_reg">
<property name="salary"/>
<property name="bonus"/>
</union-subclass>
</hibernate-mapping>
Configuration file as configuration.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="hbm2ddl.auto">create</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>
No comments:
Post a Comment