Professional Documents
Culture Documents
Mats Strandberg
v 6, 2006-09-28
Presentation
Impedance mismatch What is ORM When/why use ORM Understand Hibernate basics See Java using Hibernate API See SQL Understand Hibernate features Ask questions
v 6, 2006-09-28
Mats Strandberg
Mats Strandberg
Event
0..n
0..n
Venue
0..n
0..n
Participant
Address
v 6, 2006-09-28
Mats Strandberg
Venue
name 0..n 1
Address
street city
Participant
name
v 6, 2006-09-28
Mats Strandberg
Object Diagram
hibernateRd: Event
name=Hibernate RD date=20060929 9.00
theVenue : Venue
name=rum 1537
v 6, 2006-09-28
Mats Strandberg
v 6, 2006-09-28
Mats Strandberg
Relational Schema
Participants
* ParticipantId name
EventParticipations
* ParticipantId * EventId
Events
* EventId name
Addresses
* AddressId street city
Venues
* VenueId name VenueAddress
date EventVenue
v 6, 2006-09-28
Mats Strandberg
Example execution
v 6, 2006-09-28
Mats Strandberg
On Root Objects
In an object graph usually there s roots, where navigation starts.
Root
v 6, 2006-09-28
Mats Strandberg
Navigation
Event
name date
Venue
name
Address Participant
name street city
event.getVenue().getAddress().getStreet();
SELECT street FROM Addresses WHERE AddressId= (SELECT VenueAddress FROM Venues WHERE VenueId= (SELECT EventVenue FROM Events WHERE EventId=1));
v 6, 2006-09-28
Mats Strandberg
Event
name
Venue
name
Query
Get streets in Stockholm
date
Address Participant
name street city
Mats Strandberg
Object Queries
Roots may not be enough for search We need at least class extension or OQL
v 6, 2006-09-28
Mats Strandberg
Event
name
Venue
name
HQL
Get street in Stockholm
date
Address Participant
name street city
v 6, 2006-09-28
Mats Strandberg
v 6, 2006-09-28
Mats Strandberg
Why a Database?
Need for persitent state Support for transactions Large data sets Multiple concurrent applications share data Data distribution Usually disk based (persistence with single node) Only add complexity to solve a real problem
v 6, 2006-09-28
Mats Strandberg
v 6, 2006-09-28
Mats Strandberg
v 6, 2006-09-28
Mats Strandberg
Alternatives to ORM
Hand coded persistance layer Serialization EJB/CMP OODB
v 6, 2006-09-28
Mats Strandberg
Hibernate
Open Source LGPL Licence http://hibernate.org
v 6, 2006-09-28
Mats Strandberg
Hibernate is non-intrusive
This means: Persistence is orthogonal to class Persisting a instance is a run-time decision
v 6, 2006-09-28
Mats Strandberg
v 6, 2006-09-28
Mats Strandberg
v 6, 2006-09-28
Mats Strandberg
Impedance Mismatch
Identity Granularity Object navigation Subtypes Polymorphic associations
v 6, 2006-09-28
Mats Strandberg
Inheritance
A
a1
B
* id a1 b1
C
* id a1 c1
B
b1
C
c1
A
* id a1 b1 c1
B
* id b1
C
* id c1
v 6, 2006-09-28
Mats Strandberg
Application Transactions
A.k.a. long running transactions An object graph can be detached Updates can be done while detached The object graph can later be attached to a session session.close() detaches the objects session.update(object) attaches the object NOTE: Other updates may be clobbered!
v 6, 2006-09-28
Mats Strandberg
v 6, 2006-09-28
Mats Strandberg
Venue
name
Address
street city
isA Venue$
name
v 6, 2006-09-28
Mats Strandberg
Consider Performance
Iterating over a Class (Event) that has a n to m association
List list = session.createQuery("from Event").list(); for (Iterator i = list.iterator(); i.hasNext(); ) { Event event = (Event) i.next(); out.println("EVENT name:" + event.getName()); }
v 6, 2006-09-28
Mats Strandberg
Lazy initialization
A Proxy is used Getters are overridden, e.g. Event.getName(); The Proxy is a subclass of your persistent class, e.g extends Event Requires build-time bytecode instrumentation Beware of explicit comparison of runtime class, passing of class objects etc.
v 6, 2006-09-28
Mats Strandberg
Caching
First-level Cache Session
Query Cache
v 6, 2006-09-28
Mats Strandberg
v 6, 2006-09-28
Mats Strandberg
v 6, 2006-09-28
Mats Strandberg
Roadmap
Production: Hibernate 3.1 NHibernate 1.0 for :NET (Hibernate 2.1) Development: Hibernate 3.2 (EJB 3.0) NHibernate 1.2 (.NET Framework 2.0) NHibernate 3.x? (Hibernate 3)
v 6, 2006-09-28
Mats Strandberg
Entity Manager
Entity Manager implements a complete EJB3 persistence provider (together with Hibernate Annotations) EJB-QL based on HQL Automatic Versioning Detached Entities Non-managed set-up is rather different EJB3 has a large number of persistence contexts
v 6, 2006-09-28
Mats Strandberg
Features
Two Level Cache Locking strategies (e.g. Optimistic Locking) Application Transactions Composition Inheritance Polymorphism Persistence by reachability Fetching strategies: Lazy vs Eager Lazy initialization
v 6, 2006-09-28
Mats Strandberg
Books on Hibernate
Etc.
v 6, 2006-09-28
Mats Strandberg
Things to Mention
Locking JPA Java Persistence API Index Hibernate vs Manual ORM: When using Hibernate there is a standard for how the mapping has been done. This is good for maintenance Bidirectional relations are handled at code level
v 6, 2006-09-28
Mats Strandberg