hibernate: ternary association mapping

纵然是瞬间 提交于 2019-12-01 09:52:02

问题


Technology Description: Hibernate annotation- 3.4.0.GA java 1.5

table : users_roles_branches columns : user_id, role_id, branch_id

A user is assigned different roles for different branches of a company.

Now i have one java pojo class

     public class branch
     {
                @ManyToMany
             @JoinTable(name = "users_roles_branches", joinColumns = {   @JoinColumn(name="branch_id") }, inverseJoinColumns = { @JoinColumn(name = "role_id") })
             @MapKeyManyToMany(joinColumns = { @JoinColumn(name = "user_id", unique = false) })

                public Map<User, Role> getUserRoleMap() {
          return userRoleMap;
           }
     } 

Basic requirement is to retrieve list of roles assigned to different users in a branch.

Problem facing: since one user can have multiple roles assigned to it, map will no work for user-role mapping data.

One solution could be Map>, but i doubt if i can use nested collection with hibernate.

Please help me out!

If question is not understandable or not in representable form please let me know.


回答1:


My suggestion would be to introduce a new concept in your domain-model RoleAssignment:

class RoleAssignment {
  private User user; 
  private Branch branch;
  private Role role;
}

Entities User, Branch and Role should have 1:N relationship with RoleAssignment. For eg:

class User { 
  private Set<RoleAssignment> roleAssignemnts;
}


来源:https://stackoverflow.com/questions/4415260/hibernate-ternary-association-mapping

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!