1. 程式人生 > >JPA hibernate 異常 id變更報錯。identifier was altered

JPA hibernate 異常 id變更報錯。identifier was altered

org.springframework.orm.jpa.JpaSystemException: identifier of an instance of com.thinkgem.jeesite.modules.sys.entity.Office was altered from 151 to 404; nested exception is org.hibernate.HibernateException: identifier of an instance of com.thinkgem.jeesite.modules.sys.entity.Office was altered from 151 to 404
由於快取了變更之前的id,因此在做修改操作時,清空下balaTruckDao.clear();
@Transactional(readOnly = false)
     public void save(BalaTruck balaTruck) {
           //office.id變更導致hibernate持久化異常。因為一級快取儲存了修改之前的office.id。用clear清除session
           balaTruckDao.clear();
           //儲存時,前端傳入機構id,使用officeDao帶出機構code,再持久化儲存
          balaTruck.setTruckOwnCode(officeDao.findOne(balaTruck.getTruckOwnCode().getId()));         
           balaTruckDao.save(balaTruck);
     }