Hibernate Inheritence Table Per Hierarchy

In Table Per Hierarchy mapping, single table is required to map the whole hierarchy.An extra column called discriminator is added to table to identify the class.

Here I am taking example of User,Custom and Employee classes where User is parent class and Customer and Employee are its child classes.In parent class mapping we will define the and each class mapping will add an discriminator-value attribute to identify their values in database.

User.java

package com.expertwebinidia;
public class User {
	private int id;
	private String name;
	private String email;
	
	public User() {
		super();
	}
	public User(String name, String email) {
		super();
		this.name = name;
		this.email = email;
	}
	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;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}


}

Customer.java

package com.expertwebinidia;

public class Customer extends User{
	private String address1;
	private String address2;
	private String city;

	public Customer() {
		super();
	}
	public Customer(String name, String email,String address1, String address2, String city) {
		super(name,email);
		this.address1 = address1;
		this.address2 = address2;
		this.city = city;
	}
	public String getAddress1() {
		return address1;
	}
	public void setAddress1(String address1) {
		this.address1 = address1;
	}
	public String getAddress2() {
		return address2;
	}
	public void setAddress2(String address2) {
		this.address2 = address2;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
}
	

Employee.java

package com.expertwebinidia;
public class Employee extends User {
	private String department;
	private int salary;
	public Employee() {
		super();
	}
	public Employee(String name, String email, String department,int salary) {
		super(name, email);
		this.department = department;
		this.salary = salary;
	}
	public String getDepartment() {
		return department;
	}
	public void setDepartment(String department) {
		this.department = department;
	}
	public int getSalary() {
		return salary;
	}
	public void setSalary(int salary) {
		this.salary = salary;
	}
	
}

hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/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/hibernatetest</property>  
         <property name="connection.username">root</property>  
         <property name="connection.password"></property>  
         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>  
         <mapping resource="user.hbm.xml"/>  
     </session-factory>  
</hibernate-configuration>  

user.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 
 <hibernate-mapping>  
  <class name="com.startwithjava.User" table="users" discriminator-value="1">  
    <id name="id">  
     <generator class="increment"></generator>  
    </id>            
  <discriminator column="type" type="int"/>  
  <property name="name"/>     
  <property name="email"/> 
  <subclass name="com.startwithjava.Employee" discriminator-value="2">
	  <property name="department"/>     
	  <property name="salary" type="int"/> 
  </subclass> 
   <subclass name="com.startwithjava.Customer" discriminator-value="2">
	  <property name="address1"/>     
	  <property name="address2"/>   
	  <property name="city"/>   
  </subclass> 
  </class>           
 </hibernate-mapping>  

Leave a Reply