Hibernate Inheritance Table per Class Hierarchy using JPA Annotations

An simple tutorial on Hibernate Inheritance Table per Class Hierarchy using JPA Annotations.

1. Introduction

Java is an Object Oriented Language and supports Inheritance which is one of the most visible paradigms of Object-Relational mismatch. Object Oriented Systems can model both “is a”and “has a” relationship whereas Relational Model supports only “has a” relationship between two entities. Hibernate can help us map such Objects with relational tables.

Basically there are three ways in which we can implement Inheritance in Hibernate. We shall use one of the ways here Table per Class Hierarchy.

2. Example

Consider the below Object Model where in we have Base Class Payment and its subclasses OnlineTransfer, ChequePayment, CashPayment which are basically specific forms of Payments.

Class Diagram

Class Diagram

3. Database Table Design

In Table per Class Hierarchy method, we save the entire Class hierarchy in a single Database table. A discriminator is a key to uniquely identify the Class type of the Class hierarchy. The Table Design would look like this for the above Class Diagram.

Table Design

Table Design

As we mentioned earlier only one Table i.e. PAYMENT is created having all the attributes of subclasses also.

4. Project Setup

Lets create a simple Maven project with the below pom.xml

In the above pom.xml we added dependencies for Hibernate 3.6.10, MySQL Connector 5.1.25,  Javassist and Sl4J.

5. Database Table Creation

6. JPA Entity Classes

Lets us create a JPA Entity  Class for Payment.java, CashPayment.java, ChequePayment.java, OnlineTransfer.java

@Inheritance:

In JPA for implementing Inheritance @Inheritance annotation is used.It defines inheritance strategy to be implement for entity class hierarchy.For one table per class hierarchy,we have used SINGLE_TABLE as inheritance strategy.This annotation is defined at Root Class where different strategy is to be applied.

@DiscriminatorColumn:

This annotation is used to define discriminator column for SINGLE_TABLE and joined strategy.It is used to distinguish between different class instances.This annotation is defined at Root Class and Sub Class where inheritance Strategy is to be applied. If @DiscriminatorColumn annotation is not specified,then hibernate will create a column named as ‘DType’ and DiscriminatorType will be string.

@DiscriminatorValue:

This annotation defines value in discriminator column for that class.For example,If Entity will be of Payment instance, in PAYMENT Table then ‘OTHERS’ will be value for that row in discriminator column. If Entity of CashPayment is used then “CASH” will be used for this column and so on for others.

7. JPA Configuration

Below is the JPA Configuration File persistence.xml

7. Main Program

8. Output

Output

Output

From the above Console Output we could see that irrespective of whatever class we persist all the data is inserted into PAYMENT Table.

Conclusion

Conclusion

9. Project Structure

Project Structure

Project Structure

Download JPA Inheritance Table per Class Hierarchy Example

I hope this has been useful for you and I’d like to thank you for reading. If you like this article, please leave a helpful comment and share it with your friends.

Leave a Reply

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