Hibernate Inheritance: Table Per Class Hierarchy
One table per class hierarchy
Suppose we have following class hierarchy.We have Employee class as base class and permanentEmployee and ContractEmployee as derived class. permanentEmployee and ContractEmployee inherit from Employee class.
One table per class hierarchy
Suppose we have following class hierarchy.We have Employee class as base class and permanentEmployee and ContractEmployee as derived class. permanentEmployee and ContractEmployee inherit from Employee class.
In table per class hierarchy, only one table is created with columns to accommodate all the properties of each class involved in hierarchy. Because all the records of each class are available in a single table, to differentiate which record belongs to which class, we create one extra column known as discriminator column. Each class identified by some name (alias name) and this name is stored in the discriminator column.
In table per class hierarchy,One table will be created for above hierarchy.i.e. EMPLOYEE table will be created having following structure.
EMPLOYEE -------------- EMP_ID EMP_NAME DESIGNATION DEPARTMENT WORKING_DAYS CONTRACTOR_NAME
Let us create Java classes for the above hierarchy.
Employee.javapackage com.jwt.hibernate; public class Employee { private int empId; private String empName; 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; } }
PermanentEmployee.java:
package com.jwt.hibernate; public class PermanentEmployee extends Employee { private String designation; private String department; public String getDesignation() { return designation; } public void setDesignation(String designation) { this.designation = designation; } public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } }
ContractEmployee.java:
package com.jwt.hibernate; public class ContractEmployee extends Employee { private int workingDays; private String contractorName; public int getWorkingDays() { return workingDays; } public void setWorkingDays(int workingDays) { this.workingDays = workingDays; } public String getContractorName() { return contractorName; } public void setContractorName(String contractorName) { this.contractorName = contractorName; } }
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/jwt</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">mukesh</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <property name="hbm2ddl.auto">create </property> <mapping resource="com/jwt/hibernate/employee.hbm.xml" /> </session-factory> </hibernate-configuration>
<?xml version="1.0"?> <!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.jwt.hibernate.Employee" table="EMPLOYEE"> <id name="empId" column="EMP_ID" /> <discriminator column="DIS_COL" type="string" length="10" /> <property name="empName" column="EMP_NAME" length="50" /> <subclass name="com.jwt.hibernate.PermanentEmployee" discriminator-value="PE"> <property name="designation" column="DESIGNATION" length="50" /> <property name="department" column="DEPARTMENT" length="50" /> </subclass> <subclass name="com.jwt.hibernate.ContractEmployee" discriminator-value="CE"> <property name="workingDays" column="WORKING_DAYS" /> <property name="contractorName" column="CONTRACTOR_NAME" length="50" /> </subclass> </class> </hibernate-mapping>
Note that we have defined only one hibernate mapping (hbm) file employee.hbm.xml. All classes are defined within one hbm file.
<discriminator> tag is used to define the discriminator column.
<subclass> tag is used to map the subclass ContractEmployee and PermanentEmployee.
The discriminator-value for PermanentEmployee is defined as “PE” and that for ContractEmployee is defined “CE”, Thus, when Hibernate will persist the data for PermanentEmployee or ContractEmployee it will accordingly populate this value.
Now create a Test class for the purpose of inserting the data into the DB
Test.java:package com.jwt.hibernate; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; public class Test { public static void main(String[] args) { Configuration cfg = new Configuration(); cfg.configure("hibernate.cfg.xml"); SessionFactory sf = cfg.buildSessionFactory(); Session session = sf.openSession(); // create two objects of PermanentEmployee PermanentEmployee p1 = new PermanentEmployee(); p1.setEmpId(34326); p1.setEmpName("Mukesh Kumar"); p1.setDesignation("SE"); p1.setDepartment("RLT"); PermanentEmployee p2 = new PermanentEmployee(); p2.setEmpId(1894); p2.setEmpName("Neil Raj"); p2.setDesignation("Doctor"); p2.setDepartment("Surgery"); // create two objects of ContractEmployee c1 = new ContractEmployee(); c1.setEmpId(1234); c1.setEmpName("Vidya Rnjan"); c1.setWorkingDays(28); c1.setContractorName("Naveen"); ContractEmployee c2 = new ContractEmployee(); c2.setEmpId(1567); c2.setEmpName("Suman"); c2.setWorkingDays(22); c2.setContractorName("Divya"); // now save all four objects Transaction tx = session.beginTransaction(); session.save(p1); session.save(c1); session.save(p2); session.save(c2); tx.commit(); session.close(); sf.close(); System.out.println("Objects saved"); } }
Directory Structure of the Project
Output
When you run it,you will get following output in the eclipse console.
SQL Output
You can download the source code of the example by clicking on the Download link below.
Source : Download |
Source + Lib : Download |