May 7, 2011

Hibernate Inheritance Mapping (Strategy 3) - Table per inheritance hierarchy

In strategy-3, the entire inheritance hierarchy is mapped to a single table in the Database Schema. The mapping file reflects the existence of inheritance through special elements in the XML.

1) Create the persistent classes inheritance hierarchy, S3 is the superclass and A3 and B3 are the subclasses that inherit from the superclass.

S3.java

package info.icontraining.hibernate;

public class S3 {

   private Long id;
   private String p1;
   private String p2;
 
   public Long getId() {
      return id;
   }
   public void setId(Long id) {
      this.id = id;
   } 
   public String getP1() {
      return p1;
   }
   public void setP1(String p1) {
      this.p1 = p1;
   }
   public String getP2() {
      return p2;
   }
   public void setP2(String p2) {
      this.p2 = p2;
   } 
}

A3.java

package info.icontraining.hibernate;

public class A3 extends S3 {

   private String p3;
   private String p4;
 
   public A3() { }
 
   public A3(String p1, String p2, String p3, String p4) {
      this.setP1(p1);
      this.setP2(p2);
      this.setP3(p3);
      this.setP4(p4);
   }
 
   public String getP3() {
      return p3;
   }
   public void setP3(String p3) {
      this.p3 = p3;
   }
   public String getP4() {
      return p4;
   }
   public void setP4(String p4) {
      this.p4 = p4;
   } 
}

B3.java

package info.icontraining.hibernate;

public class B3 extends S3 {

   private String p5;
   private String p6;
 
   public B3() { }
 
   public B3(String p1, String p2, String p5, String p6) {
      this.setP1(p1);
      this.setP2(p2);
      this.setP5(p5);
      this.setP6(p6);
   }
 
   public String getP5() {
      return p5;
   }
   public void setP5(String p5) {
      this.p5 = p5;
   }
   public String getP6() {
      return p6;
   }
   public void setP6(String p6) {
      this.p6 = p6;
   }
}

2) Create the mapping file, AB3.hbm.xml

AB3.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
<hibernate-mapping> 
   <class name="info.icontraining.hibernate.S3" table="SAB3_TABLE" > 
      <id name="id" column="ID" type="long" > 
         <generator class="native" /> 
      </id>
      <discriminator column="DISC_COL" type="string" />
      <property name="p1" column="P1" type="string" />
      <property name="p2" column="P2" type="string" />
      <subclass name="info.icontraining.hibernate.A3" discriminator-value="a3">
         <property name="p3" column="P3" type="string" />
         <property name="p4" column="P4" type="string" />
      </subclass>
      <subclass name="info.icontraining.hibernate.B3" discriminator-value="b3">
         <property name="p5" column="P5" type="string" />
         <property name="p6" column="P6" type="string" />
      </subclass>
   </class> 
</hibernate-mapping> 


3) In the hibernate.cfg.xml file, add the following configuration,

<mapping resource="info/icontraining/hibernate/AB3.hbm.xml" />


4) Create the client code, in a servlet class, as follows,

package info.icontraining.servlets;

import java.io.*; 
import java.util.*; 
import javax.servlet.*; 
import javax.servlet.http.* ; 
import org.hibernate.*; 
import info.icontraining.hibernate.*;

public class InheritanceDemo3 extends HttpServlet { 
  
   public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException{ 
  
      PrintWriter out = res.getWriter(); 

      Session session = HibernateUtil.getSessionFactory().openSession(); 
      Transaction tx = session.beginTransaction(); 
      A3 a1 = new A3("str1", "str2", "str3", "str4");
      B3 b1 = new B3("str1", "str2", "str5", "str6");
      session.save(a1); 
      session.save(b1);
      tx.commit(); 
      session.close(); 

      Session newSession = HibernateUtil.getSessionFactory().openSession(); 
      Transaction newTransaction = newSession.beginTransaction(); 
      List<A3> a = newSession.createQuery("from A3").list(); 
      out.println( a.size() + " items found:" ); 
      for ( Iterator<A3> iter = a.iterator(); iter.hasNext(); ) { 
         A3 a2 = iter.next(); 
         out.println( a2.getP1() ); 
      } 
   
      out.println();
      out.println();
   
      List<B3> b = newSession.createQuery("from B3").list(); 
      out.println( b.size() + " items found:" ); 
      for ( Iterator<B3> iter = b.iterator(); iter.hasNext(); ) { 
         B3 b2 = iter.next(); 
         out.println( b2.getP2() ); 
      } 
   
      newTransaction.commit(); 
      newSession.close(); 
   }  
}


5) Configure the servlet in the web.xml file,

<servlet>
   <servlet-name>InheritanceDemo3</servlet-name>
   <servlet-class>info.icontraining.servlets.InheritanceDemo3</servlet-class>
</servlet>
<servlet-mapping>
   <servlet-name>InheritanceDemo3</servlet-name>
   <url-pattern>/inheritanceDemo3</url-pattern>
</servlet-mapping>


6) Enter the following URL in the browser to test,

http://localhost:8080/WebAppName/inheritanceDemo3

No comments:

Post a Comment