Hibernate - SPLessons

Hibernate Table Per Subclass

Home > Lesson > 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

SalaryDetails CreditCardDetails DebitCardDetails

shape Example

  • Create the project directory structure.
  • Create the persistence classes(POJO classes).
Payment.java [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; } } [/java] Here created a class Payment, by giving variables payment id and amount. Set and Get methods are applied on this variables. CreditCard.java [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; } } [/java] Here created the class CreditCard that is extending the Payment class means that it is acquiring the properties of Payment class. DebitCard.java [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; } } [/java] Here created the class DebitCard that is extending Payment class.
  • Mapping all the POJO classes in Mapping File.
payment.hbm.xml [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> [/xml]
  • Configure the mapping file in Hibernate Configuration file.
hibernate.cfg.xml [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> [/xml]
  • Create the class and store all the POJO class objects.
SPLessons.java [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"); } } [/java] The database operations will be performed here.
  • Output tables in database are as follows.
[sql]select * from SalaryDetails;[/sql] [sql]select * from CreditCardDetails;[/sql] [sql]select * from DebitCardDetails;[/sql]

Summary

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.