Hibernate Inheritance: Table Per subClass Hierarchy

This is also just like previous example, but some changes are there, in table per class hierarchy all the data was saved in a single table but here,

If we save the CreditCard class object, then first hibernate will saves the data related to super class object into the super class related table in the database and then CreditCard object data in CreditCard related table in the database, so first base class data will be saved

Required files_

  • Payment.java (Base class)
  • CreditCard.java (Derived class)
  • Cheque.java (Derived class)
  • ClientForSave.java (for our logic)
  • Payment.hbm.xml
  • hibernate.cfg.xml

All are same but mapping file is different than previous example..

Payment.java:

123456789101112131415161718192021package str;

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

}

CreditCard.java:

123456789101112131415package str;

public class CreditCard extends Payment{

private String CreditCardType; public String getCreditCardType() { return CreditCardType; } public void setCreditCardType(String creditCardType) { CreditCardType = creditCardType; } }

Cheque.java:

123456789101112131415package str;

public class Cheque extends Payment{

private String ChequeType; public String getChequeType() { return ChequeType; } public void setChequeType(String chequeType) { ChequeType = chequeType; } }

ClientForSave.java

12345678910111213141516171819202122232425262728293031323334353637383940414243package str;

import org.hibernate.*;
import org.hibernate.cfg.*;
 
public class ClientForSave { 
 
    public static void main(String[] args)
    {
 
        Configuration cfg = new Configuration();
        cfg.configure("hibernate.cfg.xml"); 
 
        SessionFactory factory = cfg.buildSessionFactory();
        Session session = factory.openSession();

        CreditCard c=new CreditCard();
 
        c.setPaymentId(10);
        c.setAmount(2500);
        c.setCreditCardType("Visa");

 
        
        Cheque c1=new Cheque();
        
        c1.setPaymentId(11);
        c1.setAmount(2600);
        c1.setChequeType("ICICI");

        
        
        Transaction tx = session.beginTransaction();
        session.save(c);
        session.save(c1);
        System.out.println("Object saved successfully.....!!");
        tx.commit();
        session.close();
        factory.close();
    }
 
}

Payment.hbm.xml:

123456789101112131415161718192021222324<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

<class name="str.Payment" table="PAYMENT">

<id name="paymentId" column="pid" />
<property name="amount" column="amt" />

<joined-subclass name="str.CreditCard" table="CreditCard">

<key column="dummy1" />

<property name="CreditCardType" column="cctype" length="10" /> </joined-subclass>

<joined-subclass name="str.Cheque" table="Cheque">

<key column="dummy2" />

<property name="ChequeType" column="cqtype" length="10" /> </joined-subclass> </class> </hibernate-mapping>

 

hibernate.cfg.xml:

123456789101112131415161718192021<?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="connection.driver_class">oracle.jdbc.driver.OracleDriver
</property>
<property name="connection.url">jdbc:oracle:thin:@www.java4s.com:1521:XE</property>
<property name="connection.username">system</property>
<property name="connection.password">admin</property>

<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>

<mapping resource="Payment.hbm.xml" />

</session-factory>
</hibernate-configuration>

Notes:

  • In the mapping file,  <key –> element is because,  once we save the derived class object, then hibernate will first save the baseclass object then derived class object right ..!, so at the time of saving the derived class object hibernate will copy the primary key value of the base class into the corresponding derived class, see in the above output 10 copied into dummy1 column of CreditCard table and 11 copied into Dummy2 column of the cheque table
  • that’s it friends, nothing to explain….   ?

Related Articles

post a comment