Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Table Per Subclass


In case of Table Per Subclass, subclass mapped tables are related to parent class mapped table by primary key and foreign key relationship.

The <joined-subclass> element of class is used to map the child class with parent using the primary key and foreign key relation.

In this example, we are going to use hb2ddl.auto property to generate the table automatically. So we don’t need to be worried about creating tables in the database.

Let’s see the hierarchy of classes that we are going to map.

inheritance1

Let’s see how can we map this hierarchy by joined-subclass element:

  1. <?xml version=‘1.0’ encoding=‘UTF-8’?>
  2. <!DOCTYPE hibernate-mapping PUBLIC
  3.   “-//Hibernate/Hibernate Mapping DTD 3.0//EN”
  4.   “http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd”>
  5.   <hibernate-mapping>
  6.   <class name=“com.Kreationnext.mypackage.Employee” table=“emp123”>
  7.   <id name=“id”>
  8.   <generator class=“increment”></generator>
  9.   </id>
  10.   <property name=“name”></property>
  11.   <joined-subclass name=“com.Kreationnext.mypackage.Regular_Employee” table=“regemp123”>
  12.   <key column=“eid”></key>
  13.   <property name=“salary”></property>
  14.   <property name=“bonus”></property>
  15.   </joined-subclass>
  16.   <joined-subclass name=“com.Kreationnext.mypackage.Contract_Employee” table=“contemp123”>
  17.   <key column=“eid”></key>
  18.   <property name=“pay_per_hour”></property>
  19.   <property name=“contract_duration”></property>
  20.   </joined-subclass>
  21.   </class>
  22.   </hibernate-mapping>
In case of table per subclass class, there will be three tables in the database, each representing a particular class.
The joined-subclass subelement of class, specifies the subclass. The key subelement of joined-subclass is used to generate the foreign key in the subclass mapped table. This foreign key will be associated with the primary key of parent class mapped table.

 

The table structure for each table will be as follows:

Table structure for Employee class

concretetable1

Table structure for Regular_Employee class

subclasstable2

Table structure for Regular_Employee class

subclasstable2

Table structure for Contract_Employee class

subclasstable3

Example of Table per subclass 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. public class Employee {
  3. private int id;
  4. private String name;
  5. //getters and setters
  6. }

File: Regular_Employee.java

  1. package com.Kreationnext.mypackage;
  2. public class Regular_Employee extends Employee{
  3. private float salary;
  4. private int bonus;
  5. //getters and setters
  6. }

File: Contract_Employee.java

  1. package com.Kreationnext.mypackage;
  2. public class Contract_Employee extends Employee{
  3.     private float pay_per_hour;
  4.     private String contract_duration;
  5. //getters and setters
  6. }

2) Create the mapping file for Persistent class

The mapping has been discussed above for the hierarchy.

File: employee.hbm.xml

  1. <?xml version=‘1.0’ encoding=‘UTF-8’?>
  2. <!DOCTYPE hibernate-mapping PUBLIC
  3.   “-//Hibernate/Hibernate Mapping DTD 3.0//EN”
  4.   “http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd”>
  5.   <hibernate-mapping>
  6.   <class name=“com.Kreationnext.mypackage.Employee” table=“emp123”>
  7.   <id name=“id”>
  8.   <generator class=“increment”></generator>
  9.   </id>
  10.   <property name=“name”></property>
  11.   <joined-subclass name=“com.Kreationnext.mypackage.Regular_Employee” table=“regemp123”>
  12.   <key column=“eid”></key>
  13.   <property name=“salary”></property>
  14.   <property name=“bonus”></property>
  15.   </joined-subclass>
  16.   <joined-subclass name=“com.Kreationnext.mypackage.Contract_Employee” table=“contemp123”>
  17.   <key column=“eid”></key>
  18.   <property name=“pay_per_hour”></property>
  19.   <property name=“contract_duration”></property>
  20.   </joined-subclass>
  21.   </class>
  22.   </hibernate-mapping>

3) create configuration file

Open the hibernate.cgf.xml file, and add an entry of mapping resource like this:

  1. <mapping resource=“employee.hbm.xml”/>

Now the configuration file will look like this:

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. <hibernate-configuration>
  6.     <session-factory>
  7.         <property name=“hbm2ddl.auto”>update</property>
  8.         <property name=“dialect”>org.hibernate.dialect.Oracle9Dialect</property>
  9.         <property name=“connection.url”>jdbc:oracle:thin:@localhost:1521:xe</property>
  10.         <property name=“connection.username”>system</property>
  11.         <property name=“connection.password”>oracle</property>
  12.         <property name=“connection.driver_class”>oracle.jdbc.driver.OracleDriver</property>
  13.     <mapping resource=“employee.hbm.xml”/>
  14.     </session-factory>
  15. </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: 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.     Session session=new Configuration().configure(“hibernate.cfg.xml”)
  7.                         .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