Keep in mind that there are two units: Department and Employee, where there are N employees in one department.
In the department:
@OneToMany(mappedBy = "department", fetch = FetchType.EAGER)
private Collection<Employee> employees = new ArrayList<Employee>();
In Employee:
@ManyToOne(fetch = FetchType.EAGER)
private Department department;
Everything works, but I would like to add employees to the department without establishing feedback. For instance:
department.getEmployees().add(employee1);
department.getEmployees().add(employee2);
employee1.setDepartment(department);
employee2.setDepartment(department);
entityManager.merge(department);
So my question is: is there any way (for example, by some kind of annotation) that the JPA will understand that it should propagate the changes to the other side of the relationship without explicitly? In other words, I would only do this:
department.getEmployees().add(employee1);
department.getEmployees().add(employee2);
entityManager.merge(department);
Thank you so much!
source
share