ORM With Hibernate - PowerPoint PPT Presentation

1 / 38
About This Presentation
Title:

ORM With Hibernate

Description:

for (Iterator iter = list.iterator(); iter.hasNext(); ) { System.out.println(''' iter.next() '' is in Stockholm'); Event. Participant. Venue ... – PowerPoint PPT presentation

Number of Views:1957
Avg rating:3.0/5.0
Slides: 39
Provided by: matsstr
Category:
Tags: orm | hibernate | iter

less

Transcript and Presenter's Notes

Title: ORM With Hibernate


1
ORM With Hibernate
2
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

3
Crisp Utbildningsdag (Crisp RD)
  • Crisp RD
  • Every second Friday, usually by employee
  • Some presentations at KTH
  • Mats Strandberg
  • Worked with several OODBs since 1990
  • Used RDBs in several OO projects
  • Used Hibernate in one commercial project
  • Worked with Java since 1996

4
Simple Domain Model
1
0..n
Event
Venue
0..n
0..n
1
0..n
Participant
Address
5
Simple Domain Model
1
0..n
Event
Venue
name
name
0..n
date
0..n
1
0..n
Address
Participant
street
name
city
6
Object Diagram
hibernateRd Event
theVenue Venue
namerum 1537
nameHibernate RD
date20060929 9.00
theAddress Address
mats Participant
streetOsquars Backe 2
nameMats Strandberg
cityStockholm
7
Example Code and Mapping File
8
Relational Schema
Participants
EventParticipations
ParticipantId
ParticipantId
EventId
name
Events
EventId
name
Addresses
Venues
date
AddressId
VenueId
EventVenue
street
name
city
VenueAddress
9
Example execution
10
On Root Objects
  • In an object graph usually theres roots, where
    navigation starts.

Root
11
Navigation
Event
Venue
name
name
date
Address
Participant
street
city
name
  • event.getVenue().getAddress().getStreet()

SELECT street FROM Addresses WHERE
AddressId(SELECT VenueAddress FROM Venues WHERE
VenueId(SELECT EventVenue FROM Events WHERE
EventId1))
12
Query
Event
Venue
name
name
date
Address
Get streets in Stockholm
Participant
street
city
name
SELECT street FROM Addresses WHERE
city"Stockholm"
  • List list // get events
  • for (Iterator iter list.iterator()
    iter.hasNext() )
  • Event event (Event) iter.next()
  • Address address event.getVenue().getAddress()
  • if ("Stockholm".equals(address.getCity()))
  • System.out.println("'" address.getStreet()
    "' is in Stockholm")

13
Object Queries
  • Roots may not be enough for search
  • We need at least class extension or
  • OQL

14
HQL
Event
Venue
name
name
date
Address
Get street in Stockholm
Participant
street
city
name
SELECT street FROM Addresses WHERE
city"Stockholm"
  • List list
  • session.createQuery(
  • "select street from Address where
    city'Stockholm'").list()
  • for (Iterator iter list.iterator()
    iter.hasNext() )
  • System.out.println("'" iter.next() "' is in
    Stockholm")

15
Example Hibernate Domain model
  • Take a look at
  • Java
  • SQL

16
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

17
Reasons for Object Persistence vs RDB
  • Reasons to use OODB or ORM
  • Use of OO in design and programming(avoid
    impedance mismatch)
  • Domain Model Intense solution
  • Hierarchic data
  • Navigational access

18
Reasons For ORM vs OODB
  • Legacy RDB (Relational Database)
  • (RDB) vendor independence
  • Vendor stability(?)
  • Schema migration
  • Tools

19
Alternatives to ORM
  • Hand coded persistance layer
  • Serialization
  • EJB/CMP
  • OODB

20
Hibernate
  • Open Source
  • LGPL Licence
  • http//hibernate.org

21
Hibernate is non-intrusive
  • This means
  • Persistence is orthogonal to class
  • Persisting a instance is a run-time decision

22
Requirements for a Persistent Class
  • Hibernate is said to non-intrusive, however
  • Classes must have a no-arg constructor
  • Classes should have a private Long id
  • Classes may have private database attributes

23
Understand ORM to use it
  • The effective use of ORM technology in all but
    the simplest of enterprise environments requires
    understanding and configuring how the mediation
    between relational data and objects is performed
  • Linda DeMichiel, Lead Architect EJB, Sun

24
Impedance Mismatch
  • Identity
  • Granularity
  • Object navigation
  • Subtypes
  • Polymorphic associations

25
Inheritance
B
C
id
id
a1
a1
b1
c1
  • Table per
  • concrete class

A
id
a1
A
b1
id
c1
a1
Table per class hierarchy
B
C
id
id
b1
c1
Table per class
26
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!

27
Detached objects automatic versioning
  • Handle concurrent updates by versioning
  • A version attribute must be added to classes
    involved
  • Detach object by session.close()
  • Updates can be done while detached
  • Attach with session.update(object)
  • Exception thrown by Hibernate at flush (commit)
    if version mismatch

28
Lazy vs Eager fetch
Event
Venue
Address
name
street
name
city
date
29
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())

30
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.

31
Caching
First-level Cache
Session
Query Cache
Cache Concurrency Strategy
Cache Provider
Second-level Cache
32
Testing without database?
  • Testing business logic
  • Hibernate is non-intrusive -gt Use POJOs in a
    transient way
  • Think of transaction demarcation
  • Beware of embedding HQL as it requires a DB.

33
Hibernate Product Suite
  • Hibernate Core
  • Hibernate Annotations
  • Hibernate EntityManager
  • Hibernate Tools
  • NHibernate
  • JBoss Seam

34
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)

35
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

36
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

37
Books on Hibernate
  • Etc.

38
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
Write a Comment
User Comments (0)
About PowerShow.com