Search in sources :

Example 16 with EventListenerRegistry

use of org.hibernate.event.service.spi.EventListenerRegistry 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 requiresPostCommitHandling(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 17 with EventListenerRegistry

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

the class SessionWithSharedConnectionTest method testChildSessionCallsAfterTransactionAction.

@Test
@TestForIssue(jiraKey = "HHH-7239")
public void testChildSessionCallsAfterTransactionAction(SessionFactoryScope scope) throws Exception {
    Session session = scope.getSessionFactory().openSession();
    final String postCommitMessage = "post commit was called";
    EventListenerRegistry eventListenerRegistry = scope.getSessionFactory().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 requiresPostCommitHandling(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().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.jupiter.api.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 18 with EventListenerRegistry

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

the class CollectionCacheInvalidator method integrate.

private void integrate(SessionFactoryServiceRegistry serviceRegistry, SessionFactoryImplementor sessionFactory) {
    final SessionFactoryOptions sessionFactoryOptions = sessionFactory.getSessionFactoryOptions();
    if (!sessionFactoryOptions.isAutoEvictCollectionCache()) {
        // feature is disabled
        return;
    }
    if (!sessionFactoryOptions.isSecondLevelCacheEnabled()) {
        // Nothing to do, if caching is disabled
        return;
    }
    EventListenerRegistry eventListenerRegistry = serviceRegistry.getService(EventListenerRegistry.class);
    eventListenerRegistry.appendListeners(EventType.POST_INSERT, this);
    eventListenerRegistry.appendListeners(EventType.POST_DELETE, this);
    eventListenerRegistry.appendListeners(EventType.POST_UPDATE, this);
}
Also used : SessionFactoryOptions(org.hibernate.boot.spi.SessionFactoryOptions) EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry)

Example 19 with EventListenerRegistry

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

the class PackagedEntityManagerTest method testListeners.

@Test
public void testListeners() throws Exception {
    File testPackage = buildExplicitPar();
    addPackageToClasspath(testPackage);
    emf = Persistence.createEntityManagerFactory("manager1", new HashMap());
    EntityManager em = emf.createEntityManager();
    try {
        EventListenerRegistry listenerRegistry = em.unwrap(SharedSessionContractImplementor.class).getFactory().getServiceRegistry().getService(EventListenerRegistry.class);
        assertEquals(listenerRegistry.getEventListenerGroup(EventType.PRE_INSERT).count(), listenerRegistry.getEventListenerGroup(EventType.PRE_UPDATE).count() + 1, "Explicit pre-insert event through hibernate.ejb.event.pre-insert does not work");
    } finally {
        em.close();
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) HashMap(java.util.HashMap) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) File(java.io.File) EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry) Test(org.junit.jupiter.api.Test)

Example 20 with EventListenerRegistry

use of org.hibernate.event.service.spi.EventListenerRegistry in project openmrs-core by openmrs.

the class ObsPostLoadEventListener method registerListener.

@PostConstruct
public void registerListener() {
    EventListenerRegistry registry = ((SessionFactoryImpl) sessionFactory).getServiceRegistry().getService(EventListenerRegistry.class);
    registry.getEventListenerGroup(EventType.POST_LOAD).appendListener(this);
}
Also used : EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry) PostConstruct(javax.annotation.PostConstruct)

Aggregations

EventListenerRegistry (org.hibernate.event.service.spi.EventListenerRegistry)60 SessionFactoryImpl (org.hibernate.internal.SessionFactoryImpl)13 Test (org.junit.Test)10 PostConstruct (javax.annotation.PostConstruct)9 EventType (org.hibernate.event.spi.EventType)9 PreInsertEventListener (org.hibernate.event.spi.PreInsertEventListener)8 TestForIssue (org.hibernate.testing.TestForIssue)8 HashMap (java.util.HashMap)6 EntityManager (javax.persistence.EntityManager)6 ConfigurationService (org.hibernate.engine.config.spi.ConfigurationService)6 PostInsertEvent (org.hibernate.event.spi.PostInsertEvent)6 PostInsertEventListener (org.hibernate.event.spi.PostInsertEventListener)6 EntityPersister (org.hibernate.persister.entity.EntityPersister)6 Map (java.util.Map)5 HibernateException (org.hibernate.HibernateException)5 Session (org.hibernate.Session)5 RevisionHistoryEventListener (com.gmoon.hibernateenvers.global.envers.listener.RevisionHistoryEventListener)4 EntityActionVetoException (org.hibernate.action.internal.EntityActionVetoException)4 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)4 PreInsertEvent (org.hibernate.event.spi.PreInsertEvent)4