hi i'm updating parent entity (Department) which has some child(Employees) ,along with update i want to delete all child and add new set of child . and i have unique constraint for EMPLOYEE_NAME by DB side . first i'm finding department and calling clear (collection clear ) for list of employees then adding new child to that department and committing . But i'm getting unique constraint violation .if i use em.flush() after clear() it is deleting child and inserting new child . But as i know Flush() is an exceptional use, commit also uses flush() internally . is there any solution to delete and insert child in a single transaction ?? and i'm using orphanremoval = true in department entity
public void deleteEmployee(Department updatingDepartment){
List<Employee> employees = new ArrayList<Employee>();
Employee employee1 = new Employee();
employee1.setEmployeeAlary(16667);
employee1.setEmployeeName("manju");
Employee employee2 = new Employee();
employee2.setEmployeeAlary(16667);
employee2.setEmployeeName("SUNIL");
entityManager.getTransaction().begin();
Department foundDepartment = entityManager.find(Department.class, updatingDepartment.getDepartmentId());
foundDepartment.getEmployees().clear();
employee2.setDepartment(foundDepartment);
employee1.setDepartment(foundDepartment);
employees.add(employee1);
employees.add(employee2);
/*entityManager.flush();*/
foundDepartment.getEmployees().add(employee1);
foundDepartment.getEmployees().add(employee2);
entityManager.merge(foundDepartment);
entityManager.getTransaction().commit();
}