Pages Navigation Menu

Coding is much easier than you think

Table per Concrete Class Example using XML – Hibernate4

Table per Concrete Class
 
In case of Table per concrete class, two tables for each subclass are created. The super class variables are placed in each subclass.
 
In this example, we are going to use hb2ddl.auto property to generate the table automatically. So we don’t need to worry about creating tables in the database.
 
Inheritance
 
The above is the Hierarchy of classes involved. Here Employee is the super class for PermanentEmployee and ContractEmployee classes. Now Let us create Java classes for the above hierarchy to implement
 

Model class

 
File: Employee.java

package model;

public class Employee {
	private int empID;
	private String empName;

	public int getEmpID() {
		return empID;
	}

	public String getEmpName() {
		return empName;
	}

	public void setEmpID(int empID) {
		this.empID = empID;
	}

	public void setEmpName(String empName) {
		this.empName = empName;
	}

}

 
File: PermanentEmployee.java

package model;

public class PermanentEmployee extends Employee {
	private String companyName;

	public String getCompanyName() {
		return companyName;
	}

	public void setCompanyName(String companyName) {
		this.companyName = companyName;
	}

}

 
File: ContractEmployee.java

package model;

public class ContractEmployee extends Employee {
	String contractorName;

	public String getContractorName() {
		return contractorName;
	}

	public void setContractorName(String contractorName) {
		this.contractorName = contractorName;
	}
}

 

Hibernate Mapping xml

 


	
		
			
		
		
		

		
			
		
		
		
			
		
		
	

 
The union-subclass sub element of class, specifies the subclass. It adds the columns of parent table into this table. In other words, it is working as a union.class.
 

Hibernate Configuration file

 



	
		
		oracle.jdbc.driver.OracleDriver
		system
		admin
		jdbc:oracle:thin:@127.0.0.1:1521:XE

		
		2

		
		org.hibernate.dialect.OracleDialect

		
		thread

		
		org.hibernate.cache.NoCacheProvider

		
		true

		
		create
		

	

 
The hbm2ddl.auto property is defined for creating automatic table in the database.
 

Client program

 
Now let us write one client program for three bean programs – Employee, PermanentEmployee, ContractEmployee
 

package util;

import model.ContractEmployee;
import model.PermanentEmployee;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class HibernateUtil {

	public static void main(String[] args) {

		Configuration cf = new Configuration().configure("hibernate.cfg.xml");

		StandardServiceRegistryBuilder srb = new StandardServiceRegistryBuilder();
		srb.applySettings(cf.getProperties());
		ServiceRegistry sr = srb.build();
		SessionFactory sf = cf.buildSessionFactory(sr);

		Session session = sf.openSession();

		PermanentEmployee p1 = new PermanentEmployee();
		p1.setEmpID(1);
		p1.setEmpName("Ameer");
		p1.setCompanyName("CTS");

		PermanentEmployee p2 = new PermanentEmployee();
		p2.setEmpID(2);
		p2.setEmpName("Lourde");
		p2.setCompanyName("TCS");

		// create two objects of ContractEmployee
		ContractEmployee t1 = new ContractEmployee();
		t1.setEmpID(3);
		t1.setEmpName("Prabhu");
		t1.setContractorName("ABD Consultancy");

		ContractEmployee t2 = new ContractEmployee();
		t2.setEmpID(4);
		t2.setEmpName("Badru");
		t2.setContractorName("MN Consultancy");

		Transaction tx = session.beginTransaction();

		session.save(p1);
		session.save(p2);
		session.save(t1);
		session.save(t2);

		tx.commit();
		System.out.println("Object saved successfully !");
		session.close();
		sf.close();
	}
}

 

Eclipse Console

 
Table per Concrete Class Example using XML - Hibernate4

Database output

 
P_EMPLOYEE2
 
T_Employee2
 

Leave a Comment

Your email address will not be published. Required fields are marked *