Hibernate Post-Insert事件意外行为

时间:2013-02-06 18:37:40

标签: hibernate events exception insert

我正在使用Hibernate 3.5.6尝试使用Post-Insert事件监听器。我遇到的行为似乎与事后处理事件的概念不一致。我将向DB提交一个对象,该对象导致会话被刷新并且后插入事件被触发。触发插入后事件后,它会进行一些处理并最终抛出一个错误,然后错误一直传回原始事务。在这里,错误被捕获。我无法从这一点回滚事务,并且数据库中没有条目。如果删除了插入后事件侦听器,我已经确认我的代码按预期执行。

hibernate中post-anything事件的预期行为会影响原始事务吗?我的理解是,事后事件应完全独立于原始交易。

请参阅下面的代码段和堆栈跟踪。

STACK TRACE

at ems.server.domain.IpeManager.getCrcMessage(IpeManager.java:113) ~[bin/:na]
at ems.shared.hibernateEvents.PostInsertListener.onPostInsert(PostInsertListener.java:34) [bin/:na]
at org.hibernate.action.EntityInsertAction.postInsert(EntityInsertAction.java:148) [hibernate3.jar:3.5.6-Final]
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:127) [hibernate3.jar:3.5.6-Final]
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:267) [hibernate3.jar:3.5.6-Final]
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:259) [hibernate3.jar:3.5.6-Final]
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:178) [hibernate3.jar:3.5.6-Final]
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321) [hibernate3.jar:3.5.6-Final]
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51) [hibernate3.jar:3.5.6-Final]
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1206) [hibernate3.jar:3.5.6-Final]
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:375) [hibernate3.jar:3.5.6-Final]
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137) [hibernate3.jar:3.5.6-Final]
at ems.server.database.dao.Dao.saveOrUpdate(Dao.java:30) [bin/:na]
at ems.server.database.dao.IpeComponentDao.insertComponent(IpeComponentDao.java:29) [bin/:na]
at ems.server.domain.NetworkManager.insertIpeConfig(NetworkManager.java:2876) [bin/:na]

DAO CLASS

Session session = HibernateUtil.currentSession();
Transaction tx = null;
try {
       tx = session.beginTransaction();
       session.saveOrUpdate(obj);
       tx.commit();
       return obj;
} catch (ConstraintViolationException e) {
       if (tx != null)
              tx.rollback();
       throw e;
} catch (HibernateException he) {
       if (tx != null)
              tx.rollback();
       throw new HibernateException("Could not save object of class " + obj.getClass().getName(), he);
}

hibernate.cfg.xml中

<event type="post-insert">
        <listener class="ems.shared.hibernateEvents.PostInsertListener"/>
</event>

PostInsertListener类

package ems.shared.hibernateEvents;

import java.util.*;

import org.hibernate.event.PostInsertEvent;
import org.hibernate.event.PostInsertEventListener;

public class PostInsertListener implements PostInsertEventListener{

       private static final long serialVersionUID = 2L;

       public void onPostInsert(PostInsertEvent event)  {
          //DO SOME PROCESSING HERE
       }      
}

1 个答案:

答案 0 :(得分:1)

我知道这是一个旧线程,但在研究同样的问题时,我在这里找到了一个建议的解决方案:

Hibernate Event Listeners - Rollback

希望它能帮助将来的某个人。