大家在调用toplink的update方法时可能会出现update变成了insert操作的问题:
对于此问题,toplink推荐的update方法流程如下:
1. 通过toplink提供获取对象的方法而不是JDBC的方法来获得需要被更新的对象,并且对此方法配置事务(因为当提交事务以后,toplink的sessioncache才会被更新)
2. 对需要被更新的方法执行更新操作
3. 对此对象执行update方法来更新对象到数据库并更新sessioncache(toplink在执行update方法时会比对sessioncache与需要被更新对象的差别,如果sessioncache不存在,那么toplink会以为是一个新的对象,这样便会执行insert操作了)
exsample:
Public void updateOrder(Order order){
//调用toplink的方法来查询出需要被更新的order
Order updateOrder = **Service.getOrderById(order.getId());
//更新order状态
updateOrder.setStatus(3);
//执行toplink的更新操作
**Service.update(updateOrder);
}
其中getOrderById方法必须配置成有事务的,并且保证在执行update方法时,此事务已经被提交了