Cheap VPS & Xen Server

Residential Proxy Network - Hourly & Monthly Packages

Table Per Concrete class using Annotation


In case of Table Per Concrete class, tables are created per class. So there are no nullable values in the table. Disadvantage of this approach is that duplicate columns are created in the subclass tables.

Here, we need to use @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) annotation in the parent class and @AttributeOverrides annotation in the subclasses.

@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) specifies that we are using table per concrete class strategy. It should be specified in the parent class only.

@AttributeOverrides defines that parent class attributes will be overriden in this class. In table structure, parent class table columns will be added in the subclass table.

The class hierarchy is given below:

inheritance1

The table structure for each table will be as follows:

Table structure for Employee class

concretetable1

Table structure for Regular_Employee class

concretetable2

Table structure for Contract_Employee class

concretetable3

Example of Table per concrete class

In this example we are creating the three classes and provide mapping of these classes in the employee.hbm.xml file.

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 = “employee102”)
  5. @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
  6. public class Employee {
  7. @Id
  8. @GeneratedValue(strategy=GenerationType.AUTO)
  9. @Column(name = “id”)
  10. private int id;
  11. @Column(name = “name”)
  12. private String name;
  13. //setters and getters
  14. }

File: Regular_Employee.java

  1. package com.Kreationnext.mypackage;
  2. import javax.persistence.*;
  3. @Entity
  4. @Table(name=“regularemployee102”)
  5. @AttributeOverrides({
  6.     @AttributeOverride(name=“id”, column=@Column(name=“id”)),
  7.     @AttributeOverride(name=“name”, column=@Column(name=“name”))
  8. })
  9. public class Regular_Employee extends Employee{
  10. @Column(name=“salary”)
  11. private float salary;
  12. @Column(name=“bonus”)
  13. private int bonus;
  14. //setters and getters
  15. }

File: Contract_Employee.java

  1. package com.Kreationnext.mypackage;
  2. import javax.persistence.*;
  3. @Entity
  4. @Table(name=“contractemployee102”)
  5. @AttributeOverrides({
  6.     @AttributeOverride(name=“id”, column=@Column(name=“id”)),
  7.     @AttributeOverride(name=“name”, column=@Column(name=“name”))
  8. })
  9. public class Contract_Employee extends Employee{
  10.     @Column(name=“pay_per_hour”)
  11.     private float pay_per_hour;
  12.     @Column(name=“contract_duration”)
  13.     private String contract_duration;
  14.     public float getPay_per_hour() {
  15.         return pay_per_hour;
  16.     }
  17.     public void setPay_per_hour(float payPerHour) {
  18.         pay_per_hour = payPerHour;
  19.     }
  20.     public String getContract_duration() {
  21.         return contract_duration;
  22.     }
  23.     public void setContract_duration(String contractDuration) {
  24.         contract_duration = contractDuration;
  25.     }
  26. }

2) Add mapping of hbm file in configuration file

File: hibernate.cfg.xml

  1. <?xml version=‘1.0’ encoding=‘UTF-8’?>
  2. <!DOCTYPE hibernate-configuration PUBLIC
  3.           “-//Hibernate/Hibernate Configuration DTD 3.0//EN”
  4.           “http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd”>
  5. <!– Generated by MyEclipse Hibernate Tools.                   –>
  6. <hibernate-configuration>
  7.     <session-factory>
  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”>oracle</property>
  13. <property name=“connection.driver_class”>oracle.jdbc.driver.OracleDriver</property>
  14.         <mapping class=“com.Kreationnext.mypackage.Employee”/>
  15.         <mapping class=“com.Kreationnext.mypackage.Contract_Employee”/>
  16.         <mapping class=“com.Kreationnext.mypackage.Regular_Employee”/>
  17.     </session-factory>
  18. </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: StoreData.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. }

Comments

comments