70 likes | 218 Views
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.
E N D
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 92.3913 R McFadyen
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 92.3913 R McFadyen
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” 92.3913 R McFadyen
Patterns of enterprise application architectureUnit 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) 92.3913 R McFadyen
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 • … 92.3913 R McFadyen
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 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. • UnitOfWork uow = session.acquireUnitOfWork(); • Pet pet new Pet(); • Pet petClone = (Pet)uow.registerObject(pet); • petClone.setId(100); • petClone.setName(“Fluffy”): • 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) 92.3913 R McFadyen
Based on Primer pages 7, 8 :Client :session acquireUnitOfWork() new() :UnitOfWork pet:Pet new() 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) 92.3913 R McFadyen