one-to-one relationship branch manager 11 each branch has a single manager each manager manages only...
TRANSCRIPT
One-to-One Relationship
Branch Manager1 1
Each branch has a single manager
Each manager manages only one branch
Sue JamesCentral
HeadOffice Tony Dunn
HighStreet Ann Bond
10.1
Implement with a Reference
Branch Manager
myManager
Sue JamesCentral
reference
10.2
Inverse Reference
Branch Manager
myManager
Branch Manager
myBranch
myManager is the inverse reference of myBranch
myBranch is the inverse reference of myManager
10.3
Update Mode
Sue JamesCentral
A reference is set manually in a method
Sue JamesCentral
JADE automatically sets inverse reference
or
or10.4
Referential Integrity
// Suppose mgr1 is Sue James, not currently managing a branch // Suppose mgr2 is Tony Dunn, not currently managing a branch // Suppose br is Central branch, currently without a manager
br.myManager := mgr1; // Manually sets Central's myManager reference to Sue James // Automatically Sue James' myBranch reference set to Central
br.myManager := mgr2; // Manually sets Central's myManager reference to Tony Dunn // Automatically Sue James' myBranch reference set to null // Automatically Tony Dunn's myBranch reference set to Central
delete br; // Manually deletes the Central branch // Automatically Tony Dunn's myBranch reference is set to null
// Suppose mgr1 is Sue James, not currently managing a branch // Suppose mgr2 is Tony Dunn, not currently managing a branch // Suppose br is Central branch, currently without a manager
br.myManager := mgr1; // Manually sets Central's myManager reference to Sue James // Automatically Sue James' myBranch reference set to Central
br.myManager := mgr2; // Manually sets Central's myManager reference to Tony Dunn // Automatically Sue James' myBranch reference set to null // Automatically Tony Dunn's myBranch reference set to Central
delete br; // Manually deletes the Central branch // Automatically Tony Dunn's myBranch reference is set to null
10.5
PersistentTransient
TransientPersistent A persistent object, visible in all nodes, may not
reference a transient object, visible in only one node.
Allow Transient to Persistent Reference
PersistentPersistent
TransientTransient
A transient object may reference a persistent object provided the inverse is not maintained.
An object can reference another object with the same lifetime.
10.6
Relationship Type
PeerPeer
Object is deleted Peer not affected
or
or
ChildParent
Parent is deleted Child is deleted
Child is deleted Parent not affected
10.7
One-to-Many Relationship
Branch Customer1
Each branch has many customers
Each customer belongs to exactly one branch
G.Brown
Central V.Clark
C.Smith
D.Jones
HighStreet
T.Watson
10.8
Implement with a Collection
Branch Customer
allCustomers
exclusive collection
G.Brown
V.Clark
C.Smith
Central
10.9
Inverse Reference
Branch Customer
allCustomers
Branch Customer
myBranch
allCustomers is the inverse reference of myBranch
myBranch is the inverse reference of allCustomers
10.10
Referential Integrity
// Suppose br1 is the Central branch // Suppose br2 is the HighStreet branch // Each branch has a dictionary of customers keyed on ID number // Suppose cust is C.Smith, a customer who is not with any branch
cust.myBranch := br1; // Manually sets the customer's myBranch reference to Central // Automatically adds C.Smith to Central's allCustomers
cust.myBranch := br2; // Manually sets the C.Smith's myBranch reference to HighStreet // Automatically removes C.Smith from Central's allCustomers // Automatically adds C.Smith to HighStreet's allCustomers
cust.myID := '25485725'; // Manually changes C.Smith's key property (ID) // Automatically removes C.Smith from HighStreet's allCustomers // Automatically re-adds C.Smith into allCustomers with a new key
delete cust; // Manually deletes C.Smith // Automatically removes C.Smith from HighStreet's allCustomers
// Suppose br1 is the Central branch // Suppose br2 is the HighStreet branch // Each branch has a dictionary of customers keyed on ID number // Suppose cust is C.Smith, a customer who is not with any branch
cust.myBranch := br1; // Manually sets the customer's myBranch reference to Central // Automatically adds C.Smith to Central's allCustomers
cust.myBranch := br2; // Manually sets the C.Smith's myBranch reference to HighStreet // Automatically removes C.Smith from Central's allCustomers // Automatically adds C.Smith to HighStreet's allCustomers
cust.myID := '25485725'; // Manually changes C.Smith's key property (ID) // Automatically removes C.Smith from HighStreet's allCustomers // Automatically re-adds C.Smith into allCustomers with a new key
delete cust; // Manually deletes C.Smith // Automatically removes C.Smith from HighStreet's allCustomers
10.11
Many-to-Many Relationship
Employee Skill
Each employee has many skills
Each skill is possessed by many employees
F. Mills
DomesticCurrency
J. Wood
ForeignCurrency
MortgageSelling
10.12
Implement with Collections
Employee Skill
allSkills
Employee Skill
allEmployees
allSkills is the inverse reference of allEmployees
allEmployees is the inverse reference of allSkills
10.13
Multiple Inverses
Bank Customer1
allCustomersByAccount myBank
First relationship between Bank and Customer
When a customer’s myBank reference is set manually,the customer is automatically added to both collections
Bank Customer1
allCustomersByName myBank
Second relationship between Bank and Customer
10.14
Conditional Inverses
Branch BankAccount1
allBankAccountsallChequeAccountsallSavingsAccounts
myBranch
SavingsAccountChequeAccount
Three relationships are defined between Branch and BankAccount
Manually setting myBranch for a cheque accountautomatically adds the account to
allBankAccounts and allChequeAccounts
Manually setting myBranch for a savings accountautomatically adds the account to
allBankAccounts and allSavingsAccounts
10.15
Challenge #20
CustomerByLastName 1 Customer
myBank
Bank
allCustomersByLastName
ChequeAccountByNumber 1 BankAccount
myBank
Bank
allChequeAccountsByNumber
SavingsAccountByNumber BankAccount
myBank
Bank
allSavingsAccountsByNumber
1
BankAccountByNumber Customer
allBankAccountsByNumber
1 BankAccount
myCustomer
• Add dictionary classes with duplicate keys not allowed
• Define relationships with collections automatically maintained
10.16
Challenge #21
• Modify ChequeAccount::setPropertiesOnCreate method
• Modify SavingsAccount::setPropertiesOnCreate method
• Modify Customer::setPropertiesOnCreate method
• Code a JadeScript::createCustomerWithBankAccounts method
• Execute the JadeScript and inspect the new objects
10.17
Attributes before References
setPropertiesOnCreate(pAddress : String; pFirstNames : String; pLastName : String) updating;
begin self.myBank := app.myBank; self.address := pAddress; self.firstNames := pFirstNames; self.lastName := pLastName;end;
setPropertiesOnCreate(pAddress : String; pFirstNames : String; pLastName : String) updating;
begin self.myBank := app.myBank; self.address := pAddress; self.firstNames := pFirstNames; self.lastName := pLastName;end;
Why is this method inefficient?
10.18