- 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" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<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]