Page 1 of 1

Many - to - Many Relationships...

Posted: Fri Aug 07, 2009 11:19 am
by ConvertFromOldNGs
by JADE Kid - Ray Hidayat >> Tue, 14 Mar 2000 3:16:47 GMT

Ok, I am having problems working out, how to use many - to - many relationships.

so the diagram looks like this:

Key:
-< One to many relationship
>< Many to many relationship
-- One to one relationship

A Class >< And another class
Account >< Customer

I try to avoid using many to many relationships by doing the thing I do in Microsoft Access. (Access Tables are now called classes)

The technique was (This is an example of a bank):

A Class >- An Intermediate Class -< And another class

Account >- Registration -< Customer


So the intermediate class would have a reference that points to all the accounts that customer has...
and all the customers an account has.

So that would make one to many relationships which I could handle.

But in JADE you get the option of a many - to - many relationship, and somehow, it works.

Does JADE use the same technique that I use to do this?
How do I use this feature?


Ray Hidayat
JADE Kid - 2000
www.jadekids.com
(Thanks to David Mitchell [JADE Kid - 1998] for the 'footer')

Re: Many - to - Many Relationships...

Posted: Fri Aug 07, 2009 11:19 am
by ConvertFromOldNGs
by Dean Cooper >> Tue, 14 Mar 2000 5:24:08 GMT
How do I use this feature?

If I understand your post correctly, an Account can have many customers and a Customer can have many accounts. You can implement this in JADE using an intermediate class (Registration, in your example, which decomposes the many-many into two one-many relationships), or as a many-many relationship with inverses between two collection properties. To define the many-many:
Say you have the following two MemberKeyDictionary subclasses:

AccountDict:
membership: Account
keys: accountNumber (or whatever keys you need)

CustomerDict
membership: Customer
keys: surname, firstName (or whatever keys you need)

On Account, define an exclusive reference, allCustomers, of type CustomerDict and click OK. Now on Customer, define an exclusive reference, allAccounts, of type AccountDict and click the Define Inverse button. Click the "many" radio button in the Customer (left-hand) side of the dialog to define the relationship as many-many, and you'll now be able to select the allCustomers property (that you defined earlier) in the Account (right-hand) side of the dialog. Specify whatever update mode and relationship type you need and click OK.

The many-many relationship means that if you add an Account to the allAccounts dictionary of a Customer, the Customer will automatically be added to the allCustomers dictionary of the Account, and vice versa. Likewise if you remove an Account from allAccounts of a Customer, the Customer will automatically be removed from allCustomers of the Account, and vice versa. Note that it's not necessary to use dictionaries (although it would seem to make sense for your example). In cases where one (or both) ends of a relationship don't require keyed access, a set is a more appropriate and more efficient choice.
Does JADE use the same technique that I use to do this?

No. JADE doesn't need an intermediate class to implement many-many relationships. The two collections (one at each end of the relationship) do the job of the intermediate class. Decomposing a many-many with an intermediate class is necessary only when the relationship itself has state or behaviour (ie: if you want/need to add properties and/or methods to Registration, in your example).

Dean.

Re: Many - to - Many Relationships...

Posted: Fri Aug 07, 2009 11:19 am
by ConvertFromOldNGs
by Craig Shearer >> Tue, 14 Mar 2000 8:01:47 GMT

although one could argue that the Collection class is in fact performing the job of an intermediate table in a relational database, just more conveniently.

Craig.

Re: Many - to - Many Relationships...

Posted: Fri Aug 07, 2009 11:19 am
by ConvertFromOldNGs
by Wilfred Verkley >> Wed, 15 Mar 2000 22:11:17 GMT
No. JADE doesn't need an intermediate class to implement many-many relationships. The two collections (one at each end of the relationship) do the job of the intermediate class. Decomposing a many-many with an intermediate class is necessary only when the relationship itself has state or behaviour (ie: if you want/need to add properties and/or methods to Registration, in your example).

Its often a good idea to create an intermediate class anyway however. I often find as the application grows, all of a sudden you find the need to store information on the many-to-many relationship.

I think we often take for granted what a powerful database Jade really is in terms of modelling and maintaining data (and behaviour). All credit to the plant for this.

Re: Many - to - Many Relationships...

Posted: Fri Aug 07, 2009 11:19 am
by ConvertFromOldNGs
by Anonymous >> Mon, 19 Jul 2004 16:00:29 GMT

I have defined a many-to-many relationship between Pubs and Beer as per Dean's post.

My database has an allBeers reference (of type BeerDictionary) on the Pub object and an allPubs reference (of type PubDictionary) on the Beers object. My dictionary keys are integer attributes called pubID and beerID.

I can see from the relationship window that an inverse (peer-to-peer) many-to-many relationship now exists between Beer and Pub.

How can I use a Jadescript method to allocate a beer to a pub?


Chris

Re: Many - to - Many Relationships...

Posted: Fri Aug 07, 2009 11:19 am
by ConvertFromOldNGs
by dcooper@jade.co.nz >> Mon, 19 Jul 2004 21:02:34 GMT

Try something like this:

vars
p : Pub;
b : Beer;
begin

beginTransaction;

// Create the pub
create p persistent;
// Set up other pub stuff here

// Create the beer
create b persistent;
// Set up other beer stuff here

// Add the beer to the pub's collection.
// Assumes this end of the relationship
// is manual or man/auto.
// Inverse maintenance will update the
// b.allPubs collection.
p.allBeers.add(b);

commitTransaction;
end;

Cheers,
Dean.