Hibernate: Object-Relational Mapping With
Hibernate: Object-Relational Mapping With
Hibernate
Brian Sam-Bodden
Principal Partner
Integrallis Software, LLC.
August 1 - 5, 2005
Contents
• The Problem
• Object-Relational Mapping
• Hibernate Core Concepts
• Hibernate Architecture
• Hibernate Mappings
• Configuring Hibernate
• Working with Persistent Objects
• Persistence Lifecycle
Contents
• Working with Collections
• More on Association Mappings
• HQL – The Hibernate Query Language
• Query by Criteria
• Query by Example
• Caching
• Other Hibernate Features
• Conclusions
The Problem
Two Different Worlds
Object-Oriented Systems Relational Databases
• Represent a problem in • Efficient data storage,
terms of objects retrieval and integrity of
• Semantically richer, the data
encapsulate data and • Provide a “normalized”
behavior set of data
The Problem
Natural Divergence
• Early on in a system’s life, both models tend to
look alike
• As the system matures two models diverge
• They grow to do what they do best
– Object model gets richer
– Data model changes to accommodate
operational factors
The Problem
Object-Relational Mismatch
• Granularity
– Object Oriented vs. Database Types
– User-Defined Column Types (UDT) are not
portable
• Inheritance & Polymorphism
– Not supported in the relational model
– No polymorphic queries
– Columns are strictly typed
The Problem
Object-Relational Mismatch
• Identity Mismatch
– Object Identity vs. Object Equality
– Different objects can map to the same column
or columns
– Database Identity Strategies
– Natural Keys versus Surrogate Keys
The Problem
Object-Relational Mismatch
• Associations
– Object References versus Foreign Keys
– Directionality of Associations
– Tables associations are always one-to-many
or many-to-one
– Object associations can be many-to-many
The Problem
Object-Relational Mismatch
• Object Graph Navigation
– Object-oriented applications “walk” the
object graph
– Object-oriented systems access what they
need
– With database we need a “plan” of what to
retrieve
– N+1 Problem is a common symptom
Object-Relational Mapping
• Tools/techniques to store and retrieve objects from a
database
• From the code perspective it behaves like a virtual
object database
• A complete ORM solution should provide:
– Basic CRUD functionality
– An Object-Oriented Query Facility
– Mapping Metadata support
– Transactional Capability
Hibernate
Relational Persistence For Idiomatic Java
• Provides
– Transparent persistence
– Object-querying capabilities
– Caching
• Works with fine-grained POJO-based models
• Supports most Databases
• Created by Gavin King, now part of the JBoss Group
Hibernate
Relational Persistence For Idiomatic Java
• Declarative programming
• Uses Runtime Reflection
• Query Language is SQL-like
– Leverages programmer’s knowledge
• Mappings
– Typically defined as XML documents
• But you never have to write XML if you don’t
want to (XDoclet, JSE 1.5 Annots)
Hibernate
Relational Persistence For Idiomatic Java
Hibernate
Core Concepts
• Session Factory
– is a cache of compiled mappings for a single database.
– used to retrieve Hibernate Sessions
• Session
– Short lived
• Temporary bridge between the app and the data storage
– Provides CRUD operations on Objects
– Wraps a JDBC Connection / J2EE Data Source
– Serves as a first level object cache
Architecture
Architecture
Choices
} finally {
session.close();
}
Working with Persistent Objects
Deleting an Object
Session session = null; • Start a Session
Transaction tx = null; • Transaction is started
try { • The object is deleted
session = factory.openSession(); • The transaction is
tx = session.beginTransaction(); committed
session.delete(address); • The session is closed
tx.commit();
…
} finally {
session.close();
}
Persistence Lifecycle
Possible States
• Transient
– Not Associated with a database table
– Non-transactional
• Persisted
– Object with Database Identity
– Associates object with a Session
– Transactional
• Detached
– no longer guaranteed to be in synch with the
database
Persistence Lifecycle
Working with Collections
session = sessionFactory.openSession();
// query string – ‘Address’ refers to a Class not a Table
String queryString = "from Address";
Conference conference = …
session = sessionFactory.openSession();
// build a query string
String queryString = “from Tracks as t where t.Conference = :conf”;
// create, configure and execute the query
List addresses = session.createQuery(queryString)
.setObject(“conf”, conference)
.list();
Hibernate Query Language
HQL Parameters
• Named parameters removed positional problems
• May occur multiple times in the same query
• Self-documenting
Conference conference = …
session = sessionFactory.openSession();
• 1st Level
– Transaction scoped cache provided by the Session.
– Always available
• 2nd Level
– Process scoped cache
– Shared by all Sessions
– Optional
– Pluggable implementation, can be clustered
• Query
– Use with care, profile first
Caching in Hibernate
Caching in Hibernate
<class name=“ReferenceData”
table=“REFDATA”>
<cache usage=“read-only” />
..
</class>
Caching in Hibernate
<class name=“ReadMostly”
table=“READMOSTLY”>
<cache usage=“nonstrict-read-write” />
..
</class>
Caching in Hibernate
Our website
http://www.integrallis.com