org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1


源代碼

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;
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM