在Hibernate中实现三元关系

时间:2016-09-28 14:19:25

标签: hibernate

我有四个table.entities,

  1. 用户
  2. 项目
  3. 角色
  4. UserProjectRole
  5. 我的场景或关系是,在一个项目中,一个用户有很多角色。 我很困惑在上面的hibernate实体中映射关系。 需要一些帮助。

    提前致谢。

1 个答案:

答案 0 :(得分: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)添加。有了它,你可以使用你的映射实体(用于导航),而无需存储两次。