• 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:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

Proxy calls/law of demeter/ rails has_many_through

 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've been running into some conceptual difficulties when working with complex relationships -- either long chains that effectively create a cycle/two access paths, or many-to-many (or doubled OneToMany's with a join entity).

Using the example from JPwH, we'd have the many-to-many relationship between Category and Items via one-to-many's to CategorizedItems. However both Category and Items would have rather have an exposed "getCategories" and "getItems" instead of "getCategorizedItems". Now of course both of these could be implemented in the respective class with looping over the return from the getCategorizedItems, and pulling out the entity of interest and building up a new list. Is this what most people end up doing? (Ie its exactly what Rails's :has_many_through was designed for)
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No not at all. What you want would be using the @ManyToMany annotations. Because it can take a join table, but the getters and setters are just getItems() or getCategories() which both return a Collection.

Here is one example
http://tadtech.blogspot.com/2007/09/hibernate-annotation-many-to-many-join.html

Hope that helps.

Mark
 
Eric Nielsen
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But I do need to hang other information on the association, hence the need for an explicit join entity. However the bulk of the time I don't care about those details and only want to deal with the M:M rather than the paired 1:Ms...
 
And when my army is complete, I will rule the world! But, for now, I'm going to be happy with this tiny ad:
The new gardening playing cards kickstarter is now live!
https://www.kickstarter.com/projects/paulwheaton/garden-cards
reply
    Bookmark Topic Watch Topic
  • New Topic