Hibernate provides advanced features, which can let your classes map even if they are in relationship like  composition and inheritance. We discussed composition last tutorial. Joined-subclass is one of the way for achieving inheritance.

 

There is two table one is Person123 and another is PersonDetail.

Person123

id job Sal
1 SALESMAN 20000
2 MGR 22222

 

PersonDetails

Id Fname lname
1 Shobha Dash
2 Abdul Reheman

PersonDetails is-a Person. In person_detail , firstname, lastname of the person has described. So person is parent class and PersonDetails is subclass ( May not be a good OOP, but suits good for demonstrating this tutorial). We have to map child class to parent one.

So when we create PersonDetails, and try to save it hibernate understands that PersonDetails is-a Person, and it splits the data and stores it in repective table. We tell hibernate about this by using
joined-subclass tag in Person.hbm.xml file.

For achieving this relationship we have to follow these steps

Step1:Design Person and PersonDetails classes.

Person.java

package com.bullraider.joinedsubclass;
import java.io.Serializable;
public class Person implements Serializable{
    private  int sal;
    private String job;
    private long id;

  public Person(String job, int sal) {
        super();
        this.job = job;
        this.sal = sal;
    }

    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public int getSal() {
        return sal;
    }
    public void setSal(int sal) {
        this.sal = sal;
    }
    public String getJob() {
        return job;
    }
    public void setJob(String job) {
        this.job = job;
    }

}


PersonDetail.java

package com.bullraider.joinedsubclass;
public class PersonDetails extends Person{

private String fname;
private String lname;

public PersonDetails(String job, int sal,String fname,String lname) {
        super(job, sal);
        this.fname=fname;
        this.lname=lname;
    }

public String getFname() {
    return fname;
}
public void setFname(String fname) {
    this.fname = fname;
}
public String getLname() {
    return lname;
}
public void setLname(String lname) {
    this.lname = lname;
}
}

 

Step2: Create Person.hbm.xml(This xml file will describe to create Person table in which PersonDetail class is associated)

<?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="com.bullraider.joinedsubclass.Person"
        table="Person123">
        <meta attribute="class-description"> This class contains person details. </meta>
        <id name="id" type="long" column="Id">
            <generator class="native" />
        </id>
        <property name="job" type="string" column="job" not-null="true" />
        <property name="sal" type="integer" column="sal" not-null="true" />
        <joined-subclass name="com.bullraider.joinedsubclass.PersonDetails"
  table="PersonDetails">
      <key column="id"/>
      <property name="fname" column="FNAME" type="string" />
      <property name="lname" column="LNAME" type="string" />
    </joined-subclass>
        </class>
</hibernate-mapping>


Step3:Create 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">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:ORCL</property>
        <property name="hibernate.connection.username">scott</property>
        <property name="connection.password">tiger</property>
        <property name="connection.pool_size">1</property>
        <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
        <property name="show_sql">true</property>
       <property name="hbm2ddl.auto">create-drop</property>
        <mapping resource="com/bullraider/joinedsubclass/Person.hbm.xml"/>
    </session-factory>
</hibernate-configuration>


Step4: Create HibernateUtil Class to get SessionFactory object

 

package com.bullraider.joinedsubclass.util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
    private static final SessionFactory sessionFactory;
    static {
        try {
            sessionFactory = new Configuration().configure()
                    .buildSessionFactory();
        } catch (Throwable ex) {
            System.err.println("SessionFactory creation failed" + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}


Step5:Design Main Class

package com.bullraider.joinedsubclass;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.bullraider.joinedsubclass.util.HibernateUtil;

public class Main {

    public static void main(String[] args) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            PersonDetails psd1=new PersonDetails("MANAGER",30000,"Susanta","Kundu");
            PersonDetails psd2=new PersonDetails("CLERK",20000,"Amar","Dutta");
            session.save(psd1);
            session.save(psd2);
            transaction.commit();
            System.out.println("Records inserted sucessessfully");
        } catch (HibernateException e) {
            transaction.rollback();
            e.printStackTrace();
        } finally {
            session.close();
        }
    }
}

Download joined_subclass_mapping.zip