簡體   English   中英

Hibernate 斷言失敗

[英]Hibernate AssertionFailure

當我嘗試將新實體保存到數據庫時,出現以下錯誤:

org.hibernate.AssertionFailure: null id in xxx.nameBean entry (don't flush the Session after an exception occurs)

在代碼處產生

 session.save(nameBean)

但是,“神奇地”它只出現在生產服務器上。 當我嘗試使用相同的代碼和數據(使用生產服務器數據庫的副本,通過 bak 文件)在本地主機上重現錯誤時,它可以正常工作。

會是什么?

編輯:添加可能導致錯誤的代碼。 該代碼的目標是保存 bean 並在同一事務中更新 otherBean,因此如果出現錯誤,則進行回滾。

public class BeanDao extends ManagedSession {

public Integer save(Bean bean) {
    Session session = null;
    try {
        session = createNewSessionAndTransaction();

        Integer idValoracio = (Integer) session.save(bean);
        doOtherAction(bean);

        commitTransaction(session);

        return idBean;
    } catch (RuntimeException re) {
        log.error("get failed", re);
        if (session != null) {
            rollbackTransaction(session);
        }
        throw re;
    }
}

private void doOtherAction(Bean bean) {
    Integer idOtherBean = bean.getIdOtherBean();
    OtherBeanDao otherBeanDao = new OtherBeanDao();
    OtherBean otherBean = otherBeanDao.findById(idOtherBean);
    .
    .
    .
    otherBeanDao.attachDirty(otherBean)
}
}

正如錯誤消息所說,它可能是由於在拋出異常后嘗試使用 session 引起的。 確保您的代碼不會吞下任何 Hibernate 異常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM