Inheritance in Hibernate ->Table per sub class

lets start with the below code(for explanations on Hibernate inheritance see previous post)

1)hibernate.cfg.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version='1.0' ?>  
<!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="hbm2ddl.auto">update</property>  
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>  
        <property name="connection.url">jdbc:mysql://localhost:3306/HibernateDB</property>  
        <property name="connection.username">root</property>  
        <property name="connection.password">kb</property>  
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="show_sql">true</property>
    <mapping resource="com/kb/hbmFiles/Person.hbm.xml"/>  
    </session-factory>  
</hibernate-configuration>  
<?xml version='1.0' ?>  
<!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="hbm2ddl.auto">update</property>  
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>  
        <property name="connection.url">jdbc:mysql://localhost:3306/HibernateDB</property>  
        <property name="connection.username">root</property>  
        <property name="connection.password">kb</property>  
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="show_sql">true</property>
    <mapping resource="com/kb/hbmFiles/Person.hbm.xml"/>  
    </session-factory>  
</hibernate-configuration>  

2)person.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.kb.model;
 
public class Person {
    private long personId;
    private String firstName;
    private String lastName;
    public Person(String firstName, String lastName) {
        this.firstName=firstName;
        this.lastName=lastName;
    }
    public long getPersonId() {
        return personId;
    }
    public void setPersonId(long personId) {
        this.personId = personId;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    
 
}
package com.kb.model;

public class Person {
	private long personId;
	private String firstName;
	private String lastName;
	public Person(String firstName, String lastName) {
		this.firstName=firstName;
		this.lastName=lastName;
	}
	public long getPersonId() {
		return personId;
	}
	public void setPersonId(long personId) {
		this.personId = personId;
	}
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	

}

3)Owner.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.kb.model;
 
public class Owner extends Person {
    private Long profitAmt;
    private Long investedAmt;
    
     public Owner(String firstName, String lastName,Long profitAmt,
             Long investedAmt) {
        super(firstName, lastName);
        this.profitAmt=profitAmt;
        this.investedAmt=investedAmt;
    }
 
    public Long getProfitAmt() {
        return profitAmt;
    }
 
    public void setProfitAmt(Long profitAmt) {
        this.profitAmt = profitAmt;
    }
 
    public Long getInvestedAmt() {
        return investedAmt;
    }
 
    public void setInvestedAmt(Long investedAmt) {
        this.investedAmt = investedAmt;
    }
 
    }
package com.kb.model;

public class Owner extends Person {
	private Long profitAmt;
    private Long investedAmt;
	
	 public Owner(String firstName, String lastName,Long profitAmt,
			 Long investedAmt) {
		super(firstName, lastName);
		this.profitAmt=profitAmt;
		this.investedAmt=investedAmt;
	}

	public Long getProfitAmt() {
		return profitAmt;
	}

	public void setProfitAmt(Long profitAmt) {
		this.profitAmt = profitAmt;
	}

	public Long getInvestedAmt() {
		return investedAmt;
	}

	public void setInvestedAmt(Long investedAmt) {
		this.investedAmt = investedAmt;
	}

	}

4)Employee.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.kb.model;
 
import java.util.Date;
 
public class Employee extends Person {
    
    private Date joiningDate;
    private String departmentName;
    
    public Employee(String firatName, String lastname, String dName, Date joiningDate) {
        super(firatName,lastname);
        this.joiningDate=joiningDate;
        this.departmentName=dName;
    }
    public Date getJoiningDate() {
        return joiningDate;
    }
    public void setJoiningDate(Date joiningDate) {
        this.joiningDate = joiningDate;
    }
    public String getDepartmentName() {
        return departmentName;
    }
    public void setDepartmentName(String departmentName) {
        this.departmentName = departmentName;
    }
 
}
package com.kb.model;

import java.util.Date;

public class Employee extends Person {
	
	private Date joiningDate;
    private String departmentName;
    
	public Employee(String firatName, String lastname, String dName, Date joiningDate) {
		super(firatName,lastname);
		this.joiningDate=joiningDate;
		this.departmentName=dName;
	}
	public Date getJoiningDate() {
		return joiningDate;
	}
	public void setJoiningDate(Date joiningDate) {
		this.joiningDate = joiningDate;
	}
	public String getDepartmentName() {
		return departmentName;
	}
	public void setDepartmentName(String departmentName) {
		this.departmentName = departmentName;
	}

}

5)Person.hbm.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?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 package="com.kb.model">
 
    <class name="Person" table="PERSON">
        <id name="personId" column="PERSON_ID">
            <generator class="native" />
        </id>
 
        <property name="firstName" />
        <property name="lastName" column="lastname" />
 
        <joined-subclass name="Employee" extends="Person">
        <key column="PERSON_ID"></key>
                <property name="departmentName" column="department_name" />
                <property name="joiningDate" type="date" column="joining_date" />
        </joined-subclass>
        
        <joined-subclass name="Owner" extends="Person">
                <key column="person_id" />
                <property name="profitAmt" column="profitAmt" />
                <property name="investedAmt" column="investedAmt" />
        </joined-subclass>
    </class>
</hibernate-mapping>
<?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 package="com.kb.model">
 
    <class name="Person" table="PERSON">
        <id name="personId" column="PERSON_ID">
            <generator class="native" />
        </id>
 
        <property name="firstName" />
        <property name="lastName" column="lastname" />
 
        <joined-subclass name="Employee" extends="Person">
        <key column="PERSON_ID"></key>
                <property name="departmentName" column="department_name" />
                <property name="joiningDate" type="date" column="joining_date" />
        </joined-subclass>
        
        <joined-subclass name="Owner" extends="Person">
                <key column="person_id" />
                <property name="profitAmt" column="profitAmt" />
                <property name="investedAmt" column="investedAmt" />
        </joined-subclass>
    </class>
</hibernate-mapping>

6)Client code(DAO)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.kb.dao;
 
import java.util.Date;
 
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
 
import com.kb.model.Employee;
import com.kb.model.Owner;
import com.kb.model.Person;
 
public class DBInteract {
    
    public static void main(String[] args) {
         
         //creating configuration object  
        Configuration cfg=new Configuration();  
        
        cfg.configure("hibernate.cfg.xml");//populates the data of the configuration file  
          
        //creating seession factory object  
       SessionFactory factory=cfg.buildSessionFactory();  
          
        Session session = factory.openSession();
        session.beginTransaction();
 
         
        Person person = new Person("kb", "gc");
        session.save(person);
 
        Employee employee = new Employee("raj", "kumar", "financing", new Date());
        session.save(employee);
         
        Owner owner = new Owner("ravi", "chandra", 10000l, 50000l);
        session.save(owner);
        
        session.getTransaction().commit();
        session.close();
 
   }
 
}
package com.kb.dao;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.kb.model.Employee;
import com.kb.model.Owner;
import com.kb.model.Person;

public class DBInteract {
	
	public static void main(String[] args) {
		 
		 //creating configuration object  
	    Configuration cfg=new Configuration();  
	    
	    cfg.configure("hibernate.cfg.xml");//populates the data of the configuration file  
	      
	    //creating seession factory object  
	   SessionFactory factory=cfg.buildSessionFactory();  
	      
	    Session session = factory.openSession();
        session.beginTransaction();
 
         
        Person person = new Person("kb", "gc");
        session.save(person);
 
        Employee employee = new Employee("raj", "kumar", "financing", new Date());
        session.save(employee);
         
        Owner owner = new Owner("ravi", "chandra", 10000l, 50000l);
        session.save(owner);
        
        session.getTransaction().commit();
        session.close();
 
   }

}

Run it and see the output as below

outputconsole

TableAndData

Now see the beauty of avoiding duplicate columns in each child table.
Each child table will have a mapping with the parent table and hence no duplicate columns in child table.

About the Author

Karibasappa G C (KB)
Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on