【问题标题】:Hibernate AssertionFailure休眠断言失败
【发布时间】:2011-05-03 10:05:23
【问题描述】:

当我尝试将新实体保存到数据库时,出现以下错误:

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

【问题讨论】:

    标签: java sql-server hibernate


    【解决方案1】:

    正如错误消息所说,它可能是由于在抛出异常后尝试使用会话引起的。确保您的代码不会吞下任何 Hibernate 异常。

    【讨论】:

    • 这很有趣。我添加了代码,因为我不知道如何在同一个事务中正确执行这两个操作。如果我把 doOtherAction(bean);在提交事务(会话)之后;我认为这可以解决错误,但是这些操作不在同一个事务中
    猜你喜欢
    • 1970-01-01
    • 2017-08-04
    • 2013-01-02
    • 2011-05-27
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    相关资源
    最近更新 更多