I need some help with a design problem. I'm having trouble wrapping my head around how to design an object (or maybe multiple objects) so that I can query the way I would like to.
Let me start by showing a sql query that returns exactly the information I want:
select distinct rights_table.receiver_guid, user_info.name
from rights_table
inner join user_info
on rights_table.receiver_guid = user_info.guid
where rights_table.giver_guid = '_this_is_the_argument_'
So user_info has one entry for each user. rights_table has info about rights that giver_guid has delegated to receiver_guid. I don't care what the rights are right now - I just want to be able to take a giver_guid and get back a list of all the guids and names of anyone he has given rights to. I'm having trouble conceptualizing how to translate these tables to an object model in such a way that I can perform that query.
If it helps to know, receiver_guid and giver_guid are both (could both be) many-to-one mappings to user_info.guid. user_info.guid is the pk on that table; the pk on rights_table is another column not mentioned here.
Edit: My solution:
As Tim suggested, I put many-to-one mappings on the rights table "object" that linked the rights table to the User table. It took me a while to understand that Hibernate does joins implicity for you when you do mappings like this, so here is what I basically ended up with for HQL:
String hqlQuery =
"select right.recipient_guid, user.name " +
"from rightsTable right, userInfo user " +
"where right.recipient_guid = user.guid and right.giver_guid like :giver_guid";
Honestly, I don't even know if there are joins happening behind the scene there, but I know that this gives me the information I originally wanted. If anyone has suggestions for how to do this query more efficiently please let me know.