• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Modeling a many-to-many relationship in java

 
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anyone have an example of how to model a many-to-many relationship in
java?
Thanks!
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To model it in UML just specify cardinality * on both ends. To implement it in Java, use arrays or collections on both ends.
If there are values on the relationship, use relationship objects in between. For example, between Person and LifeInsurancePolicy we might put a little relationship object with a value of "owner" or "insured" or "beneficiary". Then Person has a collection of relationships, LifeInsurancePolicy has a collection of relationships, and Relationship has one Person and one Policy.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stan James:
To implement it in Java, use arrays or collections on both ends.


Or just on one end - depending on how you need to navigate between the objects.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An example could be a Company has_a many Employee s. And an Employee could be employed by Multiple Companies (he/she could be a consultant, part-timer, etc.). To model that link the two classes with a flat solid line and say * at each end. You could also mention the role of the end by saying Employer at the Company end and Employee at the Employee end. You could also say Employed by at the middle of this line with a small arrow pointing to the Company. You can mount the line at its two ends with arrows to indicate that the sending end has a reference of the receiving end. For example if you mention an arrow at the Company end that means in your Employee class you will have some method like
Company[] getCompanies()
If you don't mention any arrows that means the intention of this link is not yet decided, Employee calss may and may not include a reference to Company.
Hope this helps..
 
Barry Brashear
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. So each side of the relationship would contain a collection of the
other side of the relationship? Would anyone have a code example of this?
I need a visual.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd would like to see code example too...

The SQL side of my brain would include a 'look up' table to even out the many to many relationship.
for example

Comany 1 -- * Cmp/Emp * -- 1 Employee
(if I got my directions correct so the middle table would contain the matched up keys of the other two...)

So in the OO world I assume, as mentioned above, I would have a collection that stores the references to the other object.

Which leads to my next two questions...
1) best way then to search for a specific comp - employee pair.
2) how would this work with in a Object/Relational model (e.g. JDO or Hibernate?)

Thanks for the help!
Paul C.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could simply have



Now nothing holds you from doing



That is, you already have a many-to-many relationship.

Of course there are a myriad of alternatives, and which one is the best depends on how you want to use the classes.
 
author
Posts: 608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://www-128.ibm.com/developerworks/webservices/library/ws-tip-objrel4/index.html
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic