Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Native SQL for fetching relationship fields

 
Bojan Tomic
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to customize the SQL used for fetching relationship fields?

To exemplify:
User U1 has friends U2 and U3,. This automatically means users U2 and U3 are friends with U1, as this is a reflexive relationship. The join table looks like this: (userId1, userId2, date). The catch here is that I don't want two different rows for every relation in the join table, but still be able to have a list of friends in the user entity. To make this happen, I need the order in which the IDs appear in the join table irrelevant.
That why I'm asking if I can modify the SQL used for fetching the relationship fields - I could make it look for the current user's id in both the first and the second column in the join table when populating the list of his friends.

I hope I was clear enough... Any thoughts on this? I could really use an advice...

Thanks a lot!
 
John Bengler
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bojan,

how about just selecting both variants and merge them using UNION?

e.g.



Of course you would have to add some criteria..


John
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!