Hibernate inheritance:Table per class hierarchy

This is 6 of 8 parts of tutorial series

Tutorial Content:

we will see how to implement inheritance in hibernate.There are 3 ways in which you can implement inheritance in hibernate.In this post,we will see one of them i.e.one table per class hierarchy.

Inheritance in hibernate:

Java is object oriented language and inheritance is one of main functionalities of java.Relation model can implement “is a” and “has a” relationship but hibernate provides us way to implement class hierarchy in a different ways.

One table per class hierarchy:

Lets say we have following class hierarchy.We have shape class as base class and Rectangle and Circle inherit from Shape class.

In one table per class hierarchy,One table will be created for above hierarchy.i.e. SHAPE table will be created having following structure.

As you can see only one table(SHAPE) is created having attributes of subclasses also.
As per our above class diagram,we will create three classes-Shape.java,Rectangle.java and Circle.java

1.Shape.java

This is our root class of entity class hierarchy.
Create Shape.java in src->org.arpit.javapostsforlearning.

Shape is our root class so some annotations needs to be used with root class for implementing inheritance.

@Inheritance:

For implementing inheritance in hiberante,@Inheritance annotation is used.It defines inheritance strategy to be implement for entity class hierarchy.For one table per class hierarhcy,we have used Single_Table as inheritance strategy.This annotation is defined at root level or sub hierarchy level 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 level or sub hierarchy level where different 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.This can only be applied on entity concrete class.For example,If entry will be of shape instance in SHAPE table then “s” will be value for that row in discriminator column.If this annotation is not specified and Discriminator column is used then provider specific values will be provided and if Discriminator type is String then discriminator value will be entity name.Discriminator value,if not defaulted need to specified on each enitity in hierarchy.

2.Rectangle.java

This is our child class.
Create Rectangle.java in src->org.arpit.javapostsforlearning.

3.Circle.java

This is our second child class.
Create Circle .java in src->org.arpit.javapostsforlearning.

4.Hiberante.cfg.xml:

Create a file named “hibernate.cfg.xml” in src folder.

5.Main Class:

Project structure:

6.Run it:

When you run it,you will get following output.

7.SQL output:

SHAPE table in database.

Was this post helpful?

Leave a Reply

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