考虑有两个实体,Department和Employee,其中一个部门中有N个员工。JPA - @OneToMany更新
在Departament:
@OneToMany(mappedBy = "department", fetch = FetchType.EAGER)
private Collection<Employee> employees = new ArrayList<Employee>();
在员工:
@ManyToOne(fetch = FetchType.EAGER)
private Department department;
一切正常,但我想给员工添加到部门没有设置反向关系。例如:
// I will add two employees to a department
department.getEmployees().add(employee1);
department.getEmployees().add(employee2);
// In fact, it is necessary to set the opposite side of the relationship
employee1.setDepartment(department);
employee2.setDepartment(department);
entityManager.merge(department);
//...
所以,我的问题是:是有一些方法的JPA会明白,它应该更改传播到关系的另一侧,而我明确的说(例如,由一些注释。)?换句话说,我只想做到这一点:
department.getEmployees().add(employee1);
department.getEmployees().add(employee2);
entityManager.merge(department);
非常感谢!