1

我找到了我真正想使用的这个类:

org.hibernate.action.spi.AfterTransactionCompletionProcess - http://docs.jboss.org/hibernate/orm/3.6/javadocs/org/hibernate/action/AfterTransactionCompletionProcess.html

基本上,我希望在事务提交后发生一些自定义逻辑。但我终其一生都无法弄清楚如何使用这个东西。

我在哪里指定这个接口?任何例子都会很棒。

4

1 回答 1

2

在 Hibernate 4.3 的单元测试代码库中找到了一个示例:

org.hibernate.envers.test.integration.basic.RegisterUserEventListenersTest

准确显示我正在寻找的内容:

package org.hibernate.envers.test.integration.basic;

import org.hibernate.Session;
import org.hibernate.action.spi.AfterTransactionCompletionProcess;
import org.hibernate.action.spi.BeforeTransactionCompletionProcess;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.envers.internal.tools.MutableInteger;
import org.hibernate.envers.test.BaseEnversFunctionalTestCase;
import org.hibernate.envers.test.entities.StrTestEntity;
import org.hibernate.event.service.spi.EventListenerRegistry;
import org.hibernate.event.spi.EventType;
import org.hibernate.event.spi.PostInsertEvent;
import org.hibernate.event.spi.PostInsertEventListener;
import org.hibernate.persister.entity.EntityPersister;

import org.junit.Assert;
import org.junit.Test;

import org.hibernate.testing.TestForIssue;

/**
 * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
 */
public class RegisterUserEventListenersTest extends BaseEnversFunctionalTestCase {
    @Override
    protected Class<?>[] getAnnotatedClasses() {
        return new Class<?>[] {StrTestEntity.class};
    }

    @Test
    @TestForIssue(jiraKey = "HHH-7478")
    public void testTransactionProcessSynchronization() {
        final EventListenerRegistry registry = sessionFactory().getServiceRegistry()
                .getService( EventListenerRegistry.class );
        final CountingPostInsertTransactionBoundaryListener listener = new CountingPostInsertTransactionBoundaryListener();

        registry.getEventListenerGroup( EventType.POST_INSERT ).appendListener( listener );

        Session session = openSession();
        session.getTransaction().begin();
        StrTestEntity entity = new StrTestEntity( "str1" );
        session.save( entity );
        session.getTransaction().commit();
        session.close();

        // Post insert listener invoked three times - before/after insertion of original data,
        // revision entity and audit row.
        Assert.assertEquals( 3, listener.getBeforeCount() );
        Assert.assertEquals( 3, listener.getAfterCount() );
    }

    private static class CountingPostInsertTransactionBoundaryListener implements PostInsertEventListener {
        private final MutableInteger beforeCounter = new MutableInteger();
        private final MutableInteger afterCounter = new MutableInteger();

        @Override
        public void onPostInsert(PostInsertEvent event) {
            event.getSession().getActionQueue().registerProcess(
                    new BeforeTransactionCompletionProcess() {
                        @Override
                        public void doBeforeTransactionCompletion(SessionImplementor session) {
                            beforeCounter.increase();
                        }
                    }
            );
            event.getSession().getActionQueue().registerProcess(
                    new AfterTransactionCompletionProcess() {
                        @Override
                        public void doAfterTransactionCompletion(boolean success, SessionImplementor session) {
                            afterCounter.increase();
                        }
                    }
            );
        }

        @Override
        public boolean requiresPostCommitHanding(EntityPersister persister) {
            return true;
        }

        public int getBeforeCount() {
            return beforeCounter.get();
        }

        public int getAfterCount() {
            return afterCounter.get();
        }
    }
}
于 2013-06-05T02:43:28.707 回答