associations n.
Skip this Video
Loading SlideShow in 5 Seconds..
Associations PowerPoint Presentation
Download Presentation

Loading in 2 Seconds...

play fullscreen
1 / 24


185 Vues Download Presentation
Télécharger la présentation


- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Associations Library Container Members Container Database

  2. Objectives In this section you will • Learn about the attributes of associations: • Directionality • Cardinality • Learn how to define an association • Learn how to add or delete associations

  3. What is an Association? • An association is a typed relationship between two objects • To allow an association between objects, you must define an association link in each object’s class definition • Associations are used to link objects together

  4. Loan Book Associations in the Library Object Model patron Patron loans[] activeBook previousBook previousLoans[] activeLoan

  5. Patron Patron Patron Patron Unidirectional Associations • A unidirectional association is one in which only one class defines an association link to another class • For example, there is only one Library to serve many Patrons Library

  6. Loan Loan Loan Loan Bidirectional Associations • Bidirectional associations are those in which each class instance has an association link to the other class • They guarantee referential integrity: prevents dangling references • They support two-way access paths • For example, each patron has many loans and each loan can be tracked back to a patron Patron

  7. Cardinality in Associations • An association has two ends • Each end can connect to one or many objects, depending on how the association is defined in the database schema • Objectivity/DB associations support four categories of cardinality: • 1:1 one-to-one • 1:m one-to-many • m:1 many-to-one • n:m many-to-many

  8. Defining Association Links one-to-one unidirectional class A : public ooObj { ooRef(B) toB; }; class B : public ooObj { }; 1 toB A B one-to-many bidirectional class A : public ooObj { ooRef(B) toB[] <-> toA; }; class B : public ooObj { ooRef(A) toA <-> toB[]; }; m 1 toA toB[] A B many-to-many bidirectional class A : public ooObj { ooRef(B) toB[] <-> toA[]; }; class B : public ooObj { ooRef(A) toA[] <-> toB[]; }; m n toA[] toB[] A B

  9. Using Associations • The DDL Processor generates several methods (member functions) for every association link declared in a class • You use these member functions to access the related objects and establish association links • Set as many associations to a basic object as possible at the same time so that storage is allocated contiguously

  10. DDL Generated Member Functions: the to-one association • The DDL Processor automatically generates the member functions below for one-to-one and many-to-one associations • Association member functions are defined on an object, not on a handle class Book : public ooObj { ooRef(Loan) activeLoan <-> activeBook; }; ooStatus Book::set_activeLoan(ooHandle(Loan)& objH); ooStatus Book::del_activeLoan(); ooStatus Book::activeLoan(ooHandle(Loan)& objH, ooMode mode); ooBoolean Book::exist_activeLoan (const ooHandle(Loan)& objH);

  11. Generated Member Functions: the to-many association • The DDL Processor automatically generates the member functions below for one-to-many and many-to-many associations class Book: public ooObj { ooRef(Loan) previousLoans[] <-> previousBook; }; DDL ooStatus Book::add_previousLoans(ooHandle(Loan)& newObjH); ooStatus Book ::sub_ previousLoans(ooHandle(Loan)& objH); ooStatus Book ::del_previousLoans(); ooStatus Book ::previousLoans(ooItr(Loan)&, ooMode); ooBoolean Book ::exist_previousLoans (const ooHandle(Loan)& objH);

  12. The Unidirectional To-One Association • No methods are automatically generated for a simple ooRef data member. I.e. • This data member is assigned to other ooRef/ooHandle values using the assignment operator • Technically, it is not an “association” unless it has special propagate semantics (explained later) class A : public ooObj { ooRef(B) toB; }; class B : public ooObj { };

  13. Member Functions Examples // For the current book, set an active loan link bookH -> set_activeLoan(loanH); // For the current patron, delete all loans patronH -> del_loans(); // Check if the current book has the active loan bookH -> exist_activeLoan(loanH); // For the current book, add a previous loan link bookH -> add_previousLoans(loanH); // Delete the current loan from the patron patronH -> sub_loans(loanH); // Initialize the handle to the active loan object bookH -> activeLoan(loanH); or loanH = bookH->activeLoan();

  14. Summary: Steps to Use Associations 1. Define Associations in your Object Model (diagrams) 2. Enter the definition in the DDL files 3. Build your application 4. Run your application, which a. Retrieves or creates two objects that are persistent b. Calls the association member functions which connect the link: Class::set_assocName() for a one-ended link Class::add_assocName() for a many-ended link

  15. 4 bytes 8 bytes Assoc. ID OID C1 C2 B toC 1-17- 2- 3 toC 1-17- 3-10 toB 2- 6-14- 5 ... ... System Default Association Array (VArray) Object A1 of Class A How Associations are Stored class A : public ooObj { public: ooRef(B) toB []; ooRef(C) toC []; }; To follow a specific association on an object, all entries in the system default association array are scanned until the desired association is found (i.e., linearly compare each entry in the VArray)

  16. Optimizing Associations • Associations can be optimized for size by using short OIDs • Associations can be optimized for speed and size by using inline associations • Improved traversal performance • Each inline to-many association is placed in a separate array (to-many) instead of the default association array • Inline association links are created by adding a property to your DDL file association definition

  17. Long and Short Inline Association Links • A long inline association link uses an Object Identifier (OID) to refer to the associated object • A short inline association link uses a short OID to refer to the associated object • A short inline association link is used in the same way as a long inline association link, but uses less storage space (only four bytes, rather than eight) to maintain the association, resulting in better runtime performance • When using short association links, objects being linked must be in the same container

  18. Defining & Storing Inline Associations 4 bytes 8 bytes class A : public ooObj { public: ooRef(B) toB <-> toA; ooRef(C) toC []; ooRef(D) toD; inline ooRef(E) toE []; inline ooShortRef(F) toF; inline ooShortRef(G) toG []; ... } System Default Association Array Assoc. link name OID B1 C1 C2 toB 1-17- 3- 1 toC 1-17- 2- 3 toC 1-17- 3-10 ... ... 8 bytes System overhead Pointer to Array for Inline Association Line toE Pointer to Array for Inline Association Link toG Pointer to System Default Association Array Embedded Short Inline Assocaition toF 6-12 ... 8 bytes OID Embedded ooRef toD 5- 7-12-10 3- 5- 7- 9 6-12-10- 2 2-15- 5-14 ... E1 E2 E3 4 bytes Short OID G1 G2 6-15 6-18 ... Object A1 of class A

  19. Accessing Inline Association Links • The member functions for accessing inline association links are generated by the DDL processor and placed in a C++ header file • The member functions are the same as those for accessing non-inline association links • Application programs must be relinked, but not recompiled, when the change is made

  20. Composite Objects & Propagation • Composite objects allow you to treat a group of associated objects as if they are a single object • Composite objects are built using Associations that have propagation semantics • Propagation causes each object in the composite object to be opened, operated on by the specified function, and then closed • Propagation is an atomic operation on a group of objects

  21. Effects of Propagation on Deleting Objects • Delete operations can be propagated • All objects to be deleted are first identified and then deleted as a group • All bidirectional associations are automatically adjusted to reflect deletions B A If you delete object “B”, all objects pointed at by B with delete propagate will be cleaned up as well A2 C

  22. Propagation Syntax • When you define an association link, specify the operation to propagate to other objects using the assocLink behavior specifier: assocLink : propOp(propagate {, propOp(propagate)}) • Example: class Book: public ooObj { public: ooRef(Loan) previousLoans[] <-> previousBook:delete(propagate); } ... ooDelete(bookH); //deletes all related Loans

  23. Associations Summary • An association is a typed relationship between two objects • There are two types of associations: unidirectional and bidirectional • Associations can connect to one or many objects • Associations can be optimized by using long and short inline properties • Delete propagation semantics can be added to association links to affect composite objects

  24. Default Container Book objects Patron objects Loan objects Library Container Members Container Database: Lab5 LOM Associations Lab In this lab you will create associations between a Loan and a Book and a Patron. You will: • Add the new Loan class • Define the association links • Set/remove association links • Build the LMS and populate it with Books, Patrons, Loans • Browse the federated database and traverse the associations