Hibernate - SPLessons

Chapter 20

Hibernate Table Per Subclass

Hibernate Table Per Subclass

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.

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

SalaryDetails
CreditCardDetails
DebitCardDetails

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

Payment.java

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.
CreditCard.java

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.
DebitCard.java

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.

payment.hbm.xml


<?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;	 	
 <hibernate-mapping> 	
 <class name="com.itoolsinfo.Payment" table=" SalaryDetails" >	 	 
 <id name="paymentId" column="pid">	 	 
 "generator class="increment"/>	
 </id>
 	 	 
 <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>	
 	 	 
 <joined-subclass name="com.itoolsinfo.DebitCard" table="debitcarddetails">	 	 
 <key column="paymentId"/> 	
 <property name="pinNumber"/> 	
 <property name="cardName" length="20"/> 	
 </joined-subclass> 		 	 
 </class>	 		 	 
</hibernate-mapping>
  • Configure the mapping file in Hibernate Configuration file.

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.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:@127.0.0.1:1521:XE</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"/>	 	 	 	 
 </session-factory"	 	 
 </hibernate-configuration>	 
  • Create the class and store all the POJO class objects.

SPLessons.java

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();
		         payment.setPaymentId(5);
		         payment.setAmount(30000);
			
		      CreditCard creditcard=new CreditCard();
			    creditcard.setCardNumber(45);
			    creditcard.setCardType("visa");
			    
			  DebitCard debitcard=new DebitCard();
			   debitcard.setPinNumber(6432);
			   debitcard.setCardName("ANDHRA BANK");
			   
             Transaction transaction=session.beginTransaction();
			 
			        session.save(payment);
			        session.save(creditcard);
				session.save(debitcard);
			        transaction.commit();
				session.close();
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;

Summary

  • 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.