Hibernate 3
Hibernate 3
Presented by : A.Aziz
aaziz@roadtodata.com
PART-2 ( Example )
What is Hibernate?
open source object/relational mapping
technology (ORM)
easier to build robust,high-performance
database applications with Java.
Hibernate applications are cheaper, more
portable, and more resilient to change
Better system architecture (When separate database component from
the Business logic and the logic from the persistence mechanism you can more easily apply
changes to one part without influencing the other parts. ) – plug-and-play components
architecture
How it works
<hibernate-configuration>
<session-factory>
<property name="connection.username">sa</property>
<property name="connection.url">
jdbc:jtds:sqlserver://R2DDEV2:1433/hibernatertd
</property>
<property name="dialect">
org.hibernate.dialect.SQLServerDialect
</property>
<property name="connection.password">dev123</property>
<property name="connection.driver_class">
net.sourceforge.jtds.jdbc.Driver
</property>
</session-factory>
</hibernate-configuration>
Hibernate architecture main
components
Connection Management :
Hibernate Connection management service provide efficient management of the
database connections. Database connection is the most expensive part of
interacting with the database as it requires a lot of resources of open and close the
database connection [Hibernate supports a variety of connection pooling mechanisms: c3p0, Apache DBCP, Proxool ]
Session (org.hibernate.Session) :
A session is a connected object representing a conversation between the
application and the database. It wraps a JDBC connection and can be used to
maintain the transactions. It also holds a cache of persistent objects,used when
navigating the objects or looking up objects by identifier.
Transaction (org.hibernate.Transaction) :
Transactions are used to denote an atomic unit of work that can be committed (saved)
or rolled back together. A transaction can be started by calling
session.beginTransaction() which actually uses the transaction mechanism of
underlying JDBC connection,JTA or CORBA
Package com.prtd.hibernate;
public class Customer implements java.io.Serializable {
…….
}
CREATE TABLE CUSTOMER(
CUSTOMER_ID NUMBER PRIMARY KEY,
Mapping File (user.hbm.xml)
FIRST_NAME VARCHAR(20),
LAST_NAME VARCHAR(20), Package com.prtd.hibernate;
AGE NUMBER, public class Customer {
EMAIL VARCHAR(40)
); private Integer customerId= 0 ;
private String firstName= "";
private String lastName= "";
private Integer age= 0;
private String email= "";
<?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=" com.prtd.hibernate.Customer" table="USERS" >
<id name=“customerId" type="java.lang.Integer" column=“CUSTOMER_ID" >
<generator class=“native" />
</id>
<property name="firstName" type="java.lang.String" column=“FIRST_NAME"
length="20" />
<property name="lastName" type="java.lang.String" column=“LAST_NAME"
length="20" />
<property name="age" type="java.lang.Integer" column=“AGE" length="-1" />
<property name="email" type="java.lang.String" column=“EMAIL" length="40" />
</class>
</hibernate-mapping>
Configuration file (hibernate.cfg.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.username">sa</property>
<property name="connection.url">
jdbc:jtds:sqlserver://R2DDEV2:1433/hibernatertd
</property>
<property name="dialect">
org.hibernate.dialect.SQLServerDialect
</property>
<property name="connection.password">dev123</property>
<property name="connection.driver_class">
net.sourceforge.jtds.jdbc.Driver
</property>
</session-factory>
</hibernate-configuration>
So far…
we have written a Customer class (Customer.java) that we want to
persist and
a mapping file (Customer.hbm.xml) that describes the relationship of
each of the attributes of the class to the database columns.
We have also configured the Hibernate configuration file hibernate.cfg
.xml) to use the Customer.hbm.xml.
Now it is the time to write a DAO that can perform different operations on
the User object.
Data Access Object (DAO)
package com.prtd.hibernate;
import org.hibernate.Session;
import com.prtd.hibernate.Customer;
Transaction tx = session.beginTransaction();
session.save(transientInstance);
tx.commit();
System.out.println("Saved Successfully");
System.out.println(“Errror : "+re);
}
}
}
TestClient
package com.prtd.hibernate.test;
import java.util.Iterator;
import com.prtd.hibernate.Customer;
import com.prtd.hibernate.CustomerDAO;
import com.prtd.hibernate.PhoneNumbers;
// DAOs
CustomerDAO customerDAO = new CustomerDAO();
// Customer
Customer customer = new Customer();
// customer properties
customer.setFirstName("ABC");
customer.setLastName("XYZ");
customer.setAge(22);
customer.setEmail("any@roadtodata.com");
// save customer
customerDAO.save(customer);
Output..
ID : 42
Name : ABC XYZ
Email : any@roadtodata.com
Mapping
Association mapping
Mapping Associations
Association is a relationship of one class
to another class known as
'relationships' in database terminology
One-to-one
One-to-many
Many-to-many
Example
Table: PHONE_NUMBERS
……
}
PhoneNumbers.java
package com.prtd.hibernate;
</class>
</hibernate-mapping>
TestClient
package com.prtd.hibernate.test;
import java.util.Iterator;
import com.prtd.hibernate.Customer;
import com.prtd.hibernate.CustomerDAO;
import com.prtd.hibernate.PhoneNumbers;
// DAOs
CustomerDAO customerDAO = new CustomerDAO();
// Customer
Customer customer = new Customer();
// customer properties
customer.setFirstName("ABC");
customer.setLastName("XYZ");
customer.setAge(22);
customer.setEmail("any@roadtodata.com");
// Home phone
PhoneNumbers phoneNumber1 = new PhoneNumbers();
phoneNumber1.setNumberType("Home");
phoneNumber1.setPhone("020734567654");
phoneNumber1.setCustomer(customer);
// Office phone
PhoneNumbers phoneNumber2 = new PhoneNumbers();
phoneNumber2.setNumberType("Office");
phoneNumber2.setPhone("07816762345");
phoneNumber2.setCustomer(customer);
// Other phone
PhoneNumbers phoneNumber3 = new PhoneNumbers();
phoneNumber3.setNumberType("Other");
phoneNumber3.setPhone("0852675234");
phoneNumber3.setCustomer(customer); // for customer
// save customer
customerDAO.save(customer);
// Find Customer
Customer foundCustomer = customerDAO.findById(customer.getCustomerId());
System.out.print("\n");
// Delete
customerDAO.delete(foundCustomer);
Output
Customer found
ID : 42
Name : ABC XYZ
Email : any@roadtodata.com
Phone Numbers :
Home : 020734567654
Office : 07816762345
Other : 0852675234
Documentation (\\rotoda\Development\prtd_wip\Introduction_to_hibernate\doc )
WEB
http://www.hibernate.org/hib_docs/reference/en/html/tutorial.html
Questions?
END