假设我在我的UserDAO类中有这个:
@OneToMany(cascade=CascadeType.ALL, mappedBy="owner", fetch=FetchType.EAGER, orphanRemoval=true)
private Set<Vehicle> vehicles = new HashSet<Vehicle>();
这是向用户添加新车的推荐方法:
User user = userService.findByLoginName("MartinL");
Vehicle newVehicle = new Vehicle();
newVehicle.set(...) // setters omitted
newVehicle.setOwner(user) // is this needed in any case?
user.getVehicles().add(newVehicle) // add the new vehicle to the Set in User class
userService.save(user); // persist the modified user object to database
这是最佳做法还是我错过了什么?