Hibernate Table Per Hierarchy using Annotation

In the past page, we have mapped the inheritance hierarchy of importance with one table utilizing xml record. Here, we will play out this undertaking utilizing annotation. You have to utilize @Inheritance(strategy=InheritanceType.SINGLE_TABLE), @DiscriminatorColumn and @DiscriminatorValue explanations for mapping table per pecking hierarchy strategy.

In instance of table per hierarchy of importance, just a single table is required to delineate inheritance progressive system. Here, an additional section (otherwise called discriminator column) is made in the table to recognize the class.

Let's see the inheritance hierarchy:

There are three classes in this progression. Representative is the super class for Regular_Employee and Contract_Employee classes.

Example of Hibernate Table Per Hierarchy using Annotation

You need to follow the following steps to create simple example:

  • Create the persistent classes
  • Edit pom.xml file
  • Create the configuration file
  • Create the class to store the fetch the data

1) Create the Persistent classes

You need to create the persistent classes representing the inheritance. Let's create the three classes for the above hierarchy:

File: Employee.java
  1. package com.javacodegeeks.mypackage;  
  2. import javax.persistence.*;  
  3.   
  4. @Entity  
  5. @Table(name = "employee101")  
  6. @Inheritance(strategy=InheritanceType.SINGLE_TABLE)  
  7. @DiscriminatorColumn(name="type",discriminatorType=DiscriminatorType.STRING)  
  8. @DiscriminatorValue(value="employee")  
  9.   
  10. public class Employee {  
  11. @Id  
  12. @GeneratedValue(strategy=GenerationType.AUTO)  
  13.       
  14. @Column(name = "id")  
  15. private int id;  
  16.   
  17. @Column(name = "name")  
  18. private String name;  
  19.   
  20. //setters and getters  
  21. }  
File: Regular_Employee.java
  1. package com.javacodegeeks.mypackage;  
  2.   
  3. import javax.persistence.*;  
  4.   
  5. @Entity  
  6. @DiscriminatorValue("regularemployee")  
  7. public class Regular_Employee extends Employee{  
  8.       
  9. @Column(name="salary")    
  10. private float salary;  
  11.   
  12. @Column(name="bonus")     
  13. private int bonus;  
  14.   
  15. //setters and getters  
  16. }  
File: Contract_Employee.java
  1. package com.javacodegeeks.mypackage;  
  2.   
  3. import javax.persistence.Column;  
  4. import javax.persistence.DiscriminatorValue;  
  5. import javax.persistence.Entity;  
  6.   
  7. @Entity  
  8. @DiscriminatorValue("contractemployee")  
  9. public class Contract_Employee extends Employee{  
  10.       
  11.     @Column(name="pay_per_hour")  
  12.     private float pay_per_hour;  
  13.       
  14.     @Column(name="contract_duration")  
  15.     private String contract_duration;  
  16.   
  17.     //setters and getters  
  18. }  

2) Add project information and configuration in pom.xml file.

Open pom.xml file and click source. Now, add the below dependencies between <dependencies>....</dependencies> tag.

  1. <dependency>  
  2.     <groupId>org.hibernate</groupId>  
  3.     <artifactId>hibernate-core</artifactId>  
  4.     <version>5.3.1.Final</version>  
  5. </dependency>  
  6.       
  7. <dependency>  
  8.     <groupId>com.oracle</groupId>  
  9.     <artifactId>ojdbc14</artifactId>  
  10.     <version>10.2.0.4.0</version>  
  11. </dependency>  

3) Add the persistent classes in configuration file

The table structure for this progression is as appeared
Open the hibernate.cgf.xml file, and add entries of entity classes like this:
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE hibernate-configuration PUBLIC  
  3.         "-//Hibernate/Hibernate Configuration DTD 5.3//EN"  
  4.         "http://www.hibernate.org/dtd/hibernate-configuration-5.3.dtd">  
  5. <hibernate-configuration>  
  6.     <session-factory>  
  7.             
  8.     <property name="hbm2ddl.auto">update</property>    
  9.         <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>    
  10.         <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>    
  11.         <property name="connection.username">system</property>    
  12.         <property name="connection.password">jtp</property>    
  13.         <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>   
  14.        
  15.         <mapping class="com.javacodegeeks.mypackage.Employee"/>  
  16.         <mapping class="com.javacodegeeks.mypackage.Regular_Employee"/>  
  17.         <mapping class="com.javacodegeeks.mypackage.Contract_Employee"/>  
  18.           
  19.     </session-factory>  
  20. </hibernate-configuration>  

The hbm2ddl.auto property is defined for creating automatic table in the database.


4) Create the class that stores the persistent object

In this class, we are simply storing the employee objects in the database.

File: StoreTest.java
  1. package com.javacodegeeks.mypackage;  
  2.   
  3. import org.hibernate.Session;  
  4. import org.hibernate.SessionFactory;  
  5. import org.hibernate.Transaction;  
  6. import org.hibernate.boot.Metadata;  
  7. import org.hibernate.boot.MetadataSources;  
  8. import org.hibernate.boot.registry.StandardServiceRegistry;  
  9. import org.hibernate.boot.registry.StandardServiceRegistryBuilder;  
  10.   
  11. public class StoreTest {  
  12.   
  13.     public static void main(String args[])  
  14.     {  
  15.         StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();  
  16.         Metadata meta = new MetadataSources(ssr).getMetadataBuilder().build();  
  17.           
  18.         SessionFactory factory=meta.getSessionFactoryBuilder().build();  
  19.         Session session=factory.openSession();  
  20.           
  21.          Transaction t=session.beginTransaction();    
  22.             
  23.             Employee e1=new Employee();    
  24.             e1.setName("Gaurav Chawla");    
  25.                 
  26.             Regular_Employee e2=new Regular_Employee();    
  27.             e2.setName("Vivek Kumar");    
  28.             e2.setSalary(50000);    
  29.             e2.setBonus(5);    
  30.                 
  31.             Contract_Employee e3=new Contract_Employee();    
  32.             e3.setName("Arjun Kumar");    
  33.             e3.setPay_per_hour(1000);    
  34.             e3.setContract_duration("15 hours");    
  35.                 
  36.             session.persist(e1);    
  37.             session.persist(e2);    
  38.             session.persist(e3);    
  39.                 
  40.             t.commit();    
  41.             session.close();    
  42.             System.out.println("success");        
  43.     }  
  44. }  






javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition


python