Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Proxy calls/law of demeter/ rails has_many_through

 
Eric Nielsen
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • 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)
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic