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


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.


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.


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.


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.


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


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


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.

Related Posts

  • x86 vs x64
    25 July

    x86 vs x64 – The Complete Guide to the Best Architecture

    The comparison of x86 vs x64 in terms of CPU architecture isn't exactly a new rivalry, but what really differentiates them?

  • 22 July

    Manage C Sharp Homework Assignment at Ease Online

    Table of ContentsC Sharp Homework Help: Use the Service and Get Your BenefitsC Sharp Assignment Help: Sound Guarantees for the CustomersTop-Notch Authentic ContentPrivacy24/7 SupportPlace Your Order and Get the Task Down at Ease In this post, we will see how to manage C sharp homework assignment online. Who can do my c# assignment fast? The […]

  • Break out of nested loop in java
    22 July

    How to break out of nested loops in Java

    Table of ContentsUsing break (will break inner loop)Using named loopUsing named blockUsing returnConclusion In this post, we will see how to break out of nested loops in Java. Using break (will break inner loop) It is very important to understand how nested loops work to ensure that applying break will output the desired result. If […]

  • showbox alternatives
    22 July

    15 Best Showbox Alternatives (With Download Links) in 2021

    Table of ContentsBest Showbox Alternatives to Try in 2021CrackleMovie HDPlaybox HDMovie BoxMegaBox HDTubi TvHuluPopcorn TimeCinema BoxFreeFlixMobdroVisit Popcorn FlixBobby Movie BoxVisit Flipps HDMovcyFrequently Asked QuestionWhat is Showbox?What is the need for Showbox alternatives?Which are the best Showbox alternatives?Are there free Showbox alternatives?Final Words Were you having trouble streaming your favourite movies, which made you look for […]

  • Kimcartoon alternatives
    21 July

    KimCartoon alternatives -13 Best Websites Like KimCartoon in 2021

    Table of ContentsBest KimCartoon alternatives in 2021CartoonsOnToonovaKissAnimeToonGetWatch Cartoons OnlineCartoonCrazyAnimedaoAnimeToonCartoon ExtraEyeonanimeNyaaMasteraniToonJetFrequently Asked Questions Is KimCartoon safe in 2021?Is KimCartoon legal in 2021?Is there any working proxy of KimCartoon in 2021?Wrapping Up Are you worried about KimCartoon getting shut recently? Well, we’ve you covered. KimCartoon has been one of the best platforms for seamlessly streaming cartoons in HD. […]

  • Trim String in C++
    19 July

    Trim String in C++

    Table of ContentsUsing Boost string algorithms to trim string in C++Using find_first_not_of() and find_last_not_of()` to trim strings in C++Using find_if() to trim the string in C++Using stringstream to trim the strings in C++Using a customized function to trim the strings in C++Conclusion When we take an input from an user, strings can have unwanted whitespaces […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.