Table Per Subclass Example Hibernate

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 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:

2. File Name:

3. File Name:

4. File Name: hibernate.cfg.xml

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 –

Table Per Subclass

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


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).

Table Per Subclass

Leave a Reply

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