Search in sources :

Example 21 with EventListenerRegistry

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

the class CandlepinContextListener method insertValidationEventListeners.

/**
 * There's no way to really get Guice to perform injections on stuff that
 * the JpaPersistModule is creating, so we resort to grabbing the EntityManagerFactory
 * after the fact and adding the Validation EventListener ourselves.
 * @param injector
 */
private void insertValidationEventListeners(Injector injector) {
    javax.inject.Provider<EntityManagerFactory> emfProvider = injector.getProvider(EntityManagerFactory.class);
    HibernateEntityManagerFactory hibernateEntityManagerFactory = (HibernateEntityManagerFactory) emfProvider.get();
    SessionFactoryImpl sessionFactoryImpl = (SessionFactoryImpl) hibernateEntityManagerFactory.getSessionFactory();
    EventListenerRegistry registry = sessionFactoryImpl.getServiceRegistry().getService(EventListenerRegistry.class);
    javax.inject.Provider<BeanValidationEventListener> listenerProvider = injector.getProvider(BeanValidationEventListener.class);
    registry.getEventListenerGroup(EventType.PRE_INSERT).appendListener(listenerProvider.get());
    registry.getEventListenerGroup(EventType.PRE_UPDATE).appendListener(listenerProvider.get());
    registry.getEventListenerGroup(EventType.PRE_DELETE).appendListener(listenerProvider.get());
}
Also used : BeanValidationEventListener(org.hibernate.cfg.beanvalidation.BeanValidationEventListener) HibernateEntityManagerFactory(org.hibernate.jpa.HibernateEntityManagerFactory) EntityManagerFactory(javax.persistence.EntityManagerFactory) HibernateEntityManagerFactory(org.hibernate.jpa.HibernateEntityManagerFactory) SessionFactoryImpl(org.hibernate.internal.SessionFactoryImpl) EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry)

Example 22 with EventListenerRegistry

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

the class HibernateSearchEventListener method registerTo.

public void registerTo(SessionFactoryImplementor sessionFactory) {
    EventListenerRegistry listenerRegistry = HibernateOrmUtils.getServiceOrFail(sessionFactory.getServiceRegistry(), EventListenerRegistry.class);
    listenerRegistry.addDuplicationStrategy(new KeepIfSameClassDuplicationStrategy(HibernateSearchEventListener.class));
    listenerRegistry.appendListeners(EventType.POST_INSERT, this);
    listenerRegistry.appendListeners(EventType.POST_UPDATE, this);
    listenerRegistry.appendListeners(EventType.POST_DELETE, this);
    listenerRegistry.appendListeners(EventType.POST_COLLECTION_RECREATE, this);
    listenerRegistry.appendListeners(EventType.POST_COLLECTION_REMOVE, this);
    listenerRegistry.appendListeners(EventType.POST_COLLECTION_UPDATE, this);
    listenerRegistry.appendListeners(EventType.FLUSH, this);
    listenerRegistry.appendListeners(EventType.AUTO_FLUSH, this);
    listenerRegistry.appendListeners(EventType.CLEAR, this);
}
Also used : EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry)

Example 23 with EventListenerRegistry

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

the class ValidationManager method registerValidationListeners.

public void registerValidationListeners() {
    if (!isInitialized) {
        if (sessionFactory != null && sessionFactory instanceof SessionFactoryImpl) {
            ServiceRegistryImplementor serviceRegistry = ((SessionFactoryImpl) sessionFactory).getServiceRegistry();
            final EventListenerRegistry eventRegistry = serviceRegistry.getService(EventListenerRegistry.class);
            // duplication strategy
            // eventRegistry.addDuplicationStrategy(CdmListenerDuplicationStrategy.NewInstance);
            eventRegistry.getEventListenerGroup(EventType.POST_INSERT);
            ValidationExecutor validationExecutor = new ValidationExecutor();
            // level2
            l2Listener = new Level2ValidationEventListener(validationDao);
            l2Listener.setValidationExecutor(validationExecutor);
            // level3
            l3Listener = new Level3TransactionalValidationEventListener(cdmRepository, validationDao);
            l3Listener.setValidationExecutor(validationExecutor);
            // prepend to register before or append to register after
            eventRegistry.appendListeners(EventType.POST_INSERT, l2Listener, l3Listener);
            eventRegistry.appendListeners(EventType.POST_UPDATE, l2Listener, l3Listener);
            // TODO don't we need l2Listener validation also for deleting the results?
            eventRegistry.appendListeners(EventType.POST_DELETE, l3Listener);
            isInitialized = true;
        } else {
            throw new RuntimeException("Session factory not available or not of type SessionFactoryImpl");
        }
    }
}
Also used : ValidationExecutor(eu.etaxonomy.cdm.persistence.validation.ValidationExecutor) Level2ValidationEventListener(eu.etaxonomy.cdm.persistence.hibernate.Level2ValidationEventListener) ServiceRegistryImplementor(org.hibernate.service.spi.ServiceRegistryImplementor) SessionFactoryImpl(org.hibernate.internal.SessionFactoryImpl) EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry)

