源代碼
public Authentication deleteById(String id) { Authentication entity = super.get(id); if (entity != null) { getSession().delete(entity); } return entity; }
場景:平台的前台和后台為不同的用戶登錄,前台刷新頁面的過程中,后台的頁面剛好退出登錄,攔截器在前台獲取到了后台的用戶信息,但此時后台用戶信息在退出時已被刪除,后台用戶實體處於托管態。
異常分析:實體是托管態的時候,根據實體刪除會報錯。
修改后的代碼
public Authentication deleteById(String id) { Authentication entity = super.get(id); /*此處改為HQL語句刪除,原因是直接刪除對象如果是托管態的話會報錯*/ if (entity != null) { String hql = "delete Authentication bean where bean.id=:id"; getSession().createQuery(hql).setParameter("id", id) .executeUpdate(); } return entity; }