Si consideri che ci sono due entità, Dipartimento e Dipendente, dove in un reparto ci sono N dipendenti.JPA - @OneToMany update
In Departament:
@OneToMany(mappedBy = "department", fetch = FetchType.EAGER)
private Collection<Employee> employees = new ArrayList<Employee>();
In Impiegato:
@ManyToOne(fetch = FetchType.EAGER)
private Department department;
Tutto funziona, ma vorrei aggiungere dipendenti al reparto, senza impostare la relazione inversa. Per esempio:
// 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);
//...
Quindi, la mia domanda è: c'è qualche modo che l'APP capirà che si deve propagare le modifiche verso l'altro lato della relazione, senza che esplicita che (ad esempio, da qualche annotazione.)? In altre parole, vorrei fare solo questo:
department.getEmployees().add(employee1);
department.getEmployees().add(employee2);
entityManager.merge(department);
Grazie mille!