Hibernate Inheritance Table Per Concrete Class Hierarchy
Something like previous example but the changes are at mapping file only, and one more thing is..
- Once we save the derived class object, then derived class data and base class data will be saved in the derived class related table in the database
- for this type we need the tables for derived classes, but not for the base class
- in the mapping file we need to use one new element <union-subclass — >under <class —>
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
1234567891011121314151617181920212223242526272829303132333435363738394041424344package 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" /> <property name="amount" column="amt" />
<union-subclass name="str.CreditCard">
<property name="CreditCardType" column="cctype" length="10" /> </union-subclass>
<union-subclass name="str.Cheque">
<property name="ChequeType" column="cqtype" length="10" /> </union-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>




post a comment