You are on page 1of 23

Hibernate-an ORM Tool

Submitted By:
Davinder Singh
Objectives

• To understand the basic concept of


Hibernate
• To understand how to use Hibernate in
your applications
Need
• Today’s programming language takes advantage of the
latest in object-oriented techniques.
• As an object is a construct to enforce encapsulation, the
problem was how to store an object for use by the same
application at a later time or by another application.
• Some of the solutions to this problems are:
– Serialization
– XML
– Object-oriented database systems mapping
• These three solutions would have worked but they
presented an issue when put into the mix of legacy
applications and traditional relational database systems
like Oracle, SQL Server. This problem was solved by
mapping technique.
Mapping technique
• Mapping is a technique that places an object’s Attribute
in one or more fields of a database table.
• Object/Relational Mapping is a means of taking
information from a SQL Database and turn it into objects,
and vice-versa.
• Solutions to persistent data in relational database with
Java are Java Object serialization, JDBC, Enterprise
JavaBean and Persistence Frameworks.
• There are a lot of solutions (tools and frameworks) on
the market to help programmers within their mapping
work. One of the best tools is Hibernate
Facts about Hibernate…..
• Hibernate is a Professional Open Source project
and a critical component of the JBoss Enterprise
Middleware System (JEMS) suite of products.
• The Hibernate distribution license is GNU LGPL.
Hibernate can be used in all open source and
commercial applications without limitations.
• Hibernate was published for the first time 5
years ago.
• Hibernates goal is to relieve the developer from
95 percent of common data persistence related
programming tasks, compared to manual coding
with SQL and the JDBC API.
Hibernate- Architecture
• A (very) high-level view of the Hibernate
architecture
APPLICATION
Persistent Objects

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

JNDI JDBC JTA

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

• Hibernate can manage the persistence of object


collections, whether they are Sets, Maps, Lists, arrays of
objects or primitive values. It also allows another form of
collection called a "bag".
• A bag can be mapped to a Collection or List, and
contains an unordered, unindexed collection of entities.
Bags can contain the same element many times.
• Java type of a property holding a collection must be the
interface type i.e. Map never HashMap and so on.
• Obey the usual rules for value types : No shared
references, created & deleted along with containing
entity.
Persistent Collections
• Don’t support Null value semantics.
• Persisted when referenced by a persistent object
& automatically deleted when unreferenced.
• Collection instances are distinguished in the
database by a foreign key to the owning entity
i.e. Collection Key.
• An object in a collection can either be handled
with “ Pass by value” semantics or it can be a
reference to another entity with an own lifecycle.
Persistent Collections
• Collections may not contain other collections :
The contained type is referred to as the “
Collection element type “.
• Collection elements are mapped by :
<element>, <composite-element>, <one-to-
many>, < many-to-many>, <many-to-any>.
• Lazy Initialization : Load their state from the
database only when the application needs to
access it.
Persistent Collections
Bi-Directional Associations :
• It allows navigation from both “ends” of the
association.
• Two kinds of bidirectional association :
1) One – To – Many
Set or bag valued at one end, single-
valued at the other.
2) Many – To – Many
Set or bag valued at both ends.
HQL-Hibernate Query Language
• Features :
• Hibernate automatically generates the SQL query
& execute queries against underlying database.
• Based on ‘Relational-Object Models'. Makes SQL
Object-Oriented.
• Uses Classes & Properties instead of Tables &
Columns.
• Supports Polymorphism & Associations.
• Supports advanced features.
• Case Insensitive ; Except for the names of Java
Classes & properties.
HQL-Hibernate Query Language
• HQL consists of :
• Clauses :
- From, select, Where, Order By, Group By.
• Aggregate functions :
- avg(..), sum(..), min(..), max(..), count(..)
• Sub queries :
- Query within a query
Hibernate Transactions

• 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

You might also like