我正在Spring-Hibernate
应用程序中工作。我的问题与orphan removal
以下代码中的描述有关。
@Entity
public class User {
...........
@OneToMany(mappedBy = "user", orphanRemoval = true, cascade = CascadeType.ALL)
List<UserRole> userRoles = new ArrayList<>();
..........
}
考虑到save/update User
情景。一种方法是从列表中删除该子对象,例如user.getUserRoles().remove(userRole)
.
另一种方法可能是清除子列表user.getUserRoles().clear()
,然后将其添加到列表中,无论请求中的用户角色如何。在这种情况下,未出现在请求中的用户角色将被删除orphan removal
。
哪一个更好更正确?