Example 24 with EventListenerRegistry

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

the class CdmListenerIntegrator method integrate.

@Override
public void integrate(Metadata metadata, SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) {
    if (logger.isInfoEnabled()) {
        logger.info("Registering event listeners");
    }
    final EventListenerRegistry eventRegistry = serviceRegistry.getService(EventListenerRegistry.class);
    // duplication strategy
    eventRegistry.addDuplicationStrategy(CdmListenerDuplicationStrategy.NewInstance);
    // ValidationExecutor validationExecutor = new ValidationExecutor();
    // Level2ValidationEventListener l2Listener = new Level2ValidationEventListener();
    // l2Listener.setValidationExecutor(validationExecutor);
    // Level3ValidationEventListener l3Listener = new Level3ValidationEventListener();
    // l3Listener.setValidationExecutor(validationExecutor);
    // prepend to register before or append to register after
    // this example will register a persist event listener
    // SaveEntityListener does not exist anymore
    eventRegistry.prependListeners(EventType.SAVE, new CacheStrategyGenerator());
    eventRegistry.prependListeners(EventType.UPDATE, new CacheStrategyGenerator(), new UpdateEntityListener());
    eventRegistry.prependListeners(EventType.SAVE_UPDATE, new CacheStrategyGenerator());
    eventRegistry.appendListeners(EventType.MERGE, new PostMergeEntityListener());
    // eventRegistry.appendListeners(EventType.DELETE, new CdmDeleteListener());
    eventRegistry.appendListeners(EventType.POST_LOAD, new CdmPostDataChangeObservableListener());
    // with validation
    // eventRegistry.appendListeners(EventType.POST_INSERT, new CdmPostDataChangeObservableListener(), l2Listener, l3Listener);
    // eventRegistry.appendListeners(EventType.POST_UPDATE, new CdmPostDataChangeObservableListener(), l2Listener, l3Listener);
    // eventRegistry.appendListeners(EventType.POST_DELETE, new CdmPostDataChangeObservableListener(), l3Listener);
    // without validation
    eventRegistry.appendListeners(EventType.POST_INSERT, new CdmPostDataChangeObservableListener());
    eventRegistry.appendListeners(EventType.POST_UPDATE, new CdmPostDataChangeObservableListener());
    eventRegistry.appendListeners(EventType.POST_DELETE, new CdmPostDataChangeObservableListener());
    eventRegistry.appendListeners(EventType.PRE_INSERT, new CdmPreDataChangeListener());
    eventRegistry.appendListeners(EventType.PRE_UPDATE, new CdmPreDataChangeListener());
}
Also used : EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry)

Example 25 with EventListenerRegistry

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

the class TaxonGraphHibernateListenerTest method registerListener.

@Before
public void registerListener() throws NoSuchMethodException, SecurityException {
    if (!TaxonGraphHibernateListenerTest.isRegistered) {
        EventListenerRegistry listenerRegistry = ((SessionFactoryImpl) sessionFactory).getServiceRegistry().getService(EventListenerRegistry.class);
        listenerRegistry.appendListeners(EventType.POST_UPDATE, taxonGraphHibernateListener);
        listenerRegistry.appendListeners(EventType.POST_INSERT, taxonGraphHibernateListener);
        listenerRegistry.appendListeners(EventType.PRE_DELETE, taxonGraphHibernateListener);
        TaxonGraphHibernateListenerTest.isRegistered = true;
    }
    // 
    taxonGraphHibernateListener.registerProcessClass(TaxonGraphBeforeTransactionCompleteProcess.class, new Object[] { new RunAsAdmin(runAsAuthenticationProvider) }, new Class[] { IRunAs.class });
}
Also used : RunAsAdmin(eu.etaxonomy.cdm.api.application.RunAsAdmin) EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry) Before(org.junit.Before)

Aggregations

EventListenerRegistry (org.hibernate.event.service.spi.EventListenerRegistry)52 Test (org.junit.Test)10 EventType (org.hibernate.event.spi.EventType)9 SessionFactoryImpl (org.hibernate.internal.SessionFactoryImpl)9 PreInsertEventListener (org.hibernate.event.spi.PreInsertEventListener)8 TestForIssue (org.hibernate.testing.TestForIssue)8 HashMap (java.util.HashMap)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 PostConstruct (javax.annotation.PostConstruct)5 HibernateException (org.hibernate.HibernateException)5 Session (org.hibernate.Session)5 EntityManager (javax.persistence.EntityManager)4 EntityActionVetoException (org.hibernate.action.internal.EntityActionVetoException)4 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)4 PreInsertEvent (org.hibernate.event.spi.PreInsertEvent)4 DialectChecks (org.hibernate.testing.DialectChecks)4