简体   繁体   中英

Hibernate AssertionFailure

When I try to save a new entity to the database, I have the following error:

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

produced at the code

 session.save(nameBean)

but, "magically" it only appears at Production Server. When I try to reproduce the error at localhost, with the same code and data (using copy of the DB of Production Server, via bak file) it works ok.

What can it be?

EDIT: Adding the code that probably cause the error. The objective of that code is save the bean and update the otherBean in the same transaction, so if something wrong ocurrs make the rollback.

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

As the error message says, it's probably caused by attempt to use a session after it thrown an exception. Make sure your code doesn't swallow any Hibernate exceptions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM