Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Persistent Objects Persistent objects are objects that continue to exist after the application program terminates Persistence is provided by a DBMS • ACID properties … D: durable or persistent Two main categories • OODBMS • RDBMS Chapter 38: Intro, 1- 2, 4 - 9, 16 -19 Fall 2009 ACS-3913 R McFadyen 1 OODBMS An ODBMS is a DBMS which stores objects Depending on how an ODBMS implementation chooses to persist objects, there are two types of ODBMS: non-native and native Fall 2009 ACS-3913 R McFadyen 2 OODBMS Non-native ODBMS In a non-native object database, there are two separate object models, one of the application and the other of the database itself. ODMG-compliant ODBMSs are examples of non-native ODBMSs since they require a separate data schema to be defined, regardless of the existence of the application object model. In order to query or persist objects from and to nonnative object databases, a mapping between these two distinct models is required. Fall 2009 ACS-3913 R McFadyen 3 OODBMS Native ODBMS In native object databases, objects are stored exactly as they are, without the need to map them into a different object model supported by the databases and vice versa. In the world of native object databases, there is just one single object model: the application object model databases are not easily portable across applications, languages, platforms Fall 2009 ACS-3913 R McFadyen 4 OODBMS Native ODBMS e.g. db4o www.db4o.com Consider the model for the Persistence example on the web site section * student Fall 2009 ACS-3913 R McFadyen 5 OODBMS import … Makes the db4o classes available to us. ObjectContainer db = Db4o.openFile(“…”); If the database does not exist, db4o creates it. The database is then opened for use. db.commit(); This statement ends the current transaction. It indicates the end of a unit of work that must be committed in its entirety to the database. db.close(); This statement closes the database for us and makes it available to another application. db.store(section); This one statement is all that is necessary to have the section object saved to the database. Fall 2009 ACS-3913 R McFadyen 6 OODBMS Consider code in folder Persistence example on web site: SectionDriver, Section, and Student comprise a 1903 example using arrays of objects CreateStudentDatabase: creates a database of sections where a section contains student objects ListDatabase: lists section objects and student objects in database ClearDatabase: removes all sections and students from the database Fall 2009 ACS-3913 R McFadyen 7 RDBMS Because RDBMSs are used so heavily in practice, objects must be stored and retrieved from an RDBMS The mismatch between the object representation of data and the relational representation leads to difficulties. A mapping between the two models is required to be implemented. • Relations have Primary Keys, Foreign keys • Data types • Complex objects vs tuples • Inheritance • Collections of objects Larman examines how a framework for an O-R mapping service should be constructed Fall 2009 ACS-3913 R McFadyen 8 Architectural layers User Interface Application Technical Services Fall 2009 NextgenPOS Relational Database ACS-3913 R McFadyen O/R Mapping Service 9 Patterns Representing Objects as Tables 38.6 Object Identifier 38.8 Façade 38.9 Representing Object Associations as Tables 38.19 one-to-one one-to-many many-to-many many-to-many with an Association Class Aggregation and Composition Superclass/Subclass Fall 2009 ACS-3913 R McFadyen 10 Persistent Objects in an RDBMS To use an RDBMS for NextGenPOS, 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 Hibernate, Toplink, Glassfish • provides functions such as store, retrieve, modify, commit, rollback Fall 2009 ACS-3913 R McFadyen 11 Key Ideas Mapping There are two schemas: object and relational. A mapping is required 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 Fall 2009 ACS-3913 R McFadyen 12 Each class becomes a table Representing Objects as Tables pattern define a table for each persistent object class Some mappings are straight forward 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 Fall 2009 ACS-3913 R McFadyen 13 <<Table>> stereotype We can indicate tables in UML using the stereotype:<<Table>> Note you can use UML for designing relational databases Fall 2009 ACS-3913 R McFadyen 14 OIDs Object Identifier Pattern assign an OID to each record and object i.e. We augment the attributes with a new attribute that we refer to as the OID 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 Fall 2009 ACS-3913 R McFadyen 15 Façade Persistence subsystem will have a Façade that provides access to its services façade The façade is implemented using Singleton Fall 2009 ACS-3913 R McFadyen Requesting a ProductSpecification with a specific OID 16 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 Fall 2009 1 1 houses ACS-3913 Register R McFadyen 17 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 Fall 2009 1 1..* contains ProductSpecification ACS-3913 R McFadyen 18 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 Fall 2009 * * employs ACS-3913 Person R McFadyen 19 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 * * employs Person Employment Fall 2009 ACS-3913 R McFadyen 20 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 ... * Person Fall 2009 Marriage * marries ACS-3913 R McFadyen 21 Representing Relationships in Tables Representing Object Associations as Tables pattern Aggregation and Composition 1..* Sale contains * Programme 1..* contains SalesLineItem Course Treat as 1-to-many and many-to-many associations (Aside: Note that relational integrity rules/triggers may need to be defined.) Fall 2009 ACS-3913 R McFadyen 22 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 ACS-3902) Fall 2009 ACS-3913 R McFadyen 23 OR Mapping - Example Suppose we have the following class model: Program name type Course * name * contains description credit Offering * slot offer room description When we map this structure to a Relational Database, we are creating an objectto-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. When specifying a relations include all attributes, the PK, FKs and what the FK references. Fall 2009 ACS-3913 R McFadyen 24 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 Fall 2009 ACS-3913 R McFadyen 25 Example - mapping O-R Mapping, for the given class model we could decide: Class Program Course Maps to Relation Program Course Offering Offering Association Maps to Relation/FKey contains offer Fall 2009 Table: Contains FK in Offering (refers to Course) ACS-3913 R McFadyen 26 Example - mapping Relational Schema Program pOID name type Offering oOID slot room cOID Course cOID name description •Attributes •PKs underlined. credit description Contains cOID pOID Fall 2009 Illustrates the structure of the relations ACS-3913 R McFadyen •FKs indicated via dashed lines. 27 Example - mapping Table structure with some sample records Program pOID name 123 3-year 334 4-year Course cOID 256 333 456 Fall 2009 type R R description A 3-year programme of studies in Business Computing ... name credit description C Programming 3 This course introduces … Java Programming 3 ... Systems Analysis 3 ACS-3913 ... R McFadyen 28