Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Table Per Hierarchy using Annotation


In the previous page, we have mapped the inheritance hierarchy with one table only using xml file. Here, we are going to perform this task using annotation. You need to use @Inheritance(strategy=InheritanceType.SINGLE_TABLE), @DiscriminatorColumn and @DiscriminatorValue annotations for mapping table per hierarchy strategy.

In case of table per hierarchy, only one table is required to map the inheritance hierarchy. Here, an extra column (also known as discriminator column) is created in the table to identify the class.

Let’s see the inheritance hierarchy:

inheritance1

There are three classes in this hierarchy. Employee is the super class for Regular_Employee and Contract_Employee classes.

The table structure for this hierarchy is as shown below:

inher1

 

Example of Hibernate Table Per Hierarchy using Annotation

You need to follow following steps to create simple example:

  • Create the persistent classes
  • 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.Kreationnext.mypackage;
  2. import javax.persistence.*;
  3. @Entity
  4. @Table(name = “employee101”)
  5. @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
  6. @DiscriminatorColumn(name=“type”,discriminatorType=DiscriminatorType.STRING)
  7. @DiscriminatorValue(value=“employee”)
  8. public class Employee {
  9. @Id
  10. @GeneratedValue(strategy=GenerationType.AUTO)
  11. @Column(name = “id”)
  12. private int id;
  13. @Column(name = “name”)
  14. private String name;
  15. //setters and getters
  16. }

File: Regular_Employee.java

  1. package com.Kreationnext.mypackage;
  2. import javax.persistence.*;
  3. @Entity
  4. @DiscriminatorValue(“regularemployee”)
  5. public class Regular_Employee extends Employee{
  6. @Column(name=“salary”)
  7. private float salary;
  8. @Column(name=“bonus”)
  9. private int bonus;
  10. //setters and getters
  11. }

File: Contract_Employee.java

  1. package com.Kreationnext.mypackage;
  2. import javax.persistence.Column;
  3. import javax.persistence.DiscriminatorValue;
  4. import javax.persistence.Entity;
  5. @Entity
  6. @DiscriminatorValue(“contractemployee”)
  7. public class Contract_Employee extends Employee{
  8.     @Column(name=“pay_per_hour”)
  9.     private float pay_per_hour;
  10.     @Column(name=“contract_duration”)
  11.     private String contract_duration;
  12.     //setters and getters
  13. }

2) Add the persistent classes in configuration file

Open the hibernate.cgf.xml file, and add entries of entity classes like this:
  1. <mapping class=“com.Kreationnext.mypackage.Employee”/>
  2.  <mapping class=“com.Kreationnext.mypackage.Contract_Employee”/>
  3.  <mapping class=“com.Kreationnext.mypackage.Regular_Employee”/>
  4. </pre></div>
  5. <table >
  6. <tr><td>Now the configuration file will look like this:
  7. </td></tr>
  8. </table>
  9. <span id=“filename”>File: hibernate.cfg.xml</span>
  10. <div class=“codeblock”><pre name=“code” class=“java” >
  11. <?xml version=‘1.0’ encoding=‘UTF-8’?>
  12. <!DOCTYPE hibernate-configuration PUBLIC
  13.           “-//Hibernate/Hibernate Configuration DTD 3.0//EN”
  14.           “http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd”>
  15. <!– Generated by MyEclipse Hibernate Tools.                   –>
  16. <hibernate-configuration>
  17.     <session-factory>
  18.         <property name=“hbm2ddl.auto”>update</property>
  19.         <property name=“dialect”>org.hibernate.dialect.Oracle9Dialect</property>
  20.         <property name=“connection.url”>jdbc:oracle:thin:@localhost:1521:xe</property>
  21.         <property name=“connection.username”>system</property>
  22.         <property name=“connection.password”>oracle</property>
  23.         <property name=“connection.driver_class”>oracle.jdbc.driver.OracleDriver</property>
  24.         <mapping class=“com.Kreationnext.mypackage.Employee”/>
  25.         <mapping class=“com.Kreationnext.mypackage.Contract_Employee”/>
  26.         <mapping class=“com.Kreationnext.mypackage.Regular_Employee”/>
  27.     </session-factory>
  28. </hibernate-configuration>

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


3) 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.Kreationnext.mypackage;
  2. import org.hibernate.*;
  3. import org.hibernate.cfg.*;
  4. public class StoreData {
  5. public static void main(String[] args) {
  6.     AnnotationConfiguration cfg=new AnnotationConfiguration();
  7.     Session session=cfg.configure(“hibernate.cfg.xml”).buildSessionFactory().openSession();
  8.     Transaction t=session.beginTransaction();
  9.     Employee e1=new Employee();
  10.     e1.setName(“sonoo”);
  11.     Regular_Employee e2=new Regular_Employee();
  12.     e2.setName(“Vivek Kumar”);
  13.     e2.setSalary(50000);
  14.     e2.setBonus(5);
  15.     Contract_Employee e3=new Contract_Employee();
  16.     e3.setName(“Arjun Kumar”);
  17.     e3.setPay_per_hour(1000);
  18.     e3.setContract_duration(“15 hours”);
  19.     session.persist(e1);
  20.     session.persist(e2);
  21.     session.persist(e3);
  22.     t.commit();
  23.     session.close();
  24.     System.out.println(“success”);
  25. }
  26. }

Output:

inher2

Comments

comments