I have a very simple problem for which though I have a solution but I want to know how others tackle it effectively.
Assume we have a User table, and a Role table. Each user can have multiple roles and vice-versa (many-to-many relationship). This relationship is stored in a cross table: UserRoleMapper (UserID, RoleID).
Now I have to get a list of all users, with their role names too. But if I have a user belonging to 3 different roles, I will get that user 3 times in a list. What is the best way to handle such scenarios? One simple way is to get all users first, and then fire another query (another hit to the DB) to get their respective roles. Is there any other solution better than this?
thanks,
It is right that if a user has 3 roles that 3 results should be returned for the user and their roles with something like the following query
I’m assuming that your question refers to presenting this information? How you handle this in the UI is up to you. You may decide to use a query that concatenates roles for a user, or lazy load roles for a particular user only at the point when they are needed.