Hibernate Inheritance Table Per subClass Hierarchy

,In Table Per Subclass mapping,we need tables for each classes.Their primary key-foreign key relation between tables.An additional element is used in class mapping.Primary key of parent class is saved in child class tables as foreign key. is used to add primary key of parent class table to child class table as foreign key.

Here I am taking example of User,Custom and Employee classes where User is parent class and Customer and Employee are its child classes.

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="PkFk.hbm.xml"/>  
     </session-factory>  
</hibernate-configuration>  

PkFk.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"/> 
  <joined-subclass name="com.startwithjava.Employee" table="employee">
           <key column="userId" />
	  <property name="department"/>     
	  <property name="salary" type="int"/> 
  </joined-subclass> 
   <joined-subclass name="com.startwithjava.Customer" table="customer">
          <key column="userId" />
	  <property name="address1"/>     
	  <property name="address2"/>   
	  <property name="city"/>   
  </joined-subclass> 
  </class>           
 </hibernate-mapping>  

Leave a Reply