Search in sources :

Example 46 with EventListenerRegistry

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

the class DebeziumPreStartupRoutine method execute.

@Override
public void execute() throws Exception {
    tableNameToEntityMapping.init();
    SessionFactoryImpl sessionFactory = emf.unwrap(SessionFactoryImpl.class);
    EventListenerRegistry registry = sessionFactory.getServiceRegistry().getService(EventListenerRegistry.class);
    registry.getEventListenerGroup(EventType.FLUSH).appendListener(hibernateFlushListener);
}
Also used : SessionFactoryImpl(org.hibernate.internal.SessionFactoryImpl) EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry)

Example 47 with EventListenerRegistry

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

the class HibernateListenerConfigurer method init.

@PostConstruct
protected void init() {
    boolean auditEnabled = config.isEnabled(ConfigurationKey.AUDIT_ENABLED);
    boolean isTestAndNotAuditTest = isTestRun() && !isAuditTest();
    if (!auditEnabled || isTestAndNotAuditTest) {
        return;
    }
    SessionFactoryImpl sessionFactory = emf.unwrap(SessionFactoryImpl.class);
    EventListenerRegistry registry = sessionFactory.getServiceRegistry().getService(EventListenerRegistry.class);
    registry.getEventListenerGroup(EventType.POST_COMMIT_INSERT).appendListener(postInsertAuditListener);
    registry.getEventListenerGroup(EventType.POST_COMMIT_UPDATE).appendListener(postUpdateEventListener);
    registry.getEventListenerGroup(EventType.POST_COMMIT_DELETE).appendListener(postDeleteEventListener);
    if (auditMatrix.isReadEnabled()) {
        registry.getEventListenerGroup(EventType.POST_LOAD).appendListener(postLoadEventListener);
    }
}
Also used : SessionFactoryImpl(org.hibernate.internal.SessionFactoryImpl) EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry) PostConstruct(javax.annotation.PostConstruct)

Example 48 with EventListenerRegistry

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

the class TypeSafeActivator method applyCallbackListeners.

@SuppressWarnings({ "UnusedDeclaration" })
public static void applyCallbackListeners(ValidatorFactory validatorFactory, ActivationContext activationContext) {
    final Set<ValidationMode> modes = activationContext.getValidationModes();
    if (!(modes.contains(ValidationMode.CALLBACK) || modes.contains(ValidationMode.AUTO))) {
        return;
    }
    final ConfigurationService cfgService = activationContext.getServiceRegistry().getService(ConfigurationService.class);
    final ClassLoaderService classLoaderService = activationContext.getServiceRegistry().getService(ClassLoaderService.class);
    // asks for it
    if (cfgService.getSettings().get(Environment.CHECK_NULLABILITY) == null) {
        activationContext.getSessionFactory().getSessionFactoryOptions().setCheckNullability(false);
    }
    final BeanValidationEventListener listener = new BeanValidationEventListener(validatorFactory, cfgService.getSettings(), classLoaderService);
    final EventListenerRegistry listenerRegistry = activationContext.getServiceRegistry().getService(EventListenerRegistry.class);
    listenerRegistry.addDuplicationStrategy(DuplicationStrategyImpl.INSTANCE);
    listenerRegistry.appendListeners(EventType.PRE_INSERT, listener);
    listenerRegistry.appendListeners(EventType.PRE_UPDATE, listener);
    listenerRegistry.appendListeners(EventType.PRE_DELETE, listener);
    listener.initialize(cfgService.getSettings(), classLoaderService);
}
Also used : ConfigurationService(org.hibernate.engine.config.spi.ConfigurationService) ClassLoaderService(org.hibernate.boot.registry.classloading.spi.ClassLoaderService) EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry)

Example 49 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(jakarta.persistence.Entity) EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry) RequiresDialectFeature(org.hibernate.testing.RequiresDialectFeature) CascadeType(jakarta.persistence.CascadeType) Id(jakarta.persistence.Id) Test(org.junit.Test) GeneratedValue(jakarta.persistence.GeneratedValue) GenerationType(jakarta.persistence.GenerationType) EntityActionVetoException(org.hibernate.action.internal.EntityActionVetoException) BaseCoreFunctionalTestCase(org.hibernate.testing.junit4.BaseCoreFunctionalTestCase) OneToOne(jakarta.persistence.OneToOne) TransactionUtil.doInHibernate(org.hibernate.testing.transaction.TransactionUtil.doInHibernate) PreInsertEventListener(org.hibernate.event.spi.PreInsertEventListener) TestForIssue(org.hibernate.testing.TestForIssue) DialectChecks(org.hibernate.testing.DialectChecks) EventType(org.hibernate.event.spi.EventType) Assert.fail(org.junit.Assert.fail) EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry)

Example 50 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(jakarta.persistence.Entity) EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry) RequiresDialectFeature(org.hibernate.testing.RequiresDialectFeature) Assert.assertTrue(org.junit.Assert.assertTrue) CascadeType(jakarta.persistence.CascadeType) Id(jakarta.persistence.Id) Test(org.junit.Test) GeneratedValue(jakarta.persistence.GeneratedValue) GenerationType(jakarta.persistence.GenerationType) EntityActionVetoException(org.hibernate.action.internal.EntityActionVetoException) BaseCoreFunctionalTestCase(org.hibernate.testing.junit4.BaseCoreFunctionalTestCase) OneToOne(jakarta.persistence.OneToOne) TransactionUtil.doInHibernate(org.hibernate.testing.transaction.TransactionUtil.doInHibernate) TransientObjectException(org.hibernate.TransientObjectException) PreInsertEventListener(org.hibernate.event.spi.PreInsertEventListener) TestForIssue(org.hibernate.testing.TestForIssue) DialectChecks(org.hibernate.testing.DialectChecks) EventType(org.hibernate.event.spi.EventType) Assert.fail(org.junit.Assert.fail) EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry)

Aggregations

EventListenerRegistry (org.hibernate.event.service.spi.EventListenerRegistry)64 SessionFactoryImpl (org.hibernate.internal.SessionFactoryImpl)13 EventType (org.hibernate.event.spi.EventType)10 Test (org.junit.Test)10 PostConstruct (javax.annotation.PostConstruct)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