Hibernate Table Per Hierarchy using xml file
Inheritance Mapping
Hibernate Table Per Hierarchy using xml file
By this inheritance strategy, we can map the whole hierarchy by single table only. Here, an extra column (also known as discriminator column) is created in the table to identify the class.
Let's understand the problem first.
Employee.java
public class Employee {
private int id;
private String name;
//generate getters and setters
}
Regular_Employee.java
public class Regular_Employee extends Employee {
private float salary;
private int bonus;
//generate getters and setters
}
Contract_Employee.java
public class Contract_Employee extends Employee {
private float pay_per_hour;
private String contract_duration;
//generate getters and setters
}
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>
<!-- Database connection settings -->
<property name="hbm2ddl.auto">update</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
employee.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="com.smgc.Employee" table="emp121"
discriminator-value="emp">
<id name="id">
<generator class="increment"></generator>
</id>
<discriminator column="type" type="string"></discriminator>
<property name="name"></property>
<subclass name="com.smgc.Regular_Employee"
discriminator-value="reg_emp">
<property name="salary"></property>
<property name="bonus"></property>
</subclass>
<subclass name="com.smgc.Contract_Employee"
discriminator-value="con_emp">
<property name="pay_per_hour"></property>
<property name="contract_duration"></property>
</subclass>
</class>
</hibernate-mapping>
StoreData.java
package com.smgc;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
public class StoreData {
public static void main(String[] args) {
// TODO Auto-generated method stub
AnnotationConfiguration cfg = new AnnotationConfiguration();
Session session = cfg.configure("hibernate.cfg.xml")
.buildSessionFactory().openSession();
Transaction t = session.beginTransaction();
Employee e1 = new Employee();
e1.setName("Piyush Patel");
Regular_Employee e2 = new Regular_Employee();
e2.setName("Vivek Kumar");
e2.setSalary(50000);
e2.setBonus(5);
Contract_Employee e3 = new Contract_Employee();
e3.setName("Arjun Kumar");
e3.setPay_per_hour(1111);
e3.setContract_duration("15 hours");
session.persist(e1);
session.persist(e2);
session.persist(e3);
t.commit();
session.close();
System.out.println("success");
}
}
success.
see table in database.
Comments
Post a Comment