Hibernate - SPLessons

Hibernate Table Per Subclass

Chapter 20

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Hibernate Table Per Subclass

Hibernate Table Per Subclass

shape Introduction

Hibernate Table Per Subclass uses inheritance strategy is selected, then Hibernate will map to properties of a superclass into a subclass table and properties of subclasses into a subclass table. When creating the tables in a database, a subclass table should have a foreign key column, then Hibernate will create the relation between the superclass table and subclass table. Here discriminator column is not required. In mapping file, Joined-subclass tag must be configured under the class tag.
Here Hibernate Table Per Subclass the base class object will be stored first and derived class object will be stored, If a developer wants to save derived object first, Then hibernate releases the primary key value of the parent class to store the database later on derived object will be stored.

shape Example

Example of Hibernate Table Per Subclass.

  1. First take the super class and subclasses, and superclass is inherited to subclasses.
  2. Store the POJO classes properties in tables.

Table structures

shape Tables


shape Example

  • Create the project directory structure.
  • Create the persistence classes(POJO classes).


package com.itoolsinfo;

public class Payment 
	private int paymentId;
    private double amount;
	public int getPaymentId() {
		return paymentId;
	public void setPaymentId(int paymentId) {
		this.paymentId = paymentId;
	public double getAmount() {
		return amount;
	public void setAmount(double amount) {
		this.amount = amount;

Here created a class Payment, by giving variables payment id and amount. Set and Get methods are applied on this variables.

package com.itoolsinfo;

public class CreditCard extends Payment
	private int cardNumber;
	private String cardType;
	public int getCardNumber() {
		return cardNumber;
	public void setCardNumber(int cardNumber) {
		this.cardNumber = cardNumber;
	public String getCardType() {
		return cardType;
	public void setCardType(String cardType) {
		this.cardType = cardType;


Here created the class CreditCard that is extending the Payment class means that it is acquiring the properties of Payment class.

package com.itoolsinfo;

public class DebitCard extends Payment
        private int pinNumber;
	    private String cardName;
		public int getPinNumber() {
			return pinNumber;
		public void setPinNumber(int pinNumber) {
			this.pinNumber = pinNumber;
		public String getCardName() {
			return cardName;
		public void setCardName(String cardName) {
			this.cardName = cardName;

Here created the class DebitCard that is extending Payment class.

  • Mapping all the POJO classes in Mapping File.


<?xml version='1.0' encoding='UTF-8'?>	 	
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN&quot; &quot;http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd&quot;&gt;	 	
 <class name="com.itoolsinfo.Payment" table=" SalaryDetails" >	 	 
 <id name="paymentId" column="pid">	 	 
 "generator class="increment"/>	
 <property name="amount" column="amount"/> 	
 <joined-subclass name="com.itoolsinfo.CreditCard" table="creditcarddetails">	 	 
 <key column="paymentId"/>;	 	 
 <property name="cardNumber" column="creditNumber"/>	 	 
 <property name="cardType" column="creditType" length="20"/>	 	
 <joined-subclass name="com.itoolsinfo.DebitCard" table="debitcarddetails">	 	 
 <key column="paymentId"/> 	
 <property name="pinNumber"/> 	
 <property name="cardName" length="20"/> 	
  • Configure the mapping file in Hibernate Configuration file.


<?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"> 	 	 	 	 
 <property name="hibernate.hbm2ddl.auto">create</property> 	
 <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>	 	 
 <property name="connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>	 	
 <!-- property name="connection.url"<jdbc:oracle:thin:@</property--> 	 
 <property name="connection.username">system</property>	 	 
<property name="connection.password">system</property>	 	  
 <property name="connection.driver_class"<oracle.jdbc.driver.OracleDriver</property>	 	 	 	 
 <mapping resource="payment.hbm.xml"/>	 	 	 	 
  • Create the class and store all the POJO class objects.


package com.itoolsinfo;

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

public class SPLessons
	public static void main(String args[])
		SessionFactory factory=new Configuration().configure().buildSessionFactory();
		 Session session=factory.openSession();
		     Payment payment=new Payment();
		      CreditCard creditcard=new CreditCard();
			  DebitCard debitcard=new DebitCard();
			   debitcard.setCardName("ANDHRA BANK");
             Transaction transaction=session.beginTransaction();
System.out.println("Payment datails all are inserted using Table Per Subclass");

The database operations will be performed here.

  • Output tables in database are as follows.
select * from SalaryDetails;
select * from CreditCardDetails;
select * from DebitCardDetails;


shape Key Points

  • In Hibernate Table Per Subclass key element will be used.
  • In Table Per Subclass first base class object will be stored, then derived class object will be stored.
  • Hibernate releases the primary key before derived class object will be stored.