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.


id job Sal
1 SALESMAN 20000
2 MGR 22222



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.


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) {
        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;



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);

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"
    <class name="com.bullraider.joinedsubclass.Person"
        <meta attribute="class-description"> This class contains person details. </meta>
        <id name="id" type="long" column="Id">
            <generator class="native" />
        <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"
      <key column="id"/>
      <property name="fname" column="FNAME" type="string" />
      <property name="lname" column="LNAME" type="string" />

Step3:Create hibernate.cfg.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        <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"/>

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()
        } 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");
            System.out.println("Records inserted sucessessfully");
        } catch (HibernateException e) {
        } finally {

Download joined_subclass_mapping.zip