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 ); }
/** * Register an hibernate event listener. Listener will be created and injected by Guice. * * @param type Event type. * @param listenerType Listener type. * @return This module. */ @SuppressWarnings("unchecked") public <T> Hbm onEvent(final EventType<T> type, final Class<? extends T> listenerType) { bindings.add(b -> b.bind(listenerType).asEagerSingleton()); listeners.add((s, r) -> { ServiceRegistryImplementor serviceRegistry = s.getServiceRegistry(); EventListenerRegistry service = serviceRegistry.getService(EventListenerRegistry.class); T listener = r.require(listenerType); service.appendListeners(type, listener); }); return this; }
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 )
@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 ); // de-activate not-null tracking at the core level when Bean Validation is present unless the user explicitly // 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 ); }
@Override public void integrate( Metadata metadata, SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) { // As you might expect, an EventListenerRegistry is the thing with which event // listeners are registered // It is a service so we look it up using the service registry final EventListenerRegistry eventListenerRegistry = serviceRegistry.getService( EventListenerRegistry.class ); // If you wish to have custom determination and handling of "duplicate" listeners, // you would have to add an implementation of the // org.hibernate.event.service.spi.DuplicationStrategy contract like this eventListenerRegistry.addDuplicationStrategy( new CustomDuplicationStrategy() ); // EventListenerRegistry defines 3 ways to register listeners: // 1) This form overrides any existing registrations with eventListenerRegistry.setListeners( EventType.AUTO_FLUSH, DefaultAutoFlushEventListener.class ); // 2) This form adds the specified listener(s) to the beginning of the listener chain eventListenerRegistry.prependListeners( EventType.PERSIST, DefaultPersistEventListener.class ); // 3) This form adds the specified listener(s) to the end of the listener chain eventListenerRegistry.appendListeners( EventType.MERGE, DefaultMergeEventListener.class ); }
private void registerHibernateSearchEventListener(FullTextIndexEventListener eventListener, SessionFactoryServiceRegistry serviceRegistry) { EventListenerRegistry listenerRegistry = serviceRegistry.getService( EventListenerRegistry.class ); listenerRegistry.addDuplicationStrategy( new KeepIfSameClassDuplicationStrategy( FullTextIndexEventListener.class ) ); listenerRegistry.appendListeners( EventType.POST_INSERT, eventListener ); listenerRegistry.appendListeners( EventType.POST_UPDATE, eventListener ); listenerRegistry.appendListeners( EventType.POST_DELETE, eventListener ); listenerRegistry.appendListeners( EventType.POST_COLLECTION_RECREATE, eventListener ); listenerRegistry.appendListeners( EventType.POST_COLLECTION_REMOVE, eventListener ); listenerRegistry.appendListeners( EventType.POST_COLLECTION_UPDATE, eventListener ); listenerRegistry.appendListeners( EventType.FLUSH, eventListener ); }
private void registerHibernateSearchEventListener(FullTextIndexEventListener eventListener, SessionFactoryServiceRegistry serviceRegistry) { EventListenerRegistry listenerRegistry = serviceRegistry.getService( EventListenerRegistry.class ); listenerRegistry.addDuplicationStrategy( new DuplicationStrategyImpl( FullTextIndexEventListener.class ) ); listenerRegistry.appendListeners( EventType.POST_INSERT, eventListener ); listenerRegistry.appendListeners( EventType.POST_UPDATE, eventListener ); listenerRegistry.appendListeners( EventType.POST_DELETE, eventListener ); listenerRegistry.appendListeners( EventType.POST_COLLECTION_RECREATE, eventListener ); listenerRegistry.appendListeners( EventType.POST_COLLECTION_REMOVE, eventListener ); listenerRegistry.appendListeners( EventType.POST_COLLECTION_UPDATE, eventListener ); listenerRegistry.appendListeners( EventType.FLUSH, eventListener ); }
public SessionFactory newSessionFactory(Configuration effectiveHibernateConfiguration) { SessionFactory hibernateSessionFactory = TopiaUtil.newSessionFactory(effectiveHibernateConfiguration); EventListenerRegistry eventListenerRegistry = getHibernateService(hibernateSessionFactory, EventListenerRegistry.class); TopiaHibernateEventListener listener = new TopiaHibernateEventListener(sessionRegistry); eventListenerRegistry.appendListeners(EventType.PRE_INSERT, listener); eventListenerRegistry.appendListeners(EventType.PRE_LOAD, listener); eventListenerRegistry.appendListeners(EventType.PRE_UPDATE, listener); eventListenerRegistry.appendListeners(EventType.PRE_DELETE, listener); eventListenerRegistry.appendListeners(EventType.POST_INSERT, listener); eventListenerRegistry.appendListeners(EventType.POST_LOAD, listener); eventListenerRegistry.appendListeners(EventType.POST_UPDATE, listener); eventListenerRegistry.appendListeners(EventType.POST_DELETE, listener); // following listeners must be called before hibernate eventListenerRegistry.prependListeners(EventType.SAVE_UPDATE, listener); return hibernateSessionFactory; }
@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 ); }
@Component public class CustomHibernateListenerIntegrator extends EnversIntegrator { @Override public void integrate(Configuration configuration, SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) { EventListenerRegistry listenerRegistry = serviceRegistry.getService(EventListenerRegistry.class); listenerRegistry.addDuplicationStrategy(EnversListenerDuplicationStrategy.INSTANCE); final AuditConfiguration enversConfiguration = AuditConfiguration.getFor(configuration); if (enversConfiguration.getEntCfg().hasAuditedEntities()) { listenerRegistry.appendListeners(EventType.POST_DELETE, new DeleteEnversListener(enversConfiguration)); listenerRegistry.appendListeners(EventType.PRE_UPDATE, new UpdateEnversListener(enversConfiguration)); listenerRegistry.appendListeners(EventType.LOAD, new ProductLoadEventListener()); } } }
Configuration configuration = new Configuration().configure(); SessionFactoryImpl sessionFactory = (SessionFactoryImpl) configuration.buildSessionFactory(); EventListenerRegistry listenerRegistry = sessionFactory.getServiceRegistry().getService(EventListenerRegistry.class); SolrIndexEventListener indexListener = new SolrIndexEventListener(); // a SaveOrUpdateEventListener i wanted to attach listenerRegistry.appendListeners(EventType.SAVE_UPDATE, indexListener);
@Override public void integrate( Metadata metadata, SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) { final EventListenerRegistry eventListenerRegistry = serviceRegistry.getService(EventListenerRegistry.class); eventListenerRegistry.appendListeners(EventType.POST_INSERT, ReplicationInsertEventListener.INSTANCE); eventListenerRegistry.appendListeners(EventType.POST_UPDATE, ReplicationUpdateEventListener.INSTANCE); eventListenerRegistry.appendListeners(EventType.PRE_DELETE, ReplicationDeleteEventListener.INSTANCE); }
@SuppressWarnings( {"UnusedDeclaration"}) public static void activateBeanValidation(EventListenerRegistry listenerRegistry, Configuration configuration) { final Properties properties = configuration.getProperties(); ValidatorFactory factory = getValidatorFactory( properties ); BeanValidationEventListener listener = new BeanValidationEventListener( factory, properties ); listenerRegistry.addDuplicationStrategy( DuplicationStrategyImpl.INSTANCE ); listenerRegistry.appendListeners( EventType.PRE_INSERT, listener ); listenerRegistry.appendListeners( EventType.PRE_UPDATE, listener ); listenerRegistry.appendListeners( EventType.PRE_DELETE, listener ); listener.initialize( configuration ); }
@Override public void integrate(Metadata metadata, SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) { EventListenerRegistry listenerRegistry = serviceRegistry.getService( EventListenerRegistry.class ); listenerRegistry.addDuplicationStrategy( EnversListenerDuplicationStrategy.INSTANCE ); EnversService enversService = serviceRegistry.getService(EnversService.class); if (!enversService.isInitialized()) { throw new HibernateException("Expecting EnversService to have been initialized prior to call to EnversIntegrator#integrate"); } if(enversService.getEntitiesConfigurations().hasAuditedEntities()) { listenerRegistry.appendListeners( EventType.POST_UPDATE, new PostUpdateListenerLog( enversService ) ); listenerRegistry.appendListeners( EventType.POST_INSERT, new PostInsertListenerLog( enversService ) ); listenerRegistry.appendListeners( EventType.POST_DELETE, new PostDeleteListenerLog( enversService ) ); } }
@SuppressWarnings( {"UnusedDeclaration"}) public static void activateBeanValidation(EventListenerRegistry listenerRegistry, Configuration configuration) { final Properties properties = configuration.getProperties(); ValidatorFactory factory = getValidatorFactory( properties ); BeanValidationEventListener listener = new BeanValidationEventListener( factory, properties ); listenerRegistry.addDuplicationStrategy( DuplicationStrategyImpl.INSTANCE ); listenerRegistry.appendListeners( EventType.PRE_INSERT, listener ); listenerRegistry.appendListeners( EventType.PRE_UPDATE, listener ); listenerRegistry.appendListeners( EventType.PRE_DELETE, listener ); listener.initialize( configuration ); }
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 ); }
@Override public void integrate( Metadata metadata, SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) { final EventListenerRegistry eventListenerRegistry = serviceRegistry.getService( EventListenerRegistry.class ); eventListenerRegistry.appendListeners(EventType.PERSIST, RootAwareInsertEventListener.INSTANCE); eventListenerRegistry.appendListeners(EventType.FLUSH_ENTITY, RootAwareUpdateAndDeleteEventListener.INSTANCE); }
@Override public void integrate( Metadata metadata, SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) { final EventListenerRegistry eventListenerRegistry = serviceRegistry.getService( EventListenerRegistry.class ); eventListenerRegistry.appendListeners( EventType.PERSIST, RootAwareInsertEventListener.INSTANCE); eventListenerRegistry.appendListeners( EventType.FLUSH_ENTITY, RootAwareUpdateAndDeleteEventListener.INSTANCE); }
@Override public void integrate( Metadata metadata, SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry ) { final EventListenerRegistry registry = serviceRegistry.getService( EventListenerRegistry.class ); DeletedObjectPostDeleteEventListener listener = new DeletedObjectPostDeleteEventListener(); registry.appendListeners( EventType.POST_DELETE, listener ); }
public class MyIntegrator implements org.hibernate.integrator.spi.Integrator { public void integrate(Configuration configuration, SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) { // As you might expect, an EventListenerRegistry is the thing with which event listeners are registered // It is a service so we look it up using the service registry final EventListenerRegistry eventListenerRegistry = serviceRegistry.getService( EventListenerRegistry.class ); // If you wish to have custom determination and handling of "duplicate" listeners, you would have to add an // implementation of the org.hibernate.event.service.spi.DuplicationStrategy contract like this eventListenerRegistry.addDuplicationStrategy( myDuplicationStrategy ); // EventListenerRegistry defines 3 ways to register listeners: // 1) This form overrides any existing registrations with eventListenerRegistry.setListeners( EventType.AUTO_FLUSH, myCompleteSetOfListeners ); // 2) This form adds the specified listener(s) to the beginning of the listener chain eventListenerRegistry.prependListeners( EventType.AUTO_FLUSH, myListenersToBeCalledFirst ); // 3) This form adds the specified listener(s) to the end of the listener chain eventListenerRegistry.appendListeners( EventType.AUTO_FLUSH, myListenersToBeCalledLast ); } }