addheader

Friday, 12 October 2012

hibernate class for subclass hierarchy




Table for subclass:

In this process we map the class hierarchy to multiple tables associated with the relational foreign key.
The main advantage of this process inheritance mapping is; the relational model is completely normalized, integrity constraint definitions are maintained.

Java:

http://1.bp.blogspot.com/-t8ZWZxwBPKk/T_zsBPHDSMI/AAAAAAAAABo/K_ZeBrDm7rA/s320/empsalemp.png
Database:

create table employee(empno int(4),ename char(16)
create table salariedemployee(empno int(4),annualSal int(4));

Ex4:
//hibernate-ineritance-join-subclass.


Employee.java

package madhav;

public class Employee {
       private int empno;
       private String ename;
       public int getEmpno() {                  
             return empno;
       }
       public void setEmpno(int empno) {
             this.empno = empno;
       }
       public String getEname() {
             return ename;
       }
       public void setEname(String ename) {
             this.ename = ename;
       }
}

SalariedEmployee.java

package madhav;

public class SalariedEmployee extends Employee{
      public double annualSal;

    public double getAnnualSal() {
          return annualSal;
    }

    public void setAnnualSal(double annualSal) {
          this.annualSal = annualSal;
    }
    public String toString()
    {
      return "salariedEmployee";
    }
}


employee.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="madhav.Employee" table="employee">
   <id name="empno" type="int" column="empno" >
   <generator class="increment"/>
  </id>
  <property name="ename">
   <column name="ename" />
  </property>
 <joined-subclass name="madhav.SalariedEmployee" table="salariedemp">
 <key column="empno"></key>

 <property name="annualSal"></property>
 </joined-subclass>

 </class>
</hibernate-mapping>





hibernate.cfg.xml


<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

    <session-factory>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/mysql</property>
        <property name="connection.username">madhav</property>
        <property name="connection.password">madhav</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hbm2ddl.auto">create</property>
    <mapping resource="employee.hbm.xml"/>
    </session-factory>

</hibernate-configuration>

                                                                       
TestCase.java

package madhav;

import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;

import org.hibernate.cfg.*;
import org.hibernate.*;

import org.apache.log4j.*;
public class TestCase {

     /**
      * @param args
      */
     SessionFactory factory;
   
     public void createEmp(SalariedEmployee sm)
     {
      Session session=null;
      Transaction tx=null;
      try
      {
                 
                  session =factory.openSession();
                 
         
tx=session.beginTransaction();
       
                  System.out.println("break1 in cd");
                  session.save(sm);
                 
                  tx.commit();
                  System.out.println("record inserted");
      }
      catch(HibernateException e)
      {
               
     e.printStackTrace();
                 
      }
     }
     public static void main(String[] args) {
                 // TODO Auto-generated method stub

      System.out.println("first");
                 TestCase t=new TestCase();
                 Configuration cfg=new Configuration();
                 cfg.configure();
                 //cfg.addResource("employee.hbm.xml");
                 //cfg.addResource("personaldetails.hbm.xml");
                 System.out.println("break 2");
               

t.factory=cfg.buildSessionFactory();
                 SalariedEmployee se=new SalariedEmployee();
                 se.setEname("madhav");
                 se.setAnnualSal(2000);
                 t.createEmp(se);
System.out.println("execution over");


     }

}


Output- console:

Output-sql-prompt:




No comments:

Post a Comment