TABLE PER HIERARCHY BY USING XML
By this strategy we can map whole hierarchy by single table only,Here an extra column(also known as discriminitor column) is created in the table to identify class.
Lets understand the by problem first. I want to map the whole hierarchy given below into one table.
There are three class Employee is super class contract_employee and Regular_Employee are child
class.All class are represent same table.
Here are one mapping file;
name is 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.Employee" table="e100" discriminator-value="emp">
<id name="id">
<generator class="increment"></generator>
</id>
<discriminator type="string" column="type"></discriminator>
<property name="name"/>
<subclass name="beans.Contract_Emp" discriminator-value="comp">
<property name="pay_per_hour"/>
<property name="contract_period"/>
</subclass>
<subclass name="beans.Regular_Emp" discriminator-value="regmp">
<property name="salary"/>
<property name="bonus"/>
</subclass>
</class>
</hibernate-mapping>
Here one Configuration file
name is 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="resources/emp.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Persistent Super class
package beans;
public class Employee {
private int id;
private String name;
public Employee() {
}
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;
}
}
By this strategy we can map whole hierarchy by single table only,Here an extra column(also known as discriminitor column) is created in the table to identify class.
Lets understand the by problem first. I want to map the whole hierarchy given below into one table.
There are three class Employee is super class contract_employee and Regular_Employee are child
class.All class are represent same table.
Here are one mapping file;
name is 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.Employee" table="e100" discriminator-value="emp">
<id name="id">
<generator class="increment"></generator>
</id>
<discriminator type="string" column="type"></discriminator>
<property name="name"/>
<subclass name="beans.Contract_Emp" discriminator-value="comp">
<property name="pay_per_hour"/>
<property name="contract_period"/>
</subclass>
<subclass name="beans.Regular_Emp" discriminator-value="regmp">
<property name="salary"/>
<property name="bonus"/>
</subclass>
</class>
</hibernate-mapping>
Here one Configuration file
name is 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="resources/emp.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Persistent Super class
package beans;
public class Employee {
private int id;
private String name;
public Employee() {
}
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 Contract_emp
package beans;
public class Contract_Emp {
private int pay_per_hour;
private String contract_period;
public Contract_Emp() {
super();
}
public int getPay_per_hour() {
return pay_per_hour;
}
public void setPay_per_hour(int pay_per_hour) {
this.pay_per_hour = pay_per_hour;
}
public String getContract_period() {
return contract_period;
}
public void setContract_period(String contract_period) {
this.contract_period = contract_period;
}
}
Persistent Child class Regular_emp
package beans;
public class Regular_Emp {
private int salary;
private int bonus;
public Regular_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;
}
}
Persistent Test class
package test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import beans.Contract_Emp;
import beans.Employee;
import beans.Regular_Emp;
public class Test {
public static void main(String[] args) {
Configuration cfg=new Configuration();
cfg.configure("resources/configuration.cfg.xml");
SessionFactory sf=cfg.buildSessionFactory();
Session s=sf.openSession();
Transaction t=s.beginTransaction();
Employee employee=new Employee();
Contract_Emp contract_Emp=new Contract_Emp();
Regular_Emp regular_Emp=new Regular_Emp();
employee.setId(0);
employee.setName("lk10");
contract_Emp.setContract_period("2 year");
contract_Emp.setPay_per_hour(10);
regular_Emp.setBonus(3000);
regular_Emp.setSalary(2000);
t.commit();
s.persist(employee);
s.persist(contract_Emp);
s.persist(regular_Emp);
s.close();
}
}
No comments:
Post a Comment