Search in sources :

Example 41 with EventListenerRegistry

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

the class LazyProxyOnEnhancedEntityTest method test.

@Test
public void test() {
    EventListenerRegistry registry = sessionFactory().getServiceRegistry().getService(EventListenerRegistry.class);
    registry.prependListeners(EventType.LOAD, new ImmediateLoadTrap());
    doInJPA(this::sessionFactory, em -> {
        em.find(Parent.class, parentID);
    // unwanted lazy load occurs on flush
    });
}
Also used : EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry) Test(org.junit.Test)

Example 42 with EventListenerRegistry

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

the class RegisterUserEventListenersTest method testTransactionProcessSynchronization.

@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());
}
Also used : StrTestEntity(org.hibernate.envers.test.entities.StrTestEntity) EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 43 with EventListenerRegistry

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

the class PreInsertEventListenerVetoBidirectionalTest method afterSessionFactoryBuilt.

@Override
protected void afterSessionFactoryBuilt() {
    super.afterSessionFactoryBuilt();
    EventListenerRegistry registry = sessionFactory().getServiceRegistry().getService(EventListenerRegistry.class);
    registry.appendListeners(EventType.PRE_INSERT, (PreInsertEventListener) event -> event.getEntity() instanceof Parent);
}
Also used : Entity(javax.persistence.Entity) CascadeType(javax.persistence.CascadeType) EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry) RequiresDialectFeature(org.hibernate.testing.RequiresDialectFeature) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) EntityActionVetoException(org.hibernate.action.internal.EntityActionVetoException) BaseCoreFunctionalTestCase(org.hibernate.testing.junit4.BaseCoreFunctionalTestCase) TransactionUtil.doInHibernate(org.hibernate.testing.transaction.TransactionUtil.doInHibernate) OneToOne(javax.persistence.OneToOne) GenerationType(javax.persistence.GenerationType) TransientObjectException(org.hibernate.TransientObjectException) PreInsertEventListener(org.hibernate.event.spi.PreInsertEventListener) TestForIssue(org.hibernate.testing.TestForIssue) GeneratedValue(javax.persistence.GeneratedValue) DialectChecks(org.hibernate.testing.DialectChecks) EventType(org.hibernate.event.spi.EventType) Assert.fail(org.junit.Assert.fail) Id(javax.persistence.Id) EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry)

Example 44 with EventListenerRegistry

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

the class PreInsertEventListenerVetoUnidirectionalTest method afterSessionFactoryBuilt.

@Override
protected void afterSessionFactoryBuilt() {
    super.afterSessionFactoryBuilt();
    EventListenerRegistry registry = sessionFactory().getServiceRegistry().getService(EventListenerRegistry.class);
    registry.appendListeners(EventType.PRE_INSERT, (PreInsertEventListener) event -> event.getEntity() instanceof Parent);
}
Also used : Entity(javax.persistence.Entity) CascadeType(javax.persistence.CascadeType) EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry) RequiresDialectFeature(org.hibernate.testing.RequiresDialectFeature) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) EntityActionVetoException(org.hibernate.action.internal.EntityActionVetoException) BaseCoreFunctionalTestCase(org.hibernate.testing.junit4.BaseCoreFunctionalTestCase) TransactionUtil.doInHibernate(org.hibernate.testing.transaction.TransactionUtil.doInHibernate) OneToOne(javax.persistence.OneToOne) GenerationType(javax.persistence.GenerationType) TransientObjectException(org.hibernate.TransientObjectException) PreInsertEventListener(org.hibernate.event.spi.PreInsertEventListener) TestForIssue(org.hibernate.testing.TestForIssue) GeneratedValue(javax.persistence.GeneratedValue) DialectChecks(org.hibernate.testing.DialectChecks) EventType(org.hibernate.event.spi.EventType) Assert.fail(org.junit.Assert.fail) Assert.assertEquals(org.junit.Assert.assertEquals) Id(javax.persistence.Id) EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry)

Example 45 with EventListenerRegistry

use of org.hibernate.event.service.spi.EventListenerRegistry in project webpieces by deanhiller.

the class HibernateEntityMeterBinder method bindTo.

@Override
public void bindTo(@NonNull MeterRegistry meterRegistry) {
    if (sessionFactory instanceof SessionFactoryImplementor) {
        EventListenerRegistry eventListenerRegistry = ((SessionFactoryImplementor) sessionFactory).getServiceRegistry().getService(EventListenerRegistry.class);
        EntityEventListener eventListener = new EntityEventListener(meterRegistry);
        eventListenerRegistry.appendListeners(EventType.POST_LOAD, eventListener);
        eventListenerRegistry.appendListeners(EventType.POST_DELETE, eventListener);
        eventListenerRegistry.appendListeners(EventType.POST_UPDATE, eventListener);
        eventListenerRegistry.appendListeners(EventType.POST_INSERT, eventListener);
    }
}
Also used : SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry)

Aggregations

EventListenerRegistry (org.hibernate.event.service.spi.EventListenerRegistry)63 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 EntityPersister (org.hibernate.persister.entity.EntityPersister)7 HashMap (java.util.HashMap)6 ConfigurationService (org.hibernate.engine.config.spi.ConfigurationService)6 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)6 PostInsertEvent (org.hibernate.event.spi.PostInsertEvent)6 PostInsertEventListener (org.hibernate.event.spi.PostInsertEventListener)6 Map (java.util.Map)5 EntityManager (javax.persistence.EntityManager)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 PreInsertEvent (org.hibernate.event.spi.PreInsertEvent)4