Identity Map

Download Report

Transcript Identity Map

Hibernate Persistence
What is Persistence

Persist data to database or other storage.


In OO world, persistence means persist object to
external storage.
Relational database



SQL
Using SQL in Java
Persistence object-oriented applications: ORM
What is Hibernate?



Popular Open Source (LGPL)
Object/Relational Mapping (ORM) tool
Transparent persistence for POJOs
(Plain Old Java Objects)
Core of JBoss CMP 2.0 impl.
Why Hibernate?
• Minimizes Code
• Does not require a container
• Model is not tied to persistence
implementation, Retains natural object model
(transparent)
Object/Relational Mapping
JavaObject
int id;
String name;
String getName()
int getId()
void setName(String)
void setId(int)
SQL Table
id [int] primary key,
name [varchar(50)]
Magic Happens Here
(O/R Mapper – i.e. Hibernate)
Course Object Model
Persistence Tasks

Class mapping



Attribute Mapping




Class to Table mapping
Object ID to Primary Key
Primary Attribute to Column
User defined data type to embedded type
Inheritance Mapping
Relationship Mapping


One to Many
Many to One
Persistence /Concurrency
Pattern Introduced
Martin Fowler《企业应用架构模式》
Persistence



What is Persistence
Why Persistence is Importance
History of persistence



Plan text
RDB(OODB?)
XML
Mapping to a Relational Database




Metadata Mapping
Identity Field
Lazy Load
Query Object
Metadata Mapping


Hold details of object-relational mapping in
metadata.
Hibernate as sample
Identity Field

Save a database id field in an object to
maintain identity between an in-memory
object and a database row.
Identity Field

Choosing your Key






meaningful key
meaningless key
simple key
compound key
table-unique key
database-unique key
How to get a new Key

Database counter


Key table


it's non-standard and not available in all databases.
Separate transaction is needed
GUID

Large keys may also lead to performance problems,
particularly with indexes
Identity Map


Ensure each object only gets loaded once by
keeping every loaded object in a map. Lookup
objects using the map when referring to them
A Identity Map keeps a record of all the objects that
have been read from the database in a single
business transaction. Whenever you want an object,
you check the Identity Map first to see if you already
have it.
Lazy Load

An object that doesn't contain all of the data
you need, but knows how to get it.
Query Object


An object that represents a database query
SQL can be an involved language, and many
developers are not particularly familiar with it..
Query Object( cont. )
Concurrency


When Concurrency problem raised?
Why Concurrency is difficult to deal with


It is difficult to enumerate the possible scenarios
that can get you into trouble
it is hard to test for
Execution Contexts


Connection
Session

A session is a long running interaction between a
client and server

Transaction

Process
Thread

Transactions


The primary tool for handling concurrency in
enterprise applications is the transaction.
ATM machine example
Transaction Resources

Most enterprise applications run into transactions in
terms of databases. But there are plenty of other
things than can be controlled using transactions,
such as



message queues,
printers,
As a result technical discussions of transactions use
the term 'transactional resource' to mean anything
that is transactional: that is uses transactions to
control concurrency.
Transaction types

long transaction.



request transaction


making a transaction span multiple requests is
generally known as a long transaction .
Not recommended
start a transaction at the beginning of a request
and complete it at the end.
late transaction

open a transaction as late as possible ,
Business and System Transactions

System transactions


transactions supported by RDBMS systems and
transaction monitors
Business transaction

Transaction that logically defined by a business
requirement
Concurrency Patterns



Unit of work
Optimistic Offline Lock
Pessimistic Offline Lock
Unit of Work

Maintains a list of objects that are affected by
a business transaction and coordinates the
writing out of changes and resolution of
concurrency problems.
How it Works

Unit of Work is an object that keeps track of
these changed things, such as inserted,
updated or deleted.
Unit of Work
Optimistic Offline Lock

Prevent conflicts between concurrent
business transactions, by detecting a conflict
and rolling back the transaction.
Optimistic Offline Lock
How it Works
Pessimistic Offline Lock

Prevent conflicts between concurrent
business transactions by allowing only one
business transaction to access data at once
Pessimistic Offline Lock
How it Works

have an exclusive write lock. That is, require only that a
business transaction acquire a lock in order to edit session data.
This avoids conflict by not allowing two business transactions to
simultaneously make changes to the same record.