Table Per Subclass Example Hibernate

Table Per Subclass 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 Subclass style, three tables are created one for each Java program. Here, foreign key is maintained between the tables.

The three tables ETABLE (for Employee), PETABLE (for PermanentEmployee) and TETABLE (for TemporaryEmployee) do not exist. Let hbm2ddl.auto create it.

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 at the end of this notes.

Table Per Subclass

Let us create Java classes for the above Table Per Subclass hierarchy.

1. File Name: Employee.java

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: PermanentEmployee.java

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: TemporaryEmployee.java

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






  org.hibernate.dialect.Oracle9Dialect
  jdbc:oracle:thin:@localhost:1521:orcl1
  scott
  tiger
  oracle.jdbc.driver.OracleDriver
  SNRaoConnection
  update
  true

  



5. File Name: Employee.hbm.file





    
      
        
      
      

    
      
      
      
    

    
      
      
      
    
  


<key column="PID"/>

<key> element always represents a foreign key always in mapping table. It means that column PID of permanent employee is the foreign key of the primary key empId of Employee (<id name="empId" type="short">). This is the way how Hibernate reads the mapping file. Similarly with <key column="TID"/> also.

6. Client Program – TablePerSubClass.java

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

public class TablePerSubClass 
{
  public static void main(String[] args) 
  {
    Configuration cfg = new Configuration();
    cfg.configure("hibernate.cfg.xml"); 
		 
    SessionFactory sf = cfg.buildSessionFactory();
    Session session = sf.openSession();

    PermanentEmployee p1 = new PermanentEmployee();
    p1.setEmpId((short)100);
    p1.setEmpName("S N Rao");
    p1.setDesignation("Chemist");
    p1.setDepartment("Drugs");
	        
    PermanentEmployee p2 = new PermanentEmployee();
    p2.setEmpId((short)101);
    p2.setEmpName("Sridhar");
    p2.setDesignation("Foreman");
    p2.setDepartment("Chemicals");

    TemporaryEmployee t1 = new TemporaryEmployee();
    t1.setEmpId((short)102);
    t1.setEmpName("Jyostna");
    t1.setWorkingDays((short)28);
    t1.setContractorName("Raju");
		        
    TemporaryEmployee t2 = new TemporaryEmployee();
    t2.setEmpId((short)103);
    t2.setEmpName("Jyothi");
    t2.setWorkingDays((short)22);
    t2.setContractorName("Pratap");
     		        
    Transaction tx = session.beginTransaction();
    session.save(p1);  session.save(t1);  
    session.save(p2);  session.save(t2);

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

Table Per Subclass

Observe, for total 8 records in three tables, 8 times database is hit.

image

Three tables are required. The two subclass tables have foreign key associations (<key column="PID" />) to the super class table and we can say the relational model is actually a one-to-one association. We get relational models later.

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();
    c.configure("/hibernate.cfg.xml");
    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 Subclass

Leave a Comment

Your email address will not be published.