Download presentation
Presentation is loading. Please wait.
Published bySteven Gilbert Modified over 9 years ago
1
© Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 1 Associations Library Container Members Container Database
2
© Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 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
© Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 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
© Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 4 Associations in the Library Object Model Book Loan Patron patron loans[] previousBook activeBook activeLoanpreviousLoans[]
5
© Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 5 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 Patron
6
© Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 6 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 Loan
7
© Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 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:1one-to-one 1:mone-to-many m:1many-to-one n:mmany-to-many
8
© Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 8 Defining Association Links AB 1 toB one-to-one unidirectional AB 1 m toB[] toA one-to-many bidirectional AB n m toB[] toA[] many-to-many bidirectional class A : public ooObj { ooRef(B) toB; }; class B : public ooObj { }; class A : public ooObj { ooRef(B) toB[] toA; }; class B : public ooObj { ooRef(A) toA toB[]; }; class A : public ooObj { ooRef(B) toB[] toA[]; }; class B : public ooObj { ooRef(A) toA[] toB[]; };
9
© Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 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
© Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 10 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); DDL
11
© Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 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; }; 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); DDL
12
© Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 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
© Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 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
© Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 14 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 Summary: Steps to Use Associations
15
© Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 15 How Associations are Stored class A : public ooObj { public: ooRef(B) toB []; ooRef(C) toC []; }; Object A 1 of Class A toC 1-17- 2- 3 toC 1-17- 3-10 toB 2- 6-14- 5... Assoc. ID OID System Default Association Array (VArray) 4 bytes8 bytes C1 C2 B 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
© Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 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
© Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 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
© Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 18 Defining & Storing Inline Associations toB 1-17- 3- 1 toC 1-17- 2- 3 toC 1-17- 3-10... Assoc. link name OID 4 bytes8 bytes B1 C1 C2 System Default Association Array 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 [];... } 3- 5- 7- 9 6-12-10- 2 2-15- 5-14... E1 E2 E3 8 bytes OID 6-15 6-18... 4 bytes Short OID G1 G2 Pointer to System Default Association Array Embedded Short Inline Assocaition toF 6-12... System overhead Pointer to Array for Inline Association Line toE Pointer to Array for Inline Association Link toG Embedded ooRef toD 5- 7-12-10 8 bytes Object A 1 of class A
19
© Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 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
© Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 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
© Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 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 A2A2 C If you delete object “B”, all objects pointed at by B with delete propagate will be cleaned up as well
22
© Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 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
© Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 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
© Copyright 1999, Objectivity, Inc., All Rights Reserved Objectivity/DB for C++ Developers - 24 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 Database: Lab5 Default Container Book objects Library Container Members Container Patron objects LOM Loan objects
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.