- Create the project directry structure
- Create persistence classes or 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 the developer created the class
Payment and the class will be extended by other classes to extend the properties from this class, here the developer used set and get methods, actually the purpose of
Set and
Get methods are a pattern of data encapsulation. Instead of accessing class member variables directly, one can define get methods to access these variables, and set methods to modify them.
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
Payment class to acquire the properties, here also used set and get methods for the card type.
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 the developer created the
DebitCard class and it is also extending the class Payment, here also used set and get methods for getting pin number and getting card name, the functionality of set and get methods alreay disscussed in payment class.
- Map 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" discriminator-value="employee">
<id name="paymentId" column="pid">
<generator class="increment"/>
</id>
<discriminator column="paymentmode" type="string"></discriminator>
<property name="amount" column="amount"/>
<subclass name="com.iquickinfo.CreditCard" discriminator-value="creditcard">
<property name="cardNumber"/>
<property name="cardType" length="20"/>
</subclass>
<subclass name="com.iquickinfo.DebitCard" discriminator-value="debitcard">
<property name="pinNumber"/>
<property name="cardName" length="20"/>
</subclass>
</class>
</hibernate-mapping>
[/xml]
The
generator class sub element of id utilized to produce the unique identifier for the objects of persistence class. There are numerous generator classes characterized in the Hibernate Framework. All the generator classes actualizes the
org.hibernate.id.IdentifierGenerator interface.
- Configure the mapping file in 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="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
<property name="connection.username">system</property>
<property name="connection.password">system</property>
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="show_sql">true</property>
<mapping resource="payment.hbm.xml"/>
</session-factory>
</hibernate-configuration>
[/xml]
Properties |
Description |
hibernate.connection.driver_class |
The JDBC driver class. |
hibernate.dialect |
This property makes Hibernate generate the suitable SQL for the picked database. |
hibernate.connection.url |
The JDBC URL to the database instance. |
hibernate.connection.username |
The database username. |
hibernate.connection.password |
The database password. |
hibernate.connection.pool_size |
Limits the number of connections waiting in the Hibernate database connection pool. |
hibernate.connection.autocommit |
Allows autocommit mode to be used for the JDBC connection. |
- Create the SPLessons class and stores the all POJO class objects, to perform the database operations.
SPLessons.java
[java]package com.itoolsinfo;
import javax.security.auth.login.Configuration;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
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(3);
payment.setAmount(20000);
CreditCard creditcard=new CreditCard();
creditcard.setCardNumber(5678);
creditcard.setCardType("visa");
DebitCard debitcard=new DebitCard();
debitcard.setPinNumber(6785);
debitcard.setCardName("KARUR VYSYA 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 entered using annotations");
}
}
[/java]
SessionFactory is an interface, which is available in
“org.hibernate” package. Session factory is long live multithreaded object. Usually one session factory should be created for one database. When you have multiple databases in your application you should create multiple SessionFactory object.
Output table appears in database table. This can be done using following sql command.
select * from SalaryDetails;