Table Per Concreteclass Example Hibernate

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 hbm2ddl.auto 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: Employee.java

2. File Name: PermanentEmployee.java

3. File Name: TemporaryEmployee.java

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 – TablePerConcreteClass.java

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

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 Reply

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