Hibernate
Hibernate
• The Problem
• Object-Relational Mapping
• Hibernate Core Concepts
• Hibernate Architecture
• Hibernate Mappings
• Configuring Hibernate
• Working with Persistent Objects
• Persistence Lifecycle
• 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
• 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
• 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
• 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
• 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
• 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
<hibernate-mapping
package="com.integrallis.tcms.domai •Class/Table
n"> •How to generate PK
<class name="Conference"> •Other field mappings
<id column="PK_ID" name="Id“ •Java Set for the
type="integer"> Tracks
<generator class="identity" /> •Class on the many
</id> side
... •Foreign Key on the
<set name="Tracks" inverse="true" many side
cascade="all" lazy="false">
<one-to-many class="Track" />
<key
column="FK_CONFERENCE_ID" />
• One-to-one
– Maintained with Foreign Keys in
Database
• One-to-many / Many-to-one
– Object on the ‘one’ side
– Collection on the many ‘side’
• Many-to-Many
– Use a ‘mapping’ table in the database
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();
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
<class name=“ReferenceData”
table=“REFDATA”>
<cache usage=“read-only” />
..
</class>
<class name=“ReadMostly”
table=“READMOSTLY”>
<cache usage=“nonstrict-read-write” />
..
</class>