休眠:三元关联映射 [英] hibernate: ternary association mapping

查看:178
本文介绍了休眠:三元关联映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

技术说明:
Hibernate注释 - 3.4.0.GA
java 1.5



:users_roles_branches
:user_id,role_id,branch_id



用户为公司的不同分支分配不同的角色。 b
$ b

现在我有一个Java pojo类

  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;






基本要求是检索分配给不同角色的角色列表分支中的用户。

面临的问题:由于一个用户可以分配多个角色,map对用户角色映射数据无效。



一个解决方案可能是Map>,但我怀疑是否可以使用hibernate的嵌套集合。



请帮助我!



如果问题无法理解或者无法表达,请告诉我。

解决方案

我的建议是在您的域模型 RoleAssignment 中引入一个新概念:

  class RoleAssignment {
private用户用户;
私人分行;
私人角色角色;

实体用户分支角色应该与 RoleAssignment 具有1:N关系。 。例如:

  class User {
private Set< RoleAssignment> roleAssignemnts;
}


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.

解决方案

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;
}

这篇关于休眠:三元关联映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