2 从不同实体进入新表的外键 Hibernate [英] 2 Foreign Keys Into a New Table from Different Entities Hibernate

查看:27
本文介绍了2 从不同实体进入新表的外键 Hibernate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,人们拥有基于角色的访问权限.一个人可以在多个部门工作.

In my projecet people has role based access.One person can work at more than one departments.

我的角色表

Role_id Role
1       Manager
2       Employee

我的部门表

Departmant_id Departmant
1             Production
2             Research
3             Marketing

我的用户表

User_id User_name
1       Jennifer
2       Kate
3       David

我想要的是一个新表,指定哪些人在哪个部门以及他们在该部门扮演什么角色.

What i want is a new table that specifies which people are in which departmant and what role do they have in that department.

User_id Departmant_id Role_id
x       x             x

我尝试的是

Class User{
     @ManyToOne(cascade = CascadeType.ALL)
     @JoinTable(name = "user_department_role",joinColumns = {@JoinColumn(name = "department_id",referencedColumnName = "department_id"),@JoinColumn(name = "user_id",referencedColumnName = "user_id")}, inverseJoinColumns = {@JoinColumn(name = "role_id")})
     private Set<Department> departmentList;
}

推荐答案

您需要一个关联表,通常出于各种原因在 JPA 中构建,主要是为了控制表中的内容,或者在这种情况下映射 n 路M:N 关系.

You need an association table, often constructed in JPA for various reasons mostly to do with control over what goes in the table or in this case mapping an n-way M:N relationship.

创建所有实体:

@Entity
public class User {
    @Id @GeneratedValue(strategy=GenerationType.AUTO) 
    private Integer id;
    private String userName;
    @OneToMany(mappedBy="user")
    private Set<UserDepartmentRoleAssociation> associations;
... etc
}

@Entity
public class Department {
    @Id @GeneratedValue(strategy=GenerationType.AUTO) 
    private Integer id;
    private String department;
    @OneToMany(mappedBy="department")
    private Set<UserDepartmentRoleAssociation> associations;
    ... etc
}

@Entity
public class Role {
    @Id @GeneratedValue(strategy=GenerationType.AUTO) 
    private Integer id;
    private String role;
    ... etc
}

并创建您的关联表和 id 类.

and create your association table and id class.

@Entity
public class UserDepartmentRoleAssociation {
    @EmbeddedId private UserDepartmentRoleAssociationId id;
    @ManyToOne @MapsId("userId")
    private User user;
    @ManyToOne @MapsId("departmentId")
    private Department department;
    @ManyToOne @MapsId("roleId")
    private Role role;
    public UserDepartmentRoleAssociation() {
        id = new UserDepartmentRoleAssociationId();
    }
    ... etc
}

@Embeddable
public class UserDepartmentRoleAssociationId implements Serializable {
    private Integer userId;
    private Integer departmentId;
    private Integer roleId;
    ... etc
}

然后保持关系......

and to persist a relationship then ...

        User user = new User();
        user.setUserName("user1");

        Department department = new Department();
        department.setDepartment("department 1");

        Role role = new Role();
        role.setRole("Manager");

        UserDepartmentRoleAssociation association = new UserDepartmentRoleAssociation();
        association.setUser(user);
        association.setDepartment(department);
        association.setRole(role);

        em.persist(user);
        em.persist(department);
        em.persist(role);
        em.persist(association);

然后用join fetch读取它

and to read it with join fetch then

User user = em.createQuery("select u from User u left join fetch u.associations ass left join fetch ass.department left join fetch ass.role where u.id = :id", User.class).setParameter("id", 1).getSingleResult();

请注意,我在 DepartmentUser 中使用了 Set 而不是 List 这会导致更少这些情况下的问题.此外,当我保持关系时,我不必创建 associations,因为 UserDepartmentRoleAssociation 是拥有实体,因此会进行持久化.associations 集由 JPA 在读取记录时创建.

Note that I have used a Set instead of a List in Department and User which causes much less problems in these cases. Also, I don't have to create associations when I persist the relationship because the UserDepartmentRoleAssociation is the owning entity and therefore does the persisting. The associations sets are created by JPA when it reads a record.

这篇关于2 从不同实体进入新表的外键 Hibernate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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