Search in sources :

Example 31 with EventListenerRegistry

use of org.hibernate.event.service.spi.EventListenerRegistry in project high-performance-java-persistence by vladmihalcea.

the class AssociationFetchingEventListenerIntegrator method integrate.

@Override
public void integrate(Metadata metadata, SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) {
    final EventListenerRegistry eventListenerRegistry = serviceRegistry.getService(EventListenerRegistry.class);
    eventListenerRegistry.prependListeners(EventType.LOAD, AssociationFetchPreLoadEventListener.INSTANCE);
    eventListenerRegistry.appendListeners(EventType.LOAD, AssociationFetchLoadEventListener.INSTANCE);
    eventListenerRegistry.appendListeners(EventType.POST_LOAD, AssociationFetchPostLoadEventListener.INSTANCE);
}
Also used : EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry)

Example 32 with EventListenerRegistry

use of org.hibernate.event.service.spi.EventListenerRegistry in project micronaut-data by micronaut-projects.

the class EventIntegrator method integrate.

@Override
public void integrate(Metadata metadata, SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) {
    EventListenerRegistry eventListenerRegistry = serviceRegistry.getService(EventListenerRegistry.class);
    final EntityEventListener<Object> entityEventListener = entityRegistry.getEntityEventListener();
    eventListenerRegistry.getEventListenerGroup(EventType.PRE_INSERT).appendListener((PreInsertEventListener) event -> {
        Class mappedClass = event.getPersister().getMappedClass();
        if (isNotSupportedMappedClass(mappedClass)) {
            return false;
        }
        final RuntimePersistentEntity<Object> entity = entityRegistry.getEntity(mappedClass);
        if (entity.hasPrePersistEventListeners()) {
            Object[] state = event.getState();
            final DefaultEntityEventContext<Object> context = new StatefulHibernateEventContext<>(entity, event, state);
            return !entityEventListener.prePersist(context);
        }
        return false;
    });
    eventListenerRegistry.getEventListenerGroup(EventType.POST_INSERT).appendListener(new PostInsertEventListener() {

        @Override
        public void onPostInsert(PostInsertEvent event) {
            Class mappedClass = event.getPersister().getMappedClass();
            if (isNotSupportedMappedClass(mappedClass)) {
                return;
            }
            final RuntimePersistentEntity<Object> entity = entityRegistry.getEntity(mappedClass);
            if (entity.hasPostPersistEventListeners()) {
                final DefaultEntityEventContext<Object> context = new SimpleHibernateEventContext<>(entity, event.getEntity());
                entityEventListener.postPersist(context);
            }
        }

        @Override
        public boolean requiresPostCommitHanding(EntityPersister persister) {
            return false;
        }
    });
    eventListenerRegistry.getEventListenerGroup(EventType.PRE_DELETE).appendListener((PreDeleteEventListener) event -> {
        Class mappedClass = event.getPersister().getMappedClass();
        if (isNotSupportedMappedClass(mappedClass)) {
            return false;
        }
        final RuntimePersistentEntity<Object> entity = entityRegistry.getEntity(mappedClass);
        if (entity.hasPreRemoveEventListeners()) {
            Object[] state = event.getDeletedState();
            final DefaultEntityEventContext<Object> context = new StatefulHibernateEventContext<>(entity, event, state);
            return !entityEventListener.preRemove(context);
        }
        return false;
    });
    eventListenerRegistry.getEventListenerGroup(EventType.POST_DELETE).appendListener(new PostDeleteEventListener() {

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

        @Override
        public void onPostDelete(PostDeleteEvent event) {
            Class mappedClass = event.getPersister().getMappedClass();
            if (isNotSupportedMappedClass(mappedClass)) {
                return;
            }
            final RuntimePersistentEntity<Object> entity = entityRegistry.getEntity(mappedClass);
            if (entity.hasPostPersistEventListeners()) {
                final DefaultEntityEventContext<Object> context = new SimpleHibernateEventContext<>(entity, event.getEntity());
                entityEventListener.postRemove(context);
            }
        }
    });
    eventListenerRegistry.getEventListenerGroup(EventType.PRE_UPDATE).appendListener((PreUpdateEventListener) event -> {
        Class mappedClass = event.getPersister().getMappedClass();
        if (isNotSupportedMappedClass(mappedClass)) {
            return false;
        }
        final RuntimePersistentEntity<Object> entity = entityRegistry.getEntity(mappedClass);
        if (entity.hasPreUpdateEventListeners()) {
            Object[] state = event.getState();
            final DefaultEntityEventContext<Object> context = new StatefulHibernateEventContext<>(entity, event, state);
            return !entityEventListener.preUpdate(context);
        }
        return false;
    });
    eventListenerRegistry.getEventListenerGroup(EventType.POST_UPDATE).appendListener(new PostUpdateEventListener() {

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

        @Override
        public void onPostUpdate(PostUpdateEvent event) {
            Class mappedClass = event.getPersister().getMappedClass();
            if (isNotSupportedMappedClass(mappedClass)) {
                return;
            }
            final RuntimePersistentEntity<Object> entity = entityRegistry.getEntity(mappedClass);
            if (entity.hasPostPersistEventListeners()) {
                final DefaultEntityEventContext<Object> context = new SimpleHibernateEventContext<>(entity, event.getEntity());
                entityEventListener.postUpdate(context);
            }
        }
    });
    eventListenerRegistry.getEventListenerGroup(EventType.POST_LOAD).appendListener((PostLoadEventListener) event -> {
        Class mappedClass = event.getPersister().getMappedClass();
        if (isNotSupportedMappedClass(mappedClass)) {
            return;
        }
        final RuntimePersistentEntity<Object> entity = entityRegistry.getEntity(mappedClass);
        if (entity.hasPreUpdateEventListeners()) {
            final DefaultEntityEventContext<Object> context = new SimpleHibernateEventContext<>(entity, event.getEntity());
            entityEventListener.postLoad(context);
        }
    });
}
Also used : org.hibernate.event.spi(org.hibernate.event.spi) DefaultEntityEventContext(io.micronaut.data.runtime.event.DefaultEntityEventContext) Integrator(org.hibernate.integrator.spi.Integrator) EntityPersister(org.hibernate.persister.entity.EntityPersister) EntityEventListener(io.micronaut.data.event.EntityEventListener) EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry) BeanProperty(io.micronaut.core.beans.BeanProperty) Singleton(jakarta.inject.Singleton) RuntimeEntityRegistry(io.micronaut.data.model.runtime.RuntimeEntityRegistry) Metadata(org.hibernate.boot.Metadata) EntityMetamodel(org.hibernate.tuple.entity.EntityMetamodel) Internal(io.micronaut.core.annotation.Internal) SessionFactoryServiceRegistry(org.hibernate.service.spi.SessionFactoryServiceRegistry) Primary(io.micronaut.context.annotation.Primary) RuntimePersistentEntity(io.micronaut.data.model.runtime.RuntimePersistentEntity) BeanIntrospector(io.micronaut.core.beans.BeanIntrospector) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) EntityPersister(org.hibernate.persister.entity.EntityPersister) RuntimePersistentEntity(io.micronaut.data.model.runtime.RuntimePersistentEntity) DefaultEntityEventContext(io.micronaut.data.runtime.event.DefaultEntityEventContext) EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry)

Example 33 with EventListenerRegistry

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

the class IntegratorProvider method integrate.

@Override
public void integrate(Metadata metadata, SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) {
    final EventListenerRegistry eventListenerRegistry = serviceRegistry.getService(EventListenerRegistry.class);
    // Replace the flush entity listeners which removes the standard DefaultFlushEntityEventListener
    eventListenerRegistry.setListeners(EventType.FLUSH_ENTITY, new FlushEntityEventListener());
}
Also used : EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry)

Example 34 with EventListenerRegistry

use of org.hibernate.event.service.spi.EventListenerRegistry in project webofneeds by researchstudio-sat.

the class ParentAwareEventListenerIntegrator method integrate.

@Override
public void integrate(Metadata metadata, SessionFactoryImplementor sessionFactoryImplementor, SessionFactoryServiceRegistry sessionFactoryServiceRegistry) {
    logger.debug("integrating listeners for ParentAware entities");
    final EventListenerRegistry eventListenerRegistry = sessionFactoryServiceRegistry.getService(EventListenerRegistry.class);
    eventListenerRegistry.appendListeners(EventType.PERSIST, ParentAwarePersistEventListener.INSTANCE);
    eventListenerRegistry.appendListeners(EventType.FLUSH_ENTITY, ParentAwareFlushEventListener.INSTANCE);
}
Also used : EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry)

Example 35 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();
    EventListenerRegistry listenerRegistry = em.unwrap(SharedSessionContractImplementor.class).getFactory().getServiceRegistry().getService(EventListenerRegistry.class);
    assertEquals("Explicit pre-insert event through hibernate.ejb.event.pre-insert does not work", listenerRegistry.getEventListenerGroup(EventType.PRE_INSERT).count(), listenerRegistry.getEventListenerGroup(EventType.PRE_UPDATE).count() + 1);
    em.close();
    emf.close();
}
Also used : EntityManager(javax.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.Test)

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