Table Per Concreteclass Example Hibernate

Table-per-concreteclass Hierarchy

Note: Before going through this application, it is advised to read Hibernate Mapping Tutorial and also Table per class hierarchy.

In this Table Per Concreteclass style, two tables for each subclass are created. The super class variables are placed in each subclass. Observe the screenshots at the end of the notes.

The three tables of Table-per-subclass do not exist. Here tables change (only two tables exist). Let create the required tables.

The same three programs of Table per class hierarchy Employee, PermanentEmployee and TemporaryEmployee are used and also the same client program is used. Configuration file does not change but mapping file changes.

Following is the Hierarchy of classes involved. Output screens are shown the end of this notes.

Table Per Concreteclass

Let us create Java classes for the above hierarchy.

1. File Name:

public class Employee {
  private short empId;
  private String empName;

  public Employee() {
    // TODO Auto-generated constructor stub
  public short getEmpId() {
    return empId;
  public void setEmpId(short empId) {
    this.empId = empId;
  public String getEmpName() {
    return empName;
  public void setEmpName(String empName) {
    this.empName = empName;

2. File Name:

public class PermanentEmployee extends Employee {
  private String designation;
  private String department;

  public PermanentEmployee() {
    // TODO Auto-generated constructor stub
  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;

3. File Name:

public class TemporaryEmployee extends Employee {
  private short workingDays;
  String contractorName;

  public TemporaryEmployee() {
    // TODO Auto-generated constructor stub
  public short getWorkingDays() {
    return workingDays;
  public void setWorkingDays(short workingDays) {
    this.workingDays = workingDays;
  public String getContractorName() {
    return contractorName;
  public void setContractorName(String contractorName) {
    this.contractorName = contractorName;

4. File Name: hibernate.cfg.xml



5. File Name: Employee.hbm.file


Observe, the element used in table-per-concrete class is <union-subclass>

6. Client Program –

import org.hibernate.*;
import org.hibernate.cfg.*;

public class TablePerConcreteClass 
  public static void main(String[] args) 
    Configuration cfg = new Configuration();
    SessionFactory sf = cfg.buildSessionFactory();
    Session session = sf.openSession();

    PermanentEmployee p1 = new PermanentEmployee();
    p1.setEmpName("S N Rao");
    PermanentEmployee p2 = new PermanentEmployee();

    TemporaryEmployee t1 = new TemporaryEmployee();
    TemporaryEmployee t2 = new TemporaryEmployee();
    Transaction tx = session.beginTransaction();;;;;

    tx.commit();   session.close();   sf.close();   
    System.out.println("Objects saved");

Table Per Concreteclass

Observe, for 4 records 4 times SQL statement is executed.

Table Per Concreteclass

Important observation

Important observation here is the super class Employee variables empId and empName are added to each subclass table. When they are added, obviously we do not require super class table. Here, no foreign key maintained. But in table-per-subclass, three tables existed and the primary key of super class is associated as foreign key in each subclass.

Limitations of Table Per Concreteclass strategy

The identity should not be used with union subclass inheritance. The primary key and other variables (like empName) of the super class have to be shared by all unioned subclasses in the hierarchy.

Another client program to retrieve records (same that of previous table-per-class hierarchy).

import java.util.List;  
import org.hibernate.Query;  
import org.hibernate.Session;  
import org.hibernate.SessionFactory;  
import org.hibernate.cfg.Configuration;

public class RetrievingRecords 
  public static void main(String[] args)
    Configuration c= new Configuration();
    SessionFactory sf=c.buildSessionFactory();
    Session s=sf.openSession();
    Query q = s.createQuery("FROM PermanentEmployee");
    List myList = q.list();
    for(PermanentEmployee p1 :myList) 
      System.out.println(p1.getEmpId()+ ", " + p1.getEmpName() + ", " + p1.getDesignation() + ", " + p1.getDepartment());

    Query q1 = s.createQuery("FROM TemporaryEmployee");
    List yourList = q1.list();
    for(TemporaryEmployee t1 : yourList)
      System.out.println(t1.getEmpId()+ ", " + t1.getEmpName() + ", " + t1.getWorkingDays() + ", " + t1.getContractorName());

Table Per Concreteclass
One question for you.

Can we use all three mapping in one program?

No, Hibernate does not mingle all <subclass>, <joined-subclass> and <union-subclass> mappings in one <class> element. However, it is permitted to combine <subclass> and <joined-subclass> elements in one <class>.

Leave a Comment

Your email address will not be published.