2016-09-28 116 views
1

三元关系,我有四个tables.entities,实现在休眠

  1. 用户
  2. 项目
  3. 角色
  4. UserProjectRole

我的情况或关系,在一个项目中,一个用户有很多角色。 我很困惑地在上面的hibernate实体中映射关系。 需要一些帮助。

在此先感谢。

+0

UserProjectRole与其他实体的关系如何。 OR UserProjectRole表的使用情况? –

+0

我将用它来存储哪个用户在哪个项目中具有哪个角色。例如,用户“A”在项目“P”中具有“ScrumMaster”角色。 –

+0

用户可以在一个项目中拥有多个角色吗?例如用户A既是Scrummaster又是项目P的开发人员? – dcsohl

回答

1

有很多方法可以实现与Hibernate的所谓三元关系,或者利用"simple" map或者使用中间实体/表实现关联。你的用例决定哪个是正确的。这取决于数据如何存储以及您将如何(经常)读取它。报告也是一个重要的用例。

如果您希望允许许多用户 - 项目 - 角色组合,则映射(例如用户实体上的映射)并不十分合适,因为您的密钥可能是项目或角色,并且密钥只能出现一次。尽管如此,每个关系条目在系统中都应该是唯一的,所以在这种情况下,我倾向于至少具有一些独特约束的中间表。

的“快速和肮脏”的方式将是一个实体,如:

import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.Id; 
import javax.persistence.ManyToOne; 
import javax.persistence.Table; 
import javax.persistence.UniqueConstraint; 

@Entity 
@Table(uniqueConstraints = @UniqueConstraint(columnNames={"user_id", "project_id", "role_id"})) 
public class UserProjectRoleSimple { 

    @Id 
    @GeneratedValue 
    private Long id; 

    @ManyToOne 
    private User user; 
    @ManyToOne 
    private Project project; 
    @ManyToOne 
    private Role role; 

    // you also need constructors, getters, equals, hashcode and stuff 

} 

另一个(更好的)方法是使用关系对象标识符作为复合键。这有点冗长,但你不需要额外的代理键,所以你的连接表更干净。

import java.io.Serializable; 

import javax.persistence.Embeddable; 
import javax.persistence.EmbeddedId; 
import javax.persistence.Entity; 
import javax.persistence.JoinColumn; 
import javax.persistence.ManyToOne; 
import javax.validation.constraints.NotNull; 

import org.hibernate.annotations.Immutable; 

@Entity 
@Immutable 
public class UserProjectRole { 

    protected UserProjectRole() { 
    } 

    public UserProjectRole(final User user, final Project project, final Role role) { 
     this.userProjectRoleId = new UserProjectRoleId(user, project, role); 
     this.user = user; 
     this.project = project; 
     this.role = role; 
    } 

    @EmbeddedId 
    protected UserProjectRoleId userProjectRoleId; 

    @ManyToOne 
    @JoinColumn(name = "userId", insertable = false, updatable = false) 
    private User user; 
    @ManyToOne 
    @JoinColumn(name = "projectId", insertable = false, updatable = false) 
    private Project project; 
    @ManyToOne 
    @JoinColumn(name = "roleId", insertable = false, updatable = false) 
    private Role role; 

    public User getUser() { 
     return user; 
    } 

    public Project getProject() { 
     return project; 
    } 

    public Role getRole() { 
     return role; 
    } 

    @Embeddable 
    static class UserProjectRoleId implements Serializable { 

     private static final long serialVersionUID = 7994974851694559677L; 

     @NotNull 
     private Long userId; 
     @NotNull 
     private Long projectId; 
     @NotNull 
     private Long roleId; 

     protected UserProjectRoleId() { 
     } 

     protected UserProjectRoleId(final User user, final Project project, final Role role) { 
      this.userId = user.getId(); 
      this.projectId = project.getId(); 
      this.roleId = role.getId(); 
     } 

     @Override 
     public int hashCode() { 
      final int prime = 31; 
      int result = 1; 
      result = prime * result + ((projectId == null) ? 0 : projectId.hashCode()); 
      result = prime * result + ((roleId == null) ? 0 : roleId.hashCode()); 
      result = prime * result + ((userId == null) ? 0 : userId.hashCode()); 
      return result; 
     } 

     @Override 
     public boolean equals(Object obj) { 
      if (this == obj) 
       return true; 
      if (obj == null) 
       return false; 
      if (getClass() != obj.getClass()) 
       return false; 
      UserProjectRoleId other = (UserProjectRoleId) obj; 
      if (projectId == null) { 
       if (other.projectId != null) 
        return false; 
      } else if (!projectId.equals(other.projectId)) 
       return false; 
      if (roleId == null) { 
       if (other.roleId != null) 
        return false; 
      } else if (!roleId.equals(other.roleId)) 
       return false; 
      if (userId == null) { 
       if (other.userId != null) 
        return false; 
      } else if (!userId.equals(other.userId)) 
       return false; 
      return true; 
     } 

    } 

} 

该映射也很简单,只有非凡的部分是@JoinColumn(name = "...Id", insertable = false, updatable = false)增加。有了它,您可以使用您的映射实体(用于导航目的),而不需要存储两次。