Search in sources :

Example 1 with EventListenerRegistry

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

the class EnversIntegrator method integrate.

public void integrate(Metadata metadata, SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) {
    final EnversService enversService = serviceRegistry.getService(EnversService.class);
    // Opt-out of registration if EnversService is disabled
    if (!enversService.isEnabled()) {
        log.debug("Skipping Envers listener registrations : EnversService disabled");
        return;
    }
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // Opt-out of registration if asked to not register
    final boolean autoRegister = serviceRegistry.getService(ConfigurationService.class).getSetting(AUTO_REGISTER, StandardConverters.BOOLEAN, true);
    if (!autoRegister) {
        log.debug("Skipping Envers listener registrations : Listener auto-registration disabled");
        return;
    }
    // Verify that the EnversService is fully initialized and ready to go.
    if (!enversService.isInitialized()) {
        throw new HibernateException("Expecting EnversService to have been initialized prior to call to EnversIntegrator#integrate");
    }
    // Opt-out of registration if no audited entities found
    if (!enversService.getEntitiesConfigurations().hasAuditedEntities()) {
        log.debug("Skipping Envers listener registrations : No audited entities found");
        return;
    }
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    // Do the registrations
    final EventListenerRegistry listenerRegistry = serviceRegistry.getService(EventListenerRegistry.class);
    listenerRegistry.addDuplicationStrategy(EnversListenerDuplicationStrategy.INSTANCE);
    if (enversService.getEntitiesConfigurations().hasAuditedEntities()) {
        listenerRegistry.appendListeners(EventType.POST_DELETE, new EnversPostDeleteEventListenerImpl(enversService));
        listenerRegistry.appendListeners(EventType.POST_INSERT, new EnversPostInsertEventListenerImpl(enversService));
        listenerRegistry.appendListeners(EventType.PRE_UPDATE, new EnversPreUpdateEventListenerImpl(enversService));
        listenerRegistry.appendListeners(EventType.POST_UPDATE, new EnversPostUpdateEventListenerImpl(enversService));
        listenerRegistry.appendListeners(EventType.POST_COLLECTION_RECREATE, new EnversPostCollectionRecreateEventListenerImpl(enversService));
        listenerRegistry.appendListeners(EventType.PRE_COLLECTION_REMOVE, new EnversPreCollectionRemoveEventListenerImpl(enversService));
        listenerRegistry.appendListeners(EventType.PRE_COLLECTION_UPDATE, new EnversPreCollectionUpdateEventListenerImpl(enversService));
    }
}
Also used : HibernateException(org.hibernate.HibernateException) EnversPreUpdateEventListenerImpl(org.hibernate.envers.event.spi.EnversPreUpdateEventListenerImpl) EnversPreCollectionRemoveEventListenerImpl(org.hibernate.envers.event.spi.EnversPreCollectionRemoveEventListenerImpl) EnversPreCollectionUpdateEventListenerImpl(org.hibernate.envers.event.spi.EnversPreCollectionUpdateEventListenerImpl) EnversPostInsertEventListenerImpl(org.hibernate.envers.event.spi.EnversPostInsertEventListenerImpl) ConfigurationService(org.hibernate.engine.config.spi.ConfigurationService) EnversPostUpdateEventListenerImpl(org.hibernate.envers.event.spi.EnversPostUpdateEventListenerImpl) EnversPostCollectionRecreateEventListenerImpl(org.hibernate.envers.event.spi.EnversPostCollectionRecreateEventListenerImpl) EnversPostDeleteEventListenerImpl(org.hibernate.envers.event.spi.EnversPostDeleteEventListenerImpl) EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry)

Example 2 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) {
    if (!sessionFactory.getSessionFactoryOptions().isAutoEvictCollectionCache()) {
        // feature is disabled
        return;
    }
    if (!sessionFactory.getSessionFactoryOptions().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 : EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry)

Example 3 with EventListenerRegistry

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

the class LazyProxyOnEnhancedEntityTestTask method execute.

public void execute() {
    EventListenerRegistry registry = getFactory().unwrap(SessionFactoryImplementor.class).getServiceRegistry().getService(EventListenerRegistry.class);
    registry.prependListeners(EventType.FLUSH, new JpaFlushEventListener());
    registry.prependListeners(EventType.LOAD, new ImmediateLoadTrap());
    EntityManager em = getFactory().createEntityManager();
    em.getTransaction().begin();
    Parent p = em.find(Parent.class, parentID);
    // unwanted lazy load occurs here
    em.flush();
    em.getTransaction().commit();
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) JpaFlushEventListener(org.hibernate.jpa.event.internal.core.JpaFlushEventListener) EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry)

Example 4 with EventListenerRegistry

use of org.hibernate.event.service.spi.EventListenerRegistry 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 5 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 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)

Aggregations

EventListenerRegistry (org.hibernate.event.service.spi.EventListenerRegistry)28 SessionFactoryImpl (org.hibernate.internal.SessionFactoryImpl)6 EventType (org.hibernate.event.spi.EventType)5 Test (org.junit.Test)5 PostConstruct (javax.annotation.PostConstruct)4 HibernateException (org.hibernate.HibernateException)4 ConfigurationService (org.hibernate.engine.config.spi.ConfigurationService)4 PreInsertEventListener (org.hibernate.event.spi.PreInsertEventListener)4 TestForIssue (org.hibernate.testing.TestForIssue)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ClassLoaderService (org.hibernate.boot.registry.classloading.spi.ClassLoaderService)3 PostInsertEvent (org.hibernate.event.spi.PostInsertEvent)3 PostInsertEventListener (org.hibernate.event.spi.PostInsertEventListener)3 EntityPersister (org.hibernate.persister.entity.EntityPersister)3 CascadeType (javax.persistence.CascadeType)2 Entity (javax.persistence.Entity)2 EntityManager (javax.persistence.EntityManager)2 EntityManagerFactory (javax.persistence.EntityManagerFactory)2 GeneratedValue (javax.persistence.GeneratedValue)2