1. Introduction to
Hibernate
Ashish Kulkarni
1
11 March 2009 www.ashishkulkarni.com
2. How this presentation is organised
A brief history of Relational Databases
The Object Oriented Paradigm
The Problem
Solutions
Demo - The Hibernate Way
Beyond Hibernate
Questions
Welcome anytime as long as you can accept the following
answers:
I don’t know but I will find out
Can we park that question and take it up after this presentation?
2
www.ashishkulkarni.com
11 March 2009
3. What this Presentation is about
There is a lot of documentation, books and
examples on Hibernate.
This Presentation doesn’t try to duplicate
these. Instead, it tries to show what
Hibernate is capable of with the help of
real-life examples.
3
www.ashishkulkarni.com
11 March 2009
4. A brief history of Relational Databases
Relational Databases have been around for a
long time
Out of all other database models such as
Hierarchical, Network, Object, etc., Relational
Databases have come to rule the Database
world
They have come to support a vast array of
object types and languages and have gained
vast amount of industry support in terms of the
sheer number of databases as well as tools
4
www.ashishkulkarni.com
11 March 2009
5. The Object Oriented Paradigm
Java is an Object Oriented language
(duh).
It revels in Object Orientation. POJOs or
Plain Old Java Objects form a clean way
of storing and passing information within
an application.
5
www.ashishkulkarni.com
11 March 2009
6. The Problem
While Java supports a vast array of databases
using JDBC, performing any database actions
for storing, updating and retrieving data using
JDBC can be very cumbersome
While this can be interesting for extremely small
projects, it can put severe constraints on a large
scale project and make any change very costly
6
www.ashishkulkarni.com
11 March 2009
7. The Problem (continued)
While a lot of things such as managing database
connections can be automated by means of
libraries, we still need to write a considerable
amount of code for storing and retrieving data
from individual tables, setting field level
parameters at every stage
In addition, we need to create and manage
transactions
Transferring Java data types back and forward
to Database data types is another issue
7
www.ashishkulkarni.com
11 March 2009
8. The Problem (continued)
Managing relations can be even more
tricky with plain JDBC. Just imagine
writing code to persist a Parent Java
object that Contains an ArrayList or
HashSet of Child Java objects within a
single transaction. And then, to retrieve it
all back into a Java Object Graph.
8
www.ashishkulkarni.com
11 March 2009
9. Solutions and Issues
Some of the early solutions were:
CMP EJBs
The issue with CMP EJBs was latency due to Lazy
Loading, among others
Custom/Homegrown ORM Frameworks
Maintenance of such homegrown frameworks can
be expensive
9
www.ashishkulkarni.com
11 March 2009
10. Demo – The Hibernate Way
This demo will show CRUD (Create,
Retrieve, Update and Delete) operations
using Hibernate in the following scenarios:
Single table with assigned ID
Parent-Child relationship
Single table with auto-generated ID
Many-Many relationship
10
www.ashishkulkarni.com
11 March 2009
11. What Hibernate Tools can do
Hibernate Tools can generate Java
Classes based on database structure.
These Java Classes are referred to as
Entities.
Hibernate Tools can either generate
Annotations within the Entities or a
separate hbm.xml files for each Entity.
11
www.ashishkulkarni.com
11 March 2009
12. What Hibernate can do
Hibernate Allows a Java Developer to
work with Pure POJOs within the
application without having to worry about
persistence or database dialect.
Hibernate can manage the complete
lifecycle of an object/entity.
12
www.ashishkulkarni.com
11 March 2009
14. Beyond Hibernate
With EJB 3 and JPA, Sun has incorporated
much of Hibernate into the standard. Hibernate
and TopLink are the 2 available Persistence
Providers for JPA, Hibernate being the strong
favourite.
Hibernate itself is still expanding. Hibernate
Search brings full-text indexes and Google-like
search capabilities to any traditional database
application.
14
www.ashishkulkarni.com
11 March 2009
15. Questions
15
www.ashishkulkarni.com
11 March 2009
16. Where to go from here
Me – ashish@ebizss.com
My website – www.ashishkulkarni.com
www.hibernate.org
Books:
Manning – Java Persistence with Hibernate – Bauer and King
16
www.ashishkulkarni.com
11 March 2009
17. A bit about me
I am a Java Contractor currently contracting with JP Morgan in
Glasgow. Apart from contracting, I am actively working with other
developers at my company to develop bespoke software for private
clients.
I have been using JBoss Seam, Spring and Hibernate driven JPA for
a couple of years now and am absolutely loving it.
Having worked on FoxPro, Oracle, Visual C++ and SAP in the past,
I have 15 years of development experience.
I have been using Java since 2000 which to this day remains my
favourite computer language.
I have worked in a variety of industries such as Retail Banking,
Investment Banking, Financial Institutions, Pharmaceuticals,
Manufacturing, Automobiles and Telecommunications.
17
www.ashishkulkarni.com
11 March 2009