Meaningless Drivel is fun!*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Many to Many implementaiton Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Many to Many implementaiton" Watch "Many to Many implementaiton" New topic
Author

Many to Many implementaiton

Ayman Haboubi
Greenhorn

Joined: Aug 30, 2004
Posts: 3
Hi,
I'm having trouble finding an exmaple of a many to many relationship implemented in java and using collections.

Is there a webiste or a link to an example that can help me with this.

Regards.
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2921
    
    5
A Customer object collects the references to all the Vendor objects that it uses in an internal Java collection.
The Vendor object collects the references to all the Customer objects that have purchased goods in its own internal collection.

Voil� - a many-to-many relationship.

Associative entities are used in data-models because you usually don't store collections inside a single record.

However once the Associative entities exist they often acquire additional properties (like the date of the first purchase, how many times the customer has purchased for the vendor, etc).
Now it may become necessary to create an intermediary object that carries these properties. In that case the Customer may contain an internal collection of references to CustomerVendorLink objects - the CustomerVendorLink object would contain the reference to the vendor object. The CustomerVendorLink object could also be used by the Vendor object, provided that the CustomerVendorLink object also has a reference back to the customer.

However the Vendor object may have an entirely different way of dealing with a customer so it may actually use its own VendorCustomerLink object which then contains a reference to the Customer.

So ultimately there is an (implied) many-to-many relationship between the Customer and the Vendor objects even though even though this relationship is facilitated over the CustomerVendorLink and VendorCustomerLink objects.

CustomerVendorLink and VendorCustomerLink are really bad names for the classes mentioned here - they should really reflect what the classes are all about. So in a ratings systems the Customer could refer to a number of its own VendorRatingInfo objects (each of which refers to a single Vendor object � i.e. a single customer is rating a single vendor) and the Vendor would refer to a number of its own CustomerRatingInfo objects (each of which refers to a single Customer object � i.e. a single vendor is rating a single customer).
[ October 26, 2006: Message edited by: Peer Reynders ]
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
A Customer object collects the references to all the Vendor objects that it uses in an internal Java collection.
The Vendor object collects the references to all the Customer objects that have purchased goods in its own internal collection.


Bear in mind, though, that a simple implementation of this can fall into a mutual dependency trap and cause strange compilation problems.

I usually recommend programming to interfaces in this case, but even that needs care. If you follow the "tell, don't ask" principle, you should be alright though. Here's an example of what I mean:



Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2921
    
    5
Originally posted by Frank Carver:
Bear in mind, though, that a simple implementation of this can fall into a mutual dependency trap and cause strange compilation problems.


Good Point! Got to watch out for those nefarious cyclical dependencies.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Peer Reynders:
A Customer object collects the references to all the Vendor objects that it uses in an internal Java collection.
The Vendor object collects the references to all the Customer objects that have purchased goods in its own internal collection.


That's one way to implement a *bidirectional* many-to-many relationship.

If you remove one of those collections, you can still have a many-to-many relationship. Whether it's many-to-many or one-to-many simply depends on what you put into those collections (whether you allow one entity to be put into several collections at the same time).


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ayman Haboubi
Greenhorn

Joined: Aug 30, 2004
Posts: 3
Wow Frank!!! that example is just what I wanted. I will study it well and try to understand it better.

Many a Thank you Peer for the extensive details and Ilja for your insight
[ October 28, 2006: Message edited by: Ayman Haboubi ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Many to Many implementaiton
 
Similar Threads
How many interfaces can a single interface extend?
How many months to study?
thank you
CPAN for Java?
Agrah Upadhyay turned 21 on 18th April......