I have 3 entities. User,Project and Role.
A user can be assigned to multiple projects with some roles.
A user GEORGE can have ADMIN role on PROJECT-A and MANAGER role on PROJECT-B. How can I map these entities using bi-directional association.
*The relation can be described in following way.
A USER can have many projects through a role.
A role can be assigned to 0 or more projects for a specific User.*
Is it possible with a JOIN-TABLE.
You’ll need to introduce a fourth entity (let’s call it
Right), which tells that a specific user has a specific role on a specific project. You will thus end with the following associations :The RIght entity will be mapped to a table having three foreign keys (to user, role and project), and the tuple
(userId, projectId, roleId)should be unique.You’ll need to use dedicated queries (or Java methods) to find the roles of a user for a project :
or, in HQL :