这个问题让我很生气:
所以让我们说这个非常简单的案例:
(spring/Hibernate/jpa2/@PersistenceContext(type=PersistenceContextType.TRANSACTION))
我们有 DAO 实体:
class User{
private Set<Car> carSet;
... usual setters + getters
}
class Car{
private User user;
... usual setters + getters
}
以下哪一项是通过 carId 删除汽车的推荐方法以及为什么(这将在 http post 请求中完成,除了此删除之外不会进行其他操作)?
1)简单的移除操作;
Car car = dao.find(Car.class, carId);
dao.remove(car);
2)首先清理引用。什么时候推荐这种方法?
Car car = dao.find(Car.class, carId);
car.getUser().getCarSet().remove(car); // this one as far as I know will usually fetch the other cars, and I think is not optimal, am I right?
car.setUser(null);
dao.remove(car);
先感谢您