Hibernate Inheritance Table Per Class Hierarchy
Here is the explanation and one example on hibernate table per class hierarchy, consider we have base class named Payment and 2 derived classes like CreditCard, Cheque
If we save the derived class object like CreditCard or Cheque then automatically Payment class object will also be saved into the database, and in the database all the data will be stored into a single table only, which is base class table for sure.
But here we must use one extra discriminator column in the database, just to identify which derived class object we have been saved in the table along with the base class object, if we are not using this column hibernate will throws the exception, see this example so that you will get one idea on this concept.
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
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
1234567891011121314151617181920212223242526272829303132333435363738package 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:
12345678910111213141516171819202122<?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" />
<discriminator column="dcolumn" type="string" length="5"/>
<property name="amount" column="amt" /> <subclass name="str.CreditCard" discriminator-value="CC"> <property name="CreditCardType" column="cctype" length="10" /> </subclass> <subclass name="str.Cheque" discriminator-value="cq"> <property name="ChequeType" column="cqtype" length="10" /> </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:
- Payment.java, CreditCard.java, Cheque.java are just pojo classes nothing to explain, but see in CreditCard.java, Cheque.java i have inherited the Payment.java.
- In this inheritance concept, mapping file is the central part, see in line number 10, we added one new line discriminator, after the id element just to identify which derived class object we have been saved in the table (see the oracle console once)
- every thing has been saved in a single table
post a comment