Object Persistence and Object-Relational Mapping James Brucker.
DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object...
-
Upload
patrick-holt -
Category
Documents
-
view
239 -
download
0
description
Transcript of DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object...
![Page 1: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/1.jpg)
DESIGNING A PERSISTENCE
FRAMEWORK WITH PATTERNS
![Page 2: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/2.jpg)
The Problem: Persistent Objects
persistent object An object that can survive the process or thread
that created it. A persistent object exists until it is explicitly deleted
ProductDescription Storage Mechanisms and Persistent ObjectsObject databases Relational databases others
![Page 3: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/3.jpg)
The Solution: A Persistence Service from a Persistence FrameworkThe framework should provide functions such as:store and retrieve objects in a persistent
storage mechanismcommit and rollback transactions
![Page 4: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/4.jpg)
persistence framework general-purpose, reusable, and extendable set of
types that provides functionality to support persistent objects
framework A set of collaborating abstract and concrete
classes that may be used as a template to solve a related family of problems. It is usually extended via subclassing for application-specific behavior
![Page 5: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/5.jpg)
Key Ideas MappingObject identity Database mapper Materialization and dematerialization CachesTransaction state of object Transaction operations Lazy materialization Virtual proxies
![Page 6: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/6.jpg)
The Representing Objects as Tables
patternHow do you map an object to a record or relational database schema?
The Representing Objects as Tables pattern
![Page 7: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/7.jpg)
Manufacturer
namecity...
...
name city
Now&Zen Mumbai
MANUFACTURER TABLE: Manufacturer
name = Now&Zencity = Mumbai
CelestialShortening San Ramon
![Page 8: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/8.jpg)
UML Data Modeling Profile
«Table»ProductDescription
«PK» OID : char(16)Description : varchar(100) ...«FK» Manu _OID : char(16)
«Table»Manufacturer
«PK» OID : char(16)Name : varchar(100) City : varchar(50)
*1
aggregate signifies a referential constraint: a ProductDescription row can't exist without a related Manufacturer row
PK - primary keyFK - foreign key
![Page 9: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/9.jpg)
![Page 10: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/10.jpg)
对关系的存储设计
![Page 11: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/11.jpg)
![Page 12: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/12.jpg)
![Page 13: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/13.jpg)
![Page 14: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/14.jpg)
![Page 15: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/15.jpg)
![Page 16: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/16.jpg)
对泛化的存储设计
![Page 17: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/17.jpg)
Method 1
![Page 18: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/18.jpg)
Method 2a
![Page 19: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/19.jpg)
![Page 20: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/20.jpg)
![Page 21: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/21.jpg)
Method 2b
![Page 22: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/22.jpg)
![Page 23: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/23.jpg)
object identifier (OID)pattern
OID
xyz123
abc345
This is a simplified design. In reality, the OID may be placed in a Proxy class.
primary key
Manufacturer
citynameoid : OID...
...
name city
Now&Zen Mumbai
MANUFACTURER TABLE: Manufacturer
city = Mumbainame = Now&Zenoid = xyz123
CelestialShortening San Ramon
![Page 24: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/24.jpg)
Accessing a Persistence Service with a Facade
1PersistenceFacade
...
getInstance() : PersistenceFacade
get( OID, Class ) : Objectput( OID, Object )...
: DBProductsAdapter 1: PersistenceFacade
pd = get(...)
// example use of the facade
OID oid = new OID("XYZ123");ProductDescription pd = (ProductDescription) PersistenceFacade.getInstance().get( oid, ProductDescription.class );
![Page 25: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/25.jpg)
Mapping Objects: Database Mapper or
Database Broker PatternWho should be responsible for materialization and dematerialization of objects (for example, a ProductDescription) from a persistent store?
![Page 26: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/26.jpg)
The PersistenceFacade—as true of all facades—does not do the work itself, but delegates requests to subsystem objects.direct mapping
persistent object class itself indirect mapping
Database Broker patternDatabase Mapper pattern
![Page 27: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/27.jpg)
Metadata-Based Mappers
class PersistenceFacade{ / / . . . public Object get( OID oid, Class persistenceClass ) { // an IMapper is keyed by the Class of the persistent object IMapper mapper = (IMapper) mappers.get( persistenceClass ); // delegate return mapper.get( oid ); } //... }usage:
(Manufacturer) PersistenceFacade.getInstance(). get( manuOID, Manufacturer.class) );
![Page 28: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/28.jpg)
each mapper gets and puts objects in its own unique way , depending on the kind of data store and format
1PersistenceFacade
getInstance () : PersistenceFacade
get ( OID , Class ) : Objectput ( OID , Object )...
ProductSpecificationRDBMapper
...
get ( OID ) : Objectput ( OID , Object )...
ProductSpecificationFlatFileMapper
...
get ( OID ) : Objectput ( OID , Object )...
ManufacturerRDBMapper
...
get ( OID ) : Objectput ( OID , Object )...
note that the Class as a parameter is no longer needed in this version of get , as the class is "hardwired " for a particular persistent type
1
«interface»IMapper
get (OID ) : Objectput ( OID , Object )...
Class
UML notation : This is a qualified assocation . It means :
1 . There is a 1 -M association from PersistenceFacade to IMapper objects .2 . With a key of type Class , an IMapper is found (e .g ., via a HashMap lookup )
![Page 29: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/29.jpg)
![Page 30: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/30.jpg)
Template Method Pattern
GUIComponent
update()
repaint()
MyExcellentButton
repaint()
// this is the template method// its algorithm is the unvarying part
public void update(){ clearBackground();
// this is the hook method // it is the varying part repaint();}
hook method
- varying part- overriden in subclass-may be abstract, or have a default implementation
hook method overriden
- fills in the varying part of the algorithm
HOLLYWOOD PRINCIPLE: Don't call us, we'll call you
Note that the MyExcellentButton--repaint method is called from the inherited superclass update method. This is typical in plugging into a framework class.
FRAMEWORK class
OUR class
template method
hook method
![Page 31: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/31.jpg)
Framework Design with the Template Method Pattern if (object in cache) return it else create the object from its representation in
storage save object in cache return it
![Page 32: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/32.jpg)
AbstractPersistenceMapper
+ get ( OID ) : Object {leaf }
# getObjectFromStorage (OID ) : Object {abstract }...
«interface»IMapper
get (OID ) : Objectput ( OID , Object )...
// template methodpublic final Object get ( OID oid ){obj := cachedObjects .get (oid ); if (obj == null ) { // hook method obj = getObjectFromStorage ( oid );
cachedObjects .put ( oid , obj ); }return obj ;} HOOK
TEMPLATE
![Page 33: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/33.jpg)
How to use the Framework
![Page 34: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/34.jpg)
ProductDescriptionRDBMapper
# getObjectFromStorage(OID) : Object
AbstractPersistenceMapper
+ get( OID) : Object {leaf}
# getObjectFromStorage(OID) : Object {abstract}...
// template methodpublic final Object get( OID oid ){obj := cachedObjects.get(oid); if (obj == null ) { // hook method obj = getObjectFromStorage( oid );
cachedObjects.put( oid, obj ) }return obj}
// hook method overrideprotected Object getObjectFromStorage( OID oid ){String key = oid.toString();dbRec = SQL execution result of: "Select * from PROD_DESC where key =" + key
ProductDescription pd = new ProductDescription();pd.setOID( oid );pd.setPrice( dbRec.getColumn("PRICE") );pd.setItemID( dbRec.getColumn("ITEM_ID") );pd.setDescrip( dbRec.getColumn("DESC") );
return pd;}
IMapper
![Page 35: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/35.jpg)
Further factoring out the varying and unvarying parts of the algorithm.
![Page 36: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/36.jpg)
![Page 37: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/37.jpg)
Final Framework
![Page 38: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/38.jpg)
1
«interface»IMapper
get (OID ) : Objectput ( OID , Object )...
Class
1+ PersistenceFacade
getInstance () : PersistenceFacade
get ( OID , Class ) : Objectput ( OID , Object )...
AbstractPersistenceMapper
+ get ( OID ) : Object {leaf }# getObjectFromStorage (OID ) : Object ...
AbstractRDBMapper
+ AbstractRDBMapper (tableName )# getObjectFromStorage (OID ) : Object {leaf }# getObjectFromRecord (OID , DBRecord ) : Object- getDBRecord (OID ) : DBRecord
Persistence
NextGen Persistence
ProductDescriptionRDBMapper
+ ProductDescriptionRDBMapper (tableName )# getObjectFromRecord (OID , DBRecord ) : Object
ProductDescriptionFileWithXMLMapper
# getObjectFromStorage (OID ) : Object
SaleRDBMapper
...# getObjectFromRecord (OID , DBRecord ) : Object
ProductDescriptionInMemoryTestDataMapper
# getObjectFromStorage (OID ) : Object
![Page 39: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/39.jpg)
AbstractPersistenceMapper
+ get( OID) : Object {leaf, guarded}...
// Javapublic final synchronized Object get( OID oid ){ ... }
{guarded} means a "synchronized" method; that is, only 1 thread may execute at a time within the family of guarded methods of this object.
IMapper
![Page 40: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/40.jpg)
Configuring Mappers
class MapperFactory{ public IMapper getProductSpecificationMapper(){...} public IMapper getSaleMapper() {...}}
![Page 41: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/41.jpg)
class MapperFactory{
public Map getAllMappers( ) {...}}
class PersistenceFacade{ private java.util.Map mappers = MapperFactory.getlnstance( ).getAllMappers( ); }
![Page 42: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/42.jpg)
ProductDescriptionRDBMapper
# getObjectFromStorage(OID) : Object
AbstractPersistenceMapper
+ get( OID) : Object {leaf}
# getObjectFromStorage(OID) : Object {abstract}...
// template methodpublic final Object get( OID oid ){obj := cachedObjects.get(oid); if (obj == null ) { // hook method obj = getObjectFromStorage( oid );
cachedObjects.put( oid, obj ) }return obj}
// hook method overrideprotected Object getObjectFromStorage( OID oid ){String key = oid.toString();dbRec = SQL execution result of: "Select * from PROD_DESC where key =" + key
ProductDescription pd = new ProductDescription();pd.setOID( oid );pd.setPrice( dbRec.getColumn("PRICE") );pd.setItemID( dbRec.getColumn("ITEM_ID") );pd.setDescrip( dbRec.getColumn("DESC") );
return pd;}
IMapper
![Page 43: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/43.jpg)
class ProductSpecificationRDBMapper extends …{// hook method overrideprotected Object getObjectFromStorage( OID oid ){
String key = oid.toString();dbRec = SQL execution result of: "Select * from PROD_SPEC where key =" +
key
ProductSpecification ps = new ProductSpecification();
ps.setOID( oid );ps.setPrice( dbRec.getColumn("PRICE") );ps.setItemID( dbRec.getColumn("ITEM_ID") );ps.setDescrip( dbRec.getColumn("DESC") );return ps;
}}
![Page 44: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/44.jpg)
class RDBOperations{ public ResultSet getProductDescriptionData( OID oid ) {...} public ResultSet getSaleData( OID oid ) {...} ...}
class ProductDescriptionRDBMapper extends AbstractPersistenceMapper{
protected Object getObjectFromStorage( OID oid ){ ResultSet rs = RDBOperations.getInstance().getProductDescriptionData( oid );
ProductDescription ps = new ProductDescription(); ps.setPrice( rs.getDouble( "PRICE" ) ); ps.setOID( oid ); return ps;}
![Page 45: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/45.jpg)
![Page 46: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/46.jpg)
Pattern: Cache Management
to maintain materialized objects in a local cache to improve performance (materialization is relatively slow) and support transaction management operations such as a commit. When objects are materialized, they are placed in the cache, with their OID as the key. Subsequent requests to the mapper for an object will cause the mapper to first search the cache, thus avoiding unnecessary materialization
![Page 47: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/47.jpg)
Transactional States and the State Pattern
Persistent objects can be inserted, deleted, or modified. Operating on a persistent object (for example, modifying it) does not cause an immediate database update; rather, an explicit commit operation must be performed.
![Page 48: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/48.jpg)
OldClean OldDirty
OldDelete
commit / delete
delete
New
[ from DB ]
[new (not from DB )]
save
commit / update
delete
rollback / reload
rollback / reloadcommit / insert
State chart : PersistentObject
Legend :New--newly created ; not in DBOld--retrieved from DBClean --unmodifiedDirty --modified
Deleted
![Page 49: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/49.jpg)
PersistenceDomain
ProductDescription
...
PersistentObject
oid : OIDtimeStamp: DateTime
commit()delete()rollback()save()...
![Page 50: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/50.jpg)
![Page 51: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/51.jpg)
GoF State pattern Context/ProblemAn object's behavior is dependent on its state, and its methods contain case logic reflecting conditional state-dependent actions. Is there an alternative to conditional logic?SolutionCreate state classes for each state, implementing a common interface. Delegate state-dependent operations from the context object to its current state object. Ensure the context object always points to a state object reflecting its current state.
![Page 52: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/52.jpg)
![Page 53: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/53.jpg)
PersistentObject
oid : OIDstate : PObjectState
commit()delete()rollback()save()setState(PObjectState)...
PObjectState
commit(obj : PersistentObject)delete(obj : PersistentObject)rollback(obj : PersistentObject)save(obj : PersistentObject)
OldDirtyState
commit(...)delete(...)rollback(...)
1
OldCleanState
delete(...)save(...)
NewState
commit(...)
OldDeleteState
commit(...)rollback(...)
ProductSpecification
...
...
Sale
...
...
*
{ state.delete( this ) }
{ // default no-op // bodies for // each method }
{ // deleteobj.setState( OldDeleteState.getInstance() ) }
{ // saveobj.setState( OldDirtyState.getInstance() ) }
{ // rollbackPersistenceFacade.getInstance().reload( obj )obj.setState( OldCleanState.getInstance() ) }
{ // commitPersistenceFacade.getInstance().update( obj )obj.setState( OldCleanState.getInstance() ) }
{ state.rollback( this ) } { state.commit( this ) }{ state.save( this ) }
{ // commitPersistenceFacade.getInstance().insert( obj )obj.setState( OldCleanState.getInstance() ) }
{ // commitPersistenceFacade.getInstance().delete( obj )obj.setState( DeletedState.getInstance() ) }
![Page 54: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/54.jpg)
Designing a Transaction with the Command Pattern
![Page 55: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/55.jpg)
Ordering the database tasks
Table A: caseNo StudentNo
Health
Table B: StudentNo StudentName
Inseart a record (“05001”,”wang”) to Bupdate A ("001","05001"),
![Page 56: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/56.jpg)
Command
Context/ProblemHow to handle requests or tasks that need functions such as sorting (prioritizing), queueing, delaying, logging, or undoing?SolutionMake each task a class that implements a common interface
![Page 57: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/57.jpg)
actions become objects, and thus can be sorted, logged, queued, and so forth.
![Page 58: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/58.jpg)
«interface»ICommand
execute( )undo()
DBInsertCommand
execute()
DBUpdateCommand
execute()
DBDeleteCommand
execute()
Transaction
commands : List
commit()addDelete(obj: PersistentObject)addInsert( obj: PersistentObject)addUpdate( obj: PersistentObject)sort()...
1..*
DBCommand
object : PersistentObject
execute() { abstract}undo() {leaf}
undo is a no-op for this example, but a more complex solution adds a polymorphic undo to each subclass which uniquely knows how to undo an operation
PersistentObject
commit()...1{
commands.add( new DBUpdateCommand(obj) );}
use SortStrategy objects to allow different sort algorithms to order the
Commands
perhaps simply object.commit()but each Command can perform its own unique actions
{sort()for each ICommand cmd
cmd.execute() }
![Page 59: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/59.jpg)
Lazy Materialization with a Virtual Proxy
![Page 60: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/60.jpg)
ManufacturerProxy
realSubject : IManufacturer
- getRealSubject() : IManufacturer
+ getAddress()...
Manufacturer
address
getAddress()...
«interface»IManufacturer
getAddress()...
Proxy-for 1
realSubject
{return getRealSubject().getAddress()}
ProductSpecification
manufacturer : IManufacturer...
getManufacturerAddress() : Address
1
{if ( realSubject == null ) realSubject = PersistenceFacade.get(oid, Manufacturer.class);return realSubject;}
PersistentObject
oid
...
1
{return manufacturer.getAddress()}
actually references an instance of ManufacturerProxy
1
23
![Page 61: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/61.jpg)
// EAGER MATERIALIZATION OF MANUFACTURERclass ProductSpecificationRDBMapper extends AbstractPersistenceMapper{ protected Object getObjectFromStorage( OID oid ){ ResultSet rs =
RDBOperations.getlnstance().getProductSpecificationData( oid ); ProductSpecification ps = new ProductSpecification(); ps.setPrice( rs.getDouble( "PRICE" ) );
// here's the essence of it String manufacturerForeignKey = rs.getString( "MANU_OID" ); OID manuOID = new OID( manufacturerForeignKey );
ps.setManufacturer((Manufacturer) PersistenceFacade.getInstance(). get(manuOID,
Manufacturer.class) );
// or LAZY MATERIALIZATION OF MANUFACTURERps.setManufacturer( new ManufacturerProxy( manuOID ) );
![Page 62: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/62.jpg)
the Representing Object Relationships as Tables
one-to-one associationsPlace an OID foreign key in one or both
tables representing the objects in relationship.
Or, create an associative table that records the OIDs of each object in relationship.
![Page 63: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/63.jpg)
one-to-many associations, such as a collectionmany-to-many associationsCreate an associative table that records
the OIDs of each object in relationship.
![Page 64: DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS. The Problem: Persistent Objects persistent object An object that can survive the process or thread that.](https://reader033.fdocuments.us/reader033/viewer/2022052302/5a4d1b5f7f8b9ab0599ac757/html5/thumbnails/64.jpg)
Unresolved Issues
• dematerializing objects Briefly, the mappers must define putObjectToStorage.
methods. Dematerializing composition hierarchies requires collaboration
between multiple mappers and the maintenance of associative tables (if an RDB is used).
• materialization and dematerialization of collections• queries for groups of objects• thorough transaction handling• error handling when a database operation fails• multiuser access and locking strategies• security—controlling access to the database