240 likes | 591 Views
Persistent Objects. Persistent objects are objects that continue to exist after the application program terminates Persistence is provided by a DBMS ACID property “D” Two main categories OODBMS RDBMS. Sections Intro, 34.1, 2, 4, 5, 6, 7, 8, 9, 19. Persistent Objects.
E N D
Persistent Objects • Persistent objects are objects that continue to exist after the application program terminates • Persistence is provided by a DBMS • ACID property “D” • Two main categories • OODBMS • RDBMS Sections Intro, 34.1, 2, 4, 5, 6, 7, 8, 9, 19 91.3913 R McFadyen
Persistent Objects • To use an RDBMS, we require an object to relational mapping service • persistence service is a subsystem within the Technical Services Layer • mismatch between record-oriented and object-oriented representations of data • recommendation : buy, not build • provides functions such as store, retrieve, modify, commit, rollback 91.3913 R McFadyen
Key Ideas Mapping There are two schemas: object and relational. We need a mapping that translates from one to the other. Object Identity Records and objects have a unique identifier Materialization/Dematerialization need to be able to translate the non-object representation to an object representation, and vice versa Lazy Materialization Some objects are not materialized all-at-once; part of the object may be done first, and other parts later on 91.3913 R McFadyen
Patterns • Representing Objects as Tables • Object Identifier • Representing Object Associations as Tables • one-to-one • one-to-many • many-to-many • many-to-many with an Association Class • Aggregation and Composition • Superclass/Subclass 91.3913 R McFadyen
Each class becomes a table Representing Objects as Tables patterndefine a table for each persistent object classSome mappings are straight forward We require 1NF tables Class An object The table Many of these patterns mentioned come from a paper “Crossing Chasms: A Pattern Language for Object-RDBMS” published in 1996 in Pattern Languages of Program Design 91.3913 R McFadyen
<<Table>> stereotype We can indicate tables in UML using the stereotype:<<Table>> Note you can use UML for designing relational databases 91.3913 R McFadyen
OIDs Object Identifier Patternassign an OID to each record and object We can use the OID to prevent from instantiating the same object twice and to help find an instance Objects have an OID that is used as the PK in a relational table. Each object is uniquely mapped to a row in the table 91.3913 R McFadyen
Façade Persistence subsystem will have a Façade that provides access to its services façade The façade is implemented using Singleton Requesting a ProductSpecification with a specific OID 91.3913 R McFadyen
Representing Relationships in Tables Representing Object Associations as Tables pattern one-to-one place an OID FK in one or both tables representing the objects in the association or, create an associative table that records the OIDs of each object in the association or, could merge the two classes into one table Consider Store 1 1 Register houses 91.3913 R McFadyen
Representing Relationships in Tables Representing Object Associations as Tables pattern one-to-many create an associative table that records the OIDs of each object in relationship or, could use a Foreign Key on the Many side Consider ProductCatalog 1 1..* ProductSpecification contains 91.3913 R McFadyen
Representing Relationships in Tables Representing Object Associations as Tables pattern many-to-many create an associative table that records the OIDs of each object in relationship Company * * Person employs 91.3913 R McFadyen
Representing Relationships in Tables Representing Object Associations as Tables pattern many-to-many AND with an Association Class create an associative table that records the OIDs of each object in relationship and attributes of the association class Company * * Person employs Employment 91.3913 R McFadyen
Representing Relationships in Tables Representing Object Associations as Tables pattern many-to-many AND with an Association Class create an associative table that records the OIDs of each object in relationship ... * Marriage Person * marries 91.3913 R McFadyen
Representing Relationships in Tables Representing Object Associations as Tables pattern Aggregation and Composition Sale 1..* SalesLineItem contains * Programme 1..* Course contains Treat as 1-to-many and many-to-many associations (Aside: Note that relational integrity rules/triggers may need to be defined.) 91.3913 R McFadyen
Representing Relationships in Tables Representing Object Associations as Tables pattern Superclass/Subclass Payment CashPayment CreditPayment ChequePayment • 3 basic choices: • all classes represented as separate tables, • only the ‘leaf’ classes, or • one table for all • Others are possible (see 91.3902/3) 91.3913 R McFadyen
OR Mapping - Example • Suppose we have the following class model: Program Course Offering * name type description name credit description slot room * * contains offer When we map this structure to a Relational Database, we are creating an object-to-relational mapping. We need to define the relational database schema and specify which class(es) map to which relation(s), and whether the associations are mapped to relations or to foreign keys. 91.3913 R McFadyen
Example - recall patterns for O-R mappings O-R Mapping • The Representing Objects as Tables pattern says that each class will be represented in the RDb by a separate table (relation). • The Object Identifier pattern tells us that each table representing a class will have the OID as the PK. • The Representing Object Associations as Tables pattern gives us several options: • use an association table to represent the association • (for 1-1 and 1-m) we can choose to use a Foreign Key • for a 1-1 we can choose to merge the two classes into one table • etc 91.3913 R McFadyen
Example - mapping Association Maps to Relation/FKey contains Table: Contains offer FK in Offering (refers to Course) O-R Mapping, for the given class model we could decide: Class Maps to Relation Program Program Course Course Offering Offering 91.3913 R McFadyen
Example - mapping Relational Schema Program pOID name type description • Illustrates the structure of the relations • PKs are underlined. • FKs are indicated via dashed lines. Offering oOID slot room cOID Course cOID name credit description Contains cOID pOID 91.3913 R McFadyen
Example - mapping Table structure with some sample records Program pOID name type description 123 3-year R A 3-year programme of studies in Business Computing 334 4-year R ... Course cOID name credit description 256 C Programming 3 This course introduces … 333 Java Programming 3 ... 456 Systems Analysis 3 ... 91.3913 R McFadyen
Example - application models A collection of course offerings Adding a course offering to the collection Note that these relational structures are very different from the class structures that application programs would use. For example, a course object will contain, within it, its offerings: public class Course { private Hashtable Offerings = new Hashtable(); … course_offering = new Offering (…) Offerings.put (…, course_offering); } Part of the complexity of a system that uses objects and tuples/rows in an RDb is the translation from/to the other. 91.3913 R McFadyen
Object to Relational Mapping Products • EJB-related products providing object to relational mapping services • CoCoBase • TopLink • enJin 91.3913 R McFadyen
Object to Relational Mapping Products enJin: the following is from: http://www.versant.com/products/enjin/whitepapers/WP_Benchmark-011022r4-W.pdf “… This object-to-relational bottleneck is often referred to as an 'impedance mismatch', since in order to store data in an RDBMS, objects must be broken down into flat table structures, and to retrieve objects, they must be built up from these RDBMS 'flat' structures. This white paper examines the performance characteristics of a product that has been designed specifically to overcome this object-to-relational impedance mismatch, and which successfully improves both the response times and transaction throughput of applications deployed on J2EE application servers such as IBM's WebSphere. 91.3913 R McFadyen
Object to Relational Mapping Products TopLink a commercial product (Oracle) that provides object to relational mapping capability “In an enterprise Java environment, one of the most formidable challenges is storing business objects and components in a relational database (RDB). Oracle TopLink provides developers with the flexibility to map objects and Enterprise Java Beans to a relational database schema with minimal impact on ideal application design or database integrity. The result — developers focused on addressing business needs rather than building infrastructure. 91.3913 R McFadyen