Search in sources :

Example 1 with PostInsertEventListener

use of org.hibernate.event.spi.PostInsertEventListener in project hibernate-orm by hibernate.

the class EntityIdentityInsertAction method postInsert.

private void postInsert() {
    if (isDelayed) {
        getSession().getPersistenceContext().replaceDelayedEntityIdentityInsertKeys(delayedEntityKey, generatedId);
    }
    final EventListenerGroup<PostInsertEventListener> listenerGroup = listenerGroup(EventType.POST_INSERT);
    if (listenerGroup.isEmpty()) {
        return;
    }
    final PostInsertEvent event = new PostInsertEvent(getInstance(), generatedId, getState(), getPersister(), eventSource());
    for (PostInsertEventListener listener : listenerGroup.listeners()) {
        listener.onPostInsert(event);
    }
}
Also used : PostInsertEventListener(org.hibernate.event.spi.PostInsertEventListener) PostInsertEvent(org.hibernate.event.spi.PostInsertEvent)

Example 2 with PostInsertEventListener

use of org.hibernate.event.spi.PostInsertEventListener in project hibernate-orm by hibernate.

the class SessionWithSharedConnectionTest method testChildSessionCallsAfterTransactionAction.

@Test
@TestForIssue(jiraKey = "HHH-7239")
public void testChildSessionCallsAfterTransactionAction() throws Exception {
    Session session = openSession();
    final String postCommitMessage = "post commit was called";
    EventListenerRegistry eventListenerRegistry = sessionFactory().getServiceRegistry().getService(EventListenerRegistry.class);
    //register a post commit listener
    eventListenerRegistry.appendListeners(EventType.POST_COMMIT_INSERT, new PostInsertEventListener() {

        @Override
        public void onPostInsert(PostInsertEvent event) {
            ((IrrelevantEntity) event.getEntity()).setName(postCommitMessage);
        }

        @Override
        public boolean requiresPostCommitHanding(EntityPersister persister) {
            return true;
        }
    });
    session.getTransaction().begin();
    IrrelevantEntity irrelevantEntityMainSession = new IrrelevantEntity();
    irrelevantEntityMainSession.setName("main session");
    session.save(irrelevantEntityMainSession);
    //open secondary session to also insert an entity
    Session secondSession = session.sessionWithOptions().connection().flushBeforeCompletion(true).autoClose(true).openSession();
    IrrelevantEntity irrelevantEntitySecondarySession = new IrrelevantEntity();
    irrelevantEntitySecondarySession.setName("secondary session");
    secondSession.save(irrelevantEntitySecondarySession);
    session.getTransaction().commit();
    //both entities should have their names updated to the postCommitMessage value
    assertEquals(postCommitMessage, irrelevantEntityMainSession.getName());
    assertEquals(postCommitMessage, irrelevantEntitySecondarySession.getName());
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) PostInsertEventListener(org.hibernate.event.spi.PostInsertEventListener) IrrelevantEntity(org.hibernate.IrrelevantEntity) PostInsertEvent(org.hibernate.event.spi.PostInsertEvent) Session(org.hibernate.Session) EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 3 with PostInsertEventListener

use of org.hibernate.event.spi.PostInsertEventListener in project hibernate-orm by hibernate.

the class MergeListPreAndPostPersistTest method addEntityListeners.

private void addEntityListeners(final Order order) {
    EventListenerRegistry registry = sessionFactory().getServiceRegistry().getService(EventListenerRegistry.class);
    registry.setListeners(EventType.PRE_INSERT, new PreInsertEventListener() {

        @Override
        public boolean onPreInsert(PreInsertEvent event) {
            if (Order.class.isInstance(event.getEntity())) {
                assertEquals(order, event.getEntity());
                assertEquals(order.items, ((Order) event.getEntity()).items);
            }
            return false;
        }
    });
    registry.setListeners(EventType.POST_INSERT, new PostInsertEventListener() {

        public void onPostInsert(PostInsertEvent event) {
            if (Order.class.isInstance(event.getEntity())) {
                assertEquals(order, event.getEntity());
                assertEquals(order.items, ((Order) event.getEntity()).items);
            }
        }

        public boolean requiresPostCommitHanding(EntityPersister persister) {
            return false;
        }
    });
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) PostInsertEventListener(org.hibernate.event.spi.PostInsertEventListener) PreInsertEventListener(org.hibernate.event.spi.PreInsertEventListener) PreInsertEvent(org.hibernate.event.spi.PreInsertEvent) PostInsertEvent(org.hibernate.event.spi.PostInsertEvent) EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry)

Example 4 with PostInsertEventListener

use of org.hibernate.event.spi.PostInsertEventListener in project hibernate-orm by hibernate.

the class MergeListPreAndPostPersistWithIdentityTest method addEntityListeners.

private void addEntityListeners(final Order order) {
    EventListenerRegistry registry = sessionFactory().getServiceRegistry().getService(EventListenerRegistry.class);
    registry.setListeners(EventType.PRE_INSERT, new PreInsertEventListener() {

        @Override
        public boolean onPreInsert(PreInsertEvent event) {
            if (Order.class.isInstance(event.getEntity())) {
                assertEquals(order, event.getEntity());
                assertEquals(order.items, ((Order) event.getEntity()).items);
            }
            return false;
        }
    });
    registry.setListeners(EventType.POST_INSERT, new PostInsertEventListener() {

        public void onPostInsert(PostInsertEvent event) {
            if (Order.class.isInstance(event.getEntity())) {
                assertEquals(order, event.getEntity());
                assertEquals(order.items, ((Order) event.getEntity()).items);
            }
        }

        public boolean requiresPostCommitHanding(EntityPersister persister) {
            return false;
        }
    });
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) PostInsertEventListener(org.hibernate.event.spi.PostInsertEventListener) PreInsertEventListener(org.hibernate.event.spi.PreInsertEventListener) PreInsertEvent(org.hibernate.event.spi.PreInsertEvent) PostInsertEvent(org.hibernate.event.spi.PostInsertEvent) EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry)

Example 5 with PostInsertEventListener

use of org.hibernate.event.spi.PostInsertEventListener in project hibernate-orm by hibernate.

the class EntityInsertAction method postInsert.

private void postInsert() {
    final EventListenerGroup<PostInsertEventListener> listenerGroup = listenerGroup(EventType.POST_INSERT);
    if (listenerGroup.isEmpty()) {
        return;
    }
    final PostInsertEvent event = new PostInsertEvent(getInstance(), getId(), getState(), getPersister(), eventSource());
    for (PostInsertEventListener listener : listenerGroup.listeners()) {
        listener.onPostInsert(event);
    }
}
Also used : PostInsertEventListener(org.hibernate.event.spi.PostInsertEventListener) PostInsertEvent(org.hibernate.event.spi.PostInsertEvent)

Aggregations

PostInsertEvent (org.hibernate.event.spi.PostInsertEvent)7 PostInsertEventListener (org.hibernate.event.spi.PostInsertEventListener)7 EventListenerRegistry (org.hibernate.event.service.spi.EventListenerRegistry)3 EntityPersister (org.hibernate.persister.entity.EntityPersister)3 PostCommitInsertEventListener (org.hibernate.event.spi.PostCommitInsertEventListener)2 PreInsertEvent (org.hibernate.event.spi.PreInsertEvent)2 PreInsertEventListener (org.hibernate.event.spi.PreInsertEventListener)2 IrrelevantEntity (org.hibernate.IrrelevantEntity)1 Session (org.hibernate.Session)1 TestForIssue (org.hibernate.testing.TestForIssue)1 Test (org.junit.Test)1