Table per Hierarchy using Annotations (Single Table)

Table per Hierarchy using Annotations (Single Table)
We have already seen an example for Table per hierarchy (here). In this article we are going to see how Table per Hierarchy can be implemented in Hibernate using Annotations.

pic1

In this diagram, Employee is a class and Mobile and LandLine classes extends employee class. When we insert the values in them and when we run, all the data will be accumulated in a Single table. This is known as Table per Hierarchy.

Lets see this example in Detail,

Structure
structure

employee.java

mobile.java

landline.java

NewHibernateUtil.java

hibernate.cfg.xml

execution.java

 

Output:
op1

From the output we can see that, we have used @Entity annotation in both mobile and landline classes but hibernate has created only one table Annotate_Employee and has inserted both the values of mobile and landline in single table

op2

There is one column – DTYPE that differentiates the classes and the data

 

 

 

 

One thought on “Table per Hierarchy using Annotations (Single Table)

Leave a Reply

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