I am working in Spring-Hibernate
application.My question is related to orphan removal
as described below in the code.
@Entity
public class User {
...........
@OneToMany(mappedBy = "user", orphanRemoval = true, cascade = CascadeType.ALL)
List<UserRole> userRoles = new ArrayList<>();
..........
}
Considering the save/update User
scenario.
One way is to remove that child object from the list like user.getUserRoles().remove(userRole)
.
The other way could be like clearing the child list as user.getUserRoles().clear()
and then adding it to the list whatever user roles are coming in the request. In this case, user roles not coming in the request will be deleted by orphan removal
.
Which one is better and more correct?