[英]Spring Data JPA: How to set 2 foreign keys on the child entity during insert
I am new to Spring JPA and I am having some trouble with setting foreign keys on an entity when inserting a row.我是 Spring JPA 的新手,在插入行时在实体上设置外键时遇到了一些麻烦。
I have the following entities我有以下实体
@Entity
public class Department {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String departmentName;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "department_id")
List<Employee> employees;
}
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String employeeName;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "employee_id")
List<Role> roles;
@ManyToOne
@JoinColumn(name = "department_id", insertable = false, updatable = false)
private Department department;
}
@Entity
public class Role {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String roleName;
@ManyToOne
@JoinColumn(name = "employee_id", insertable = false, updatable = false)
private Employee employee;
@ManyToOne
@JoinColumn(name = "department_id", insertable = false, updatable = false)
private Department department;
}
And here is the Department Repository:这是部门存储库:
@Repository
public interface DepartmentRepository extends JpaRepository<Department, Integer> {
Department findById(long id);
}
With this approach, the department_id
in the employee
table and the employee_id
in the role
table is set correctly when I save a Department
object using departmentRepository.save(department)
.使用这种方法,当我使用
departmentRepository.save(department)
保存Department
对象时, employee
表中的department_id
ID 和role
表中的employee_id
ID 设置正确。
But I also want it to set the department_id
in the role
table.但我也希望它在
role
表中设置department_id
。 How can this be achieved?如何做到这一点? Currently the relationship Department and Role is indirect (ie it is through Employee) but would I have to create a direct relationship between the 2 entities?
目前部门和角色的关系是间接的(即通过员工),但我必须在这两个实体之间创建直接关系吗? I am not sure how to achieve this though.
我不确定如何实现这一点。 Any input will be appreciated.
任何输入将不胜感激。
Edit:编辑:
I want to model the relationship in this Entity Relationship diagram我想在这个实体关系图中建模关系
As I see that you already using我看到你已经在使用
@OneToMany(cascade = CascadeType.ALL)
So you knew that every update on Department with list Employee can do "many" things.因此,您知道 Department 的每次更新以及列表 Employee 都可以做“很多”事情。
So Employee do the same to Roles (your list) but the magic auto save/update won't help.所以员工对角色(你的列表)做同样的事情,但神奇的自动保存/更新不会有帮助。
Condition now:现在条件:
As I understand:我认为:
You have Department that 1-n to Employee (Employee can only have 1 Department)您有 1-n 到员工的部门(员工只能有 1 个部门)
Employee can have many Role员工可以有很多角色
As normal logic: Deparment 1-n Employee 1-n Roles按照正常逻辑:部门 1-n 员工 1-n 角色
But some how you want Role have object of Department also.
但是一些你想要的角色也有部门的对象。
So my first suggestion is change of logic code.所以我的第一个建议是更改逻辑代码。
And second: a way to solve your problem!第二:解决问题的方法!
when add Role to Employee, please access and add deparment_id also.将 Role 添加到 Employee 时,请访问并添加 deparment_id。 (Department save List Employee, when save Employee - list Roles also save with same logic, just make sure to add it)
(部门保存List Employee,保存Employee时-list Roles也保存相同的逻辑,只要确保添加它)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.