2011-05-03 14 views
0

當我嘗試一個新的實體保存到數據庫中,我有以下錯誤:休眠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,所以如果有錯誤的ocurrs進行回滾。

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

回答

1

正如錯誤消息所述,這可能是由於嘗試在引發異常之後使用會話引起的。確保你的代碼不會吞噬任何Hibernate異常。

+0

這很有趣。我添加了代碼,因爲我不知道如何在同一個事務中正確執行這兩個操作。如果我把doOtherAction(bean);在commitTransaction(session)之後;我認爲這解決了錯誤,但是這些操作不在同一個事務中 – 2011-05-03 10:43:30