Hibernate IntroPPT
Hibernate IntroPPT
Hibernate IntroPPT
Submitted By:
Davinder Singh
Objectives
HIBERNATE
hibernate- XML
properties Mapping
Database
Hibernate- Architecture
• The ‘‘lite’’ architecture
Transien
t
Objects Application
Application
Persisten
t Objects
Session
Session JDBC JNDI JTA
Factory
Database
Hibernate- Architecture
• Full architecture
Transien
t
Objects
Application
Persiste
nt
Objects
Session Factory
Session JNDI
Session Session
Database
Hibernate as an ORM tool
• Hibernate is an object/relational mapping (ORM)
tool that also provides data querying and
retrieval functions in a Java environment.
• Hibernate makes it easy to work with relational
databases – Oracle, MS SQL, MySQL and many
more are supported.
• Hibernate makes use of persistent objects
commonly called as POJO
• It provides libraries of classes which are able to
do mapping automatically
Functionality
• Connection Management
• Transaction management
• Object relational mapping
Features
• Transparent Persistence
• Flexible Object/Relational Mapping
• Object-Oriented Query Language
• Ultra High Performance
• Detached object support.
• Automatic primary key generation
Persistent Classes in Hibernate
• Hibernate works best if classes follow the POJO
programming model .
• Rules to create Persistent Classes
• Declare accessors and mutators for persistent fields
• Implement a no-argument constructor
• Provide an identifier property (optional)
• Prefer non-final Person
Personid : long
Name : string
Gender : string
Addresses : set
Person()
Person (long personid)
Person (long personid, String name, String gender,
Set addresses)
getPersonid()
setPersonid (long personid)
Basic O/R Mapping
<hibernate-mapping>
<class name=“com.sapient.dao.Person” table=“PERSON”>
<id name=“personid” type=“long”>
<column name=“PERSONID”/>
<generator class=“assigned”/>
</id>
<property name=“name” type=“string”>
<column name=“NAME” length=“50”/>
</property>
<set name=“addresses”>
<key>
<column name=“PERSONID” />
</key>
<one-to-many class=“com.sapient.dao.Address” />
</set>
</class>
</hibernate-mapping>
Persistent collections
• What is a Transaction?
– A single logical operation on the data is called a
Transaction.
– Example: Transfer of funds from one account to
another.
• A transaction is either managed by the
environment or by the application itself.
• Hibernate provides its own transaction API to
give the similar code look for both managed and
non-managed environment
Using the Hibernate Transaction
API
Session session = sessions.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
concludeAuction();
tx.commit();
} catch (Exception e) {
if ( tx != null ) {
try {
tx.rollback();
} catch (HibernateException he) {
}
}
throw e;
} finally {
try {
session.close();
} catch ( HibernateException he) {
throw he;
}
}
Advantages of Hibernate
• Hibernate Beans are easier to implement since
you don't need any interface coding.
• Queries can be dynamic and perform faster
• Hibernate offers a more object-oriented
approach. You can map “is-a” relationships as
subclasses.
• For data transfer you can use Hibernate Beans
as DTOs.
• Hibernate can be used as the persistence
framework for the shopping platform
References: Useful Links
• Web Links
• http://www.hibernate.org/
• http://www.onjava.com/pub/a/onjava/2004/01/14/hibernate.html
• http://www.hibernate.org/hib_docs/reference/en/pdf/hibernate_refere
nce.pdf
• www.hibernate.org/hib_docs/v3/reference/en/html/tutorial.html
• Books
– Hibernate in Action :
– Christian Bauer, Gavin King
– Hibernate : A J2EE(TM) Developer's Guide (Paperback)
by Will Iverson
– Pro Hibernate 3 (Expert's Voice) (Paperback)
by Jeff Linwood, Dave Minter