Transcript example

Object Relational Mapping example
TopLink
Part of Oracle environment
Provides an Object/Relational Mapping Layer
References: pages from:
•Oracle Toplink Unit of Work Primer
•Patterns of enterprise application architecture
2003
Martin Fowler
Addison-Wesley
0321146530
March 2005
92.3913
R McFadyen
1
Patterns of enterprise application architecture
A book on enterprise application design
•Layering
•Domain/business logic
•Web user interface
•Object to relational mapping
•Session state in a stateless environment
•Distribution
March 2005
92.3913
R McFadyen
2
Patterns of enterprise application architecture - Architecture
Two common elements to architecture:
•Highest-level breakdown of a system into its parts
•Decisions that are hard to change
page 2:
“The architectural pattern I like the most is that of layers”
“Most nontrivial enterprise applications use a layered architecture
of some form”
March 2005
92.3913
R McFadyen
3
Patterns of enterprise application architecture
Unit of Work Pattern
Maintains a list of objects affected by a business transaction
and coordinates the writing out of changes and the resolution
of concurrency problems
Mentions 3 variations: caller registration, object registration,
unit or work controller (e.g. Toplink)
March 2005
92.3913
R McFadyen
4
Unit of Work Pattern
unit of work controller variation:
•UoW makes a copy (clone)
•Application works on the copy
•UoW compares object and clone at commit time
and issues necessary SQL to database
•…
March 2005
92.3913
R McFadyen
5
Based on Primer pages 7, 8
Code shows how a Java program collaborates with Toplink to
ensure a new object persists in the relational database
UnitOfWork uow = session.acquireUnitOfWork();
Pet pet new Pet();
Pet petClone = (Pet)uow.registerObject(pet);
petClone.setId(100);
petClone.setName(“Fluffy”):
The application gets a Unit
of Work.
Application creates a new
Pet object, but the
application works on a
clone.
At commit time, Toplink
issues the necessary SQL
based on difference
between clones and
originals.
petClone.setType(“Cat”);
uow.commit();
At commit time Toplink sends the following to the database
Insert into Pet (ID, Name, Type, Pet_Own_Id) Values (100, ‘Fluffy’ ,’Cat’ , Null)
March 2005
92.3913
R McFadyen
6
Based on Primer pages 7, 8
:Client
:session
acquireUnitOfWork()
new()
new()
:UnitOfWork
pet:Pet
registerObject(pet)
new()
petClone:Pet
petClone
setID(100)
setName(Fluffy)
setType(Cat)
commit()
At commit time, the UnitOfWork submits the SQL statement:
Insert into Pet (ID, Name, Type, Pet_Own_Id) Values (100, ‘Fluffy’ ,’Cat’ , Null)
March 2005
92.3913
R McFadyen
7