Inheritance: Table Per Concrete Class Hierarchy

Table Per Concrete class Hierarchy :

Once we save the derived class object, then derived class data and base class data will be saved in the derived class related table in the database

hibernate-inheritance-2

In case of Table Per Concrete class, there will be changes in the mapping file.
In this we would require table for child classes, but not for the parent class

In table per concrete class hierarchy, number of subclasses is equals to number of tables required(example two sub-class required two tables).

x number of derived classes = x number of tables in the database

One new element <union-subclass — >under <class —> will be introduced in the mapping file.

Advantages

This is the easiest method of Inheritance mapping to implement.

Disadvantages

  • Data that belongs to a parent class is scattered across a number of subclass tables, which represents concrete classes.
  • Changes to a parent class is reflected to large number of tables.

Lets see an example :

Required files_

  • Payment.java (Base class)
  • CreditCard.java (Derived class)
  • Cheque.java (Derived class)
  • Client.java (Persistence  logic)
  • Payment.hbm.xml
  • hibernate.cfg.xml

Payment.java:

CreditCard.java:

Cheque.java:

Client.java

Payment.hbm.xml:

hibernate.cfg.xml:


   

Comments are closed