SlideShare a Scribd company logo
Basic

Fundamentals

Hibernate: An Introduction
Cao Duc Nguyen
nguyen.cao-duc@hp.com

Software Designer
HP Software Products and Solution

May 3, 2012

Conclusion
Basic

Fundamentals

Talk Outline

1

Basic
Getting Started
Problem
Challenges

2

Fundamentals
Concepts
Data Model
Persistent Lifecycle
HQL
Architecture

3

Conclusion

Conclusion
Basic

Fundamentals

Getting Started

Setting up the Environment

JDK
Ant/Maven
MySQL/PostgreSQL/Oracle, etc.
JDBC
Eclipse Hibernate Tools

Conclusion
Basic
Getting Started

Hibernate ORM distribution

Fundamentals

Conclusion
Basic

Fundamentals

Conclusion

Problem

Hibernate - Definitions

by Hibernate.org
Hibernate is concerned with helping your
application to achieve persistence. . . Persistence
simply means that we would like our application’s
data to outlive the applications process. . . would
like the state of (some of) our objects to live
beyond the scope of the JVM so that the same
state is available later.
by Wikipedia.org
Hibernate is an object-relational mapping (ORM)
library for the Java language, . . . a framework for
mapping an object-oriented domain model to a
traditional relational database . . .
Basic
Problem

Traditional View

Fundamentals

Conclusion
Basic
Problem

Traditional View (cont.)

Fundamentals

Conclusion
Basic
Problem

Hibernate View

Fundamentals

Conclusion
Basic

Fundamentals

Challenges

Two Different Worlds

Object-Oriented Systems
System composed of objects interacting with each other
Objects encapsulate data and behaviors
Relational Databases
Data is stored in tables composed of rows

Conclusion
Basic

Fundamentals

Challenges

Obstacles

Identity
Granularity
Associations
Navigation
Inheritance & Polymorphism
Data type mismatches

Conclusion
Basic

Fundamentals

Conclusion

Concepts

Plain Old Java Object (POJOs)

In general, a POJO is a Java object not bound by any restriction
other than those forced by the Java Language Specification.
However, due to technical difficulties and other reasons, in the
context of Hibernate, a POJO is defined as follow:
No-argument class constructor
Property accessor (get/set) methods
Class is not declared final nor has final methods.
Collection-typed attributes must be declared as interface
types.
Basic

Fundamentals

Conclusion

Data Model

Example Application: EventApp

Event-management application used to manage a conference
with speakers, attendees, and various locations, among other
things.
Basic
Data Model

An Example: EventApp

Fundamentals

Conclusion
Basic

Fundamentals

Conclusion

Data Model

Identity Mapping

<id name="id" column="id" type="long">
<generator class="native"/>
</id>
Mapped classes must declare the primary key column of
the database table.
Generators using the native class will use identity or
sequence columns depending on available database
support. If neither method is supported, the native
generator falls back to a high/low generator method to
create unique primary key values.
The native generator returns a short, integer, or long value.
Hibernate documentation about Identity mapping here.
Basic

Fundamentals

Conclusion

Data Model

Property Mapping

<property name="startDate" column="start_date"
type="date"/>
A typical Hibernate Property mapping defines a POJO
property name, a database column name, and the name of
a Hibernate type, and it is often possible to omit the type.
Hibernate uses reflection to determine the Java type of the
property.
Details about Hibernate Types mapping here.
Hibernate documentation about Property mapping here.
Basic

Fundamentals

Conclusion

Data Model

Entity Mapping

<class name="Event" table="events">
<!-- define identity, properties, components,
collections, associations here... -->
</class>
A typical Hibernate Entity mapping defines a POJO class
name, a database table name.
By default, all class names are automatically “imported”
into the namespace of HQL
Hibernate documentation about Entity mapping here.
Basic

Fundamentals

Conclusion

Data Model

Component Mapping

<component name="componentName"
class="componentClass">
<!-- defines properties of the component here
these properties will be mapped to columns of
the enclosing entity-->
</class>
A Hibernate Component mapping is defined within an
Entity mapping several objects into one single table of the
enclosing entity.
Hibernate documentation about Component mapping here.
Basic

Fundamentals

Data Model

Put it all together
Hibernate Mapping files:

The Event.hbm.xml mapping file

Conclusion
Basic

Fundamentals

Data Model

Put it all together (cont.)

The Location.hbm.xml mapping file

Conclusion
Basic

Fundamentals

Data Model

Put it all together (cont.)
Hibernate Configuration hibernate.cfg.xml file:

Conclusion
Basic

Fundamentals

Conclusion

Data Model

Collection Mapping

Common Collections: sets, lists, bags, maps of value types.
Value Type:
An object of value type has no database identity; it belongs
to an entity instance, and its persistent state is embedded
in the table row of the owning entity
Value-typed classes do not have identifiers or identifier
properties
Basic

Fundamentals

Data Model

Collection Mapping (cont.)

Hibernate persistent collections

Conclusion
Basic

Fundamentals

Data Model

Collection Mapping (cont.)

Example of persisting collections

Conclusion
Basic

Fundamentals

Data Model

Inheritance Mapping

Table per concrete class with union:

Conclusion
Basic

Fundamentals

Data Model

Inheritance Mapping (cont.)

Table per class hierarchy:

Conclusion
Basic

Fundamentals

Data Model

Inheritance Mapping (cont.)

Table per subclass:

Conclusion
Basic

Fundamentals

Conclusion

Persistent Lifecycle

Object States

Transient
The object is not associated with any persistence
context. It has no persistent identity (primary key
value).
Persistent
The object is currently associated with a
persistence context. It has a persistent identity
(primary key value) and, perhaps, a corresponding
row in the database. Hibernate guarantees that
persistent identity is equivalent to Java identity.
Basic

Fundamentals

Conclusion

Persistent Lifecycle

Object States (cont.)

Detached
The instance was once associated with a
persistence context, but that context was closed,
or the instance was serialized to another process.
It has a persistent identity and, perhaps, a
corrsponding row in the database. For detached
instances, Hibernate makes no guarantees about
the relationship between persistent identity and
Java identity
Basic
Persistent Lifecycle

State Transition Diagram

Fundamentals

Conclusion
Basic

Fundamentals

Conclusion

Persistent Lifecycle

Case 1: Making an object persistent
Item item = new Item();
item.setName("Playstation3 incl. all accessories");
item.setEndDate( ... );
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Serializable itemId = session.save(item);
tx.commit();
session.close();
Basic

Fundamentals

Conclusion

Persistent Lifecycle

Case 2: Retrieving a persistent object
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Item item = (Item) session.load(Item.class,
new Long(1234));
// Item item = (Item) session.get(Item.class,
// new Long(1234));
tx.commit();
session.close();
Basic

Fundamentals

Conclusion

Persistent Lifecycle

Case 3: Modifying a persistent object
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Item item = (Item) session.get(Item.class,
new Long(1234));
item.setDescription("This Playstation is
as good as new!");
tx.commit();
session.close();
Basic

Fundamentals

Conclusion

Persistent Lifecycle

Case 4: Making a persistent object transient

Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Item item = (Item) session.load(Item.class,
new Long(1234));
session.delete(item);
tx.commit();
session.close();
Basic

Fundamentals

Conclusion

Persistent Lifecycle

Case 5: Reattaching a modified detached instance
// Loaded in previous Session
item.setDescription(...);
Session sessionTwo = sessionFactory.openSession();
Transaction tx = sessionTwo.beginTransaction();
sessionTwo.update(item);
item.setEndDate(...);
tx.commit();
sessionTwo.close();
Basic

Fundamentals

HQL

Query

HQL Query:

HQL SQLQuery:

Conclusion
Basic
HQL

Parameter Binding

Fundamentals

Conclusion
Basic

Fundamentals

HQL

Joins
Join:

Left Join:

Conclusion
Basic

Fundamentals

Conclusion

HQL

Criteria API

Some developers prefer to build queries dynamically, using an
object-oriented API, rather than building query strings.
Hibernate provides an intuitive org.hibernate.Criteria
represents a query against a particular persistent class:
Basic

Fundamentals

HQL

DetachedCriteria
A DetachedCriteria is used to express a subquery.

Conclusion
Basic

Fundamentals

Architecture

Structural Components

More in depth explanation can be found here.

Conclusion
Basic

Fundamentals

Architecture

Hibernate Flexibility and Extendibility

Extension points:
Dialects (for different databases)
Custom mapping types
ID generators
Cache, CacheProvider
Transaction, TransactionFactory
PropertyAccessor
ProxyFactory
ConnectionProvider

Conclusion
Basic

Fundamentals

Why Hibernate?

Free, open source Java package
Release developers from data persistent related tasks,
help to focus on objects and features of application
No need for JDBC API for Result handling
Database almost-independence
Efficient queries

Conclusion
Basic

Fundamentals

THANK YOU *-*

Conclusion

More Related Content

PPT
Hibernate Tutorial
PPT
Intro To Hibernate
PPT
Introduction to hibernate
PPTX
Hibernate
PPT
hibernate with JPA
PPTX
Introduction to Hibernate Framework
PDF
Introduction to JPA and Hibernate including examples
Hibernate Tutorial
Intro To Hibernate
Introduction to hibernate
Hibernate
hibernate with JPA
Introduction to Hibernate Framework
Introduction to JPA and Hibernate including examples

What's hot (20)

PPTX
Hibernate in Nutshell
PPT
Java Persistence API (JPA) Step By Step
PPT
jpa-hibernate-presentation
PPTX
Introduction to JPA Framework
PPTX
Hibernate Basic Concepts - Presentation
PPTX
Spring (1)
PPTX
Hibernate ppt
PPTX
JPA For Beginner's
ODP
Hibernate complete Training
PPTX
Hibernate working with criteria- Basic Introduction
PPT
Entity Persistence with JPA
PPTX
Hibernate inheritance and relational mappings with examples
PPTX
Introduction to JPA (JPA version 2.0)
PDF
JPA and Hibernate
PDF
Java persistence api 2.1
PPTX
Hibernate in Action
PDF
Hibernate Presentation
ODP
JPA Best Practices
PDF
Hibernate Tutorial
Hibernate in Nutshell
Java Persistence API (JPA) Step By Step
jpa-hibernate-presentation
Introduction to JPA Framework
Hibernate Basic Concepts - Presentation
Spring (1)
Hibernate ppt
JPA For Beginner's
Hibernate complete Training
Hibernate working with criteria- Basic Introduction
Entity Persistence with JPA
Hibernate inheritance and relational mappings with examples
Introduction to JPA (JPA version 2.0)
JPA and Hibernate
Java persistence api 2.1
Hibernate in Action
Hibernate Presentation
JPA Best Practices
Hibernate Tutorial
Ad

Viewers also liked (13)

RTF
THE WORLDS NO.1 BLACK MAGIC EXPERT WITH POWERFUL LOVE SPELLS +27631229624
PPTX
Introduction to Spring Framework
PPT
02 Hibernate Introduction
PPT
Hibernate
PDF
Hibernate an introduction
PPTX
Introduction to Hibernate Framework
PDF
Introduction To Hibernate
PPT
Hibernate presentation
PPT
Introduction to hibernate
PDF
Hibernate ORM: Tips, Tricks, and Performance Techniques
PPS
Java Hibernate Programming with Architecture Diagram and Example
ODP
Hibernate Developer Reference
PPTX
Slideshare ppt
THE WORLDS NO.1 BLACK MAGIC EXPERT WITH POWERFUL LOVE SPELLS +27631229624
Introduction to Spring Framework
02 Hibernate Introduction
Hibernate
Hibernate an introduction
Introduction to Hibernate Framework
Introduction To Hibernate
Hibernate presentation
Introduction to hibernate
Hibernate ORM: Tips, Tricks, and Performance Techniques
Java Hibernate Programming with Architecture Diagram and Example
Hibernate Developer Reference
Slideshare ppt
Ad

Similar to Hibernate An Introduction (20)

PPT
Patni Hibernate
PPT
5-Hibernate.ppt
PDF
2014 Pre-MSc-IS-3 Persistence Layer
PPT
PPTX
Advanced Hibernate V2
PDF
Ejb3 Struts Tutorial En
PDF
Ejb3 Struts Tutorial En
PDF
Hibernate 3
DOC
Advanced Hibernate Notes
PPTX
Hibernate Training Session1
PDF
inf5750---lecture-2.-c---hibernate-intro.pdf
PDF
Local data storage for mobile apps
PPT
Hibernate
PPTX
Real World MVC
PPT
DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS.ppt
PPT
Basic Hibernate Final
PDF
Java Web Programming on Google Cloud Platform [2/3] : Datastore
PPT
04 Data Access
PPTX
NET Systems Programming Learned the Hard Way.pptx
PDF
Introduction to Datastore
Patni Hibernate
5-Hibernate.ppt
2014 Pre-MSc-IS-3 Persistence Layer
Advanced Hibernate V2
Ejb3 Struts Tutorial En
Ejb3 Struts Tutorial En
Hibernate 3
Advanced Hibernate Notes
Hibernate Training Session1
inf5750---lecture-2.-c---hibernate-intro.pdf
Local data storage for mobile apps
Hibernate
Real World MVC
DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS.ppt
Basic Hibernate Final
Java Web Programming on Google Cloud Platform [2/3] : Datastore
04 Data Access
NET Systems Programming Learned the Hard Way.pptx
Introduction to Datastore

Recently uploaded (20)

PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
KodekX | Application Modernization Development
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
cuic standard and advanced reporting.pdf
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Machine learning based COVID-19 study performance prediction
Per capita expenditure prediction using model stacking based on satellite ima...
Encapsulation_ Review paper, used for researhc scholars
KodekX | Application Modernization Development
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
The Rise and Fall of 3GPP – Time for a Sabbatical?
NewMind AI Weekly Chronicles - August'25 Week I
Unlocking AI with Model Context Protocol (MCP)
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
cuic standard and advanced reporting.pdf
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Review of recent advances in non-invasive hemoglobin estimation
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Machine learning based COVID-19 study performance prediction

Hibernate An Introduction