private boolean preUpdate() { boolean veto = false; final EventListenerGroup<PreUpdateEventListener> listenerGroup = listenerGroup( EventType.PRE_UPDATE ); if ( listenerGroup.isEmpty() ) { return veto; } final PreUpdateEvent event = new PreUpdateEvent( getInstance(), getId(), state, previousState, getPersister(), eventSource() ); for ( PreUpdateEventListener listener : listenerGroup.listeners() ) { veto |= listener.onPreUpdate( event ); } return veto; }
/** * PostLoad cannot occur during initializeEntity, as that call occurs *before* * the Set collections are added to the persistence context by Loader. * Without the split, LazyInitializationExceptions can occur in the Entity's * postLoad if it acts upon the collection. * * HHH-6043 * * @param entity The entity * @param session The Session * @param postLoadEvent The (re-used) post-load event */ public static void postLoad( final Object entity, final SessionImplementor session, final PostLoadEvent postLoadEvent) { if ( session.isEventSource() ) { final PersistenceContext persistenceContext = session.getPersistenceContext(); final EntityEntry entityEntry = persistenceContext.getEntry( entity ); postLoadEvent.setEntity( entity ).setId( entityEntry.getId() ).setPersister( entityEntry.getPersister() ); final EventListenerGroup<PostLoadEventListener> listenerGroup = session.getFactory() .getServiceRegistry() .getService( EventListenerRegistry.class ) .getEventListenerGroup( EventType.POST_LOAD ); for ( PostLoadEventListener listener : listenerGroup.listeners() ) { listener.onPostLoad( postLoadEvent ); } } }
@Override public boolean isDirty() throws HibernateException { errorIfClosed(); checkTransactionSynchStatus(); LOG.debug( "Checking session dirtiness" ); if ( actionQueue.areInsertionsOrDeletionsQueued() ) { LOG.debug( "Session dirty (scheduled updates and insertions)" ); return true; } DirtyCheckEvent event = new DirtyCheckEvent( this ); for ( DirtyCheckEventListener listener : listeners( EventType.DIRTY_CHECK ) ) { listener.onDirtyCheck( event ); } delayedAfterCompletion(); return event.isDirty(); }
public EventListenerGroupImpl(EventType<T> eventType) { this.eventType = eventType; duplicationStrategies.add( // At minimum make sure we do not register the same exact listener class multiple times. new DuplicationStrategy() { @Override public boolean areMatch(Object listener, Object original) { return listener.getClass().equals( original.getClass() ); } @Override public Action getAction() { return Action.ERROR; } } ); }
private static <T> void prepareListeners(EventType<T> type, T defaultListener, Map<EventType,EventListenerGroupImpl> map) { final EventListenerGroupImpl<T> listenerGroup; if ( type == EventType.POST_COMMIT_DELETE || type == EventType.POST_COMMIT_INSERT || type == EventType.POST_COMMIT_UPDATE ) { listenerGroup = new PostCommitEventListenerGroupImpl<T>( type ); } else { listenerGroup = new EventListenerGroupImpl<T>( type ); } if ( defaultListener != null ) { listenerGroup.appendListener( defaultListener ); } map.put( type, listenerGroup ); }
public PostCommitEventListenerGroupImpl(EventType<T> eventType) { super( eventType ); if ( eventType == EventType.POST_COMMIT_DELETE ) { this.extendedListenerContract = PostCommitDeleteEventListener.class; } else if ( eventType == EventType.POST_COMMIT_INSERT ) { this.extendedListenerContract = PostCommitInsertEventListener.class; } else if ( eventType == EventType.POST_COMMIT_UPDATE ) { this.extendedListenerContract = PostCommitUpdateEventListener.class; } else { throw new IllegalStateException( "Unexpected usage of PostCommitEventListenerGroupImpl" ); } }
private void postDelete() { final EventListenerGroup<PostDeleteEventListener> listenerGroup = listenerGroup( EventType.POST_DELETE ); if ( listenerGroup.isEmpty() ) { return; } final PostDeleteEvent event = new PostDeleteEvent( getInstance(), getId(), state, getPersister(), eventSource() ); for ( PostDeleteEventListener listener : listenerGroup.listeners() ) { listener.onPostDelete( event ); } }
private void postInsert() { final EventListenerGroup<PostInsertEventListener> listenerGroup = listenerGroup( EventType.POST_INSERT ); if ( listenerGroup.isEmpty() ) { return; } final PostInsertEvent event = new PostInsertEvent( getInstance(), getId(), getState(), getPersister(), eventSource() ); for ( PostInsertEventListener listener : listenerGroup.listeners() ) { listener.onPostInsert( event ); } }
private void postInsert() { if ( isDelayed ) { getSession().getPersistenceContext().replaceDelayedEntityIdentityInsertKeys( delayedEntityKey, generatedId ); } final EventListenerGroup<PostInsertEventListener> listenerGroup = listenerGroup( EventType.POST_INSERT ); if ( listenerGroup.isEmpty() ) { return; } final PostInsertEvent event = new PostInsertEvent( getInstance(), generatedId, getState(), getPersister(), eventSource() ); for ( PostInsertEventListener listener : listenerGroup.listeners() ) { listener.onPostInsert( event ); } }
private void postUpdate() { final EventListenerGroup<PostUpdateEventListener> listenerGroup = listenerGroup( EventType.POST_UPDATE ); if ( listenerGroup.isEmpty() ) { return; } final PostUpdateEvent event = new PostUpdateEvent( getInstance(), getId(), state, previousState, dirtyFields, getPersister(), eventSource() ); for ( PostUpdateEventListener listener : listenerGroup.listeners() ) { listener.onPostUpdate( event ); } }
@PostConstruct public void registerListeners() { // TODO: This method of getting a reference to the SessionFactory (and thereforce the ServiceRegistry) is Deprecated. Find out the right Hibernate 5.2 way to do this. EventListenerRegistry registry = entityManagerFactory.getSessionFactory().getServiceRegistry().getService(EventListenerRegistry.class); if (preInsertEventListeners != null) { registry.appendListeners(EventType.PRE_INSERT, preInsertEventListeners.toArray(new PreInsertEventListener[preInsertEventListeners.size()])); } if (postInsertEventListeners != null) { registry.appendListeners(EventType.POST_INSERT, postInsertEventListeners.toArray(new PostInsertEventListener[postInsertEventListeners.size()])); } if (preUpdateEventListeners != null) { registry.appendListeners(EventType.PRE_UPDATE, preUpdateEventListeners.toArray(new PreUpdateEventListener[preUpdateEventListeners.size()])); } if (postUpdateEventListeners != null) { registry.appendListeners(EventType.POST_UPDATE, postUpdateEventListeners.toArray(new PostUpdateEventListener[postUpdateEventListeners.size()])); } if (preDeleteEventListeners != null) { registry.appendListeners(EventType.PRE_DELETE, preDeleteEventListeners.toArray(new PreDeleteEventListener[preDeleteEventListeners.size()])); } if (postDeleteEventListeners != null) { registry.appendListeners(EventType.POST_DELETE, postDeleteEventListeners.toArray(new PostDeleteEventListener[postDeleteEventListeners.size()])); } }
@Override public void integrate(Metadata metadata, SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) { EventListenerRegistry service = serviceRegistry.getService(org.hibernate.event.service.spi.EventListenerRegistry.class); StandardPBEStringEncryptor encrypt = new StandardPBEStringEncryptor(); encrypt.setPassword("test_password"); RenderedMessageEncryptEventListener encryptListener = new RenderedMessageEncryptEventListener(); encryptListener.setStringEncryptor(encrypt); RenderedMessageDecryptEventListener decryptListener = new RenderedMessageDecryptEventListener(); decryptListener.setStringEncryptor(encrypt); FullTextIndexEventListener fullTextListener = new FullTextIndexEventListener(); service.appendListeners(EventType.PRE_UPDATE, encryptListener); service.prependListeners(EventType.POST_UPDATE, decryptListener); service.appendListeners(EventType.PRE_INSERT, encryptListener); service.prependListeners(EventType.POST_INSERT, decryptListener); service.appendListeners(EventType.POST_LOAD, decryptListener); }
public void integrate(Configuration configuration, SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) { System.out.println("Integrating......"); //final AuditService auditService = serviceRegistry.getService(AuditService.class); final AuditService auditService = new AuditServiceImpl(); auditService.init(); if (!auditService.isInitialized()) { throw new InitializationException( "Audit4j hibernate integration can not be initialized.."); } // Register listeners.. final EventListenerRegistry listenerRegistry = serviceRegistry .getService(EventListenerRegistry.class); listenerRegistry.appendListeners(EventType.POST_INSERT, new AuditPostInsertEventListenerImpl(auditService)); listenerRegistry.appendListeners(EventType.POST_UPDATE, new AuditPostUpdateEventListenerImpl(auditService)); listenerRegistry.appendListeners(EventType.POST_DELETE, new AuditPostDeleteEventListenerImpl(auditService)); }
protected <T> void appendListeners(EventListenerRegistry listenerRegistry, EventType<T> eventType, Collection<T> listeners) { EventListenerGroup<T> group = listenerRegistry.getEventListenerGroup(eventType); for (T listener : listeners) { if (listener != null) { if(shouldOverrideListeners(eventType, listener)) { // since ClosureEventTriggeringInterceptor extends DefaultSaveOrUpdateEventListener we want to override instead of append the listener here // to avoid there being 2 implementations which would impact performance too group.clear(); group.appendListener(listener); } else { group.appendListener(listener); } } } }
@SuppressWarnings("unchecked") protected <T> void appendListeners(final EventListenerRegistry listenerRegistry, final EventType<T> eventType, final Map<String, Object> listeners) { Object listener = listeners.get(eventType.eventName()); if (listener != null) { if(shouldOverrideListeners(eventType, listener)) { // since ClosureEventTriggeringInterceptor extends DefaultSaveOrUpdateEventListener we want to override instead of append the listener here // to avoid there being 2 implementations which would impact performance too listenerRegistry.setListeners(eventType, (T) listener); } else { listenerRegistry.appendListeners(eventType, (T)listener); } } }
@Override public void integrate(org.hibernate.cfg.Configuration configuration, org.hibernate.engine.spi.SessionFactoryImplementor sessionFactory, org.hibernate.service.spi.SessionFactoryServiceRegistry serviceRegistry) { final EventListenerRegistry eventRegistry = serviceRegistry.getService(EventListenerRegistry.class); eventRegistry.prependListeners(EventType.POST_COMMIT_INSERT, listener); final AuditConfiguration enversConfiguration = AuditConfiguration.getFor( configuration ); AuditStrategy auditStrategy = enversConfiguration.getAuditStrategy(); if (enversConfiguration.getEntCfg().hasAuditedEntities()) { listenerRegistry.appendListeners( EventType.POST_DELETE, new EnversPostDeleteEventListenerImpl( enversConfiguration ) ); listenerRegistry.appendListeners( EventType.POST_INSERT, new EnversPostInsertEventListenerImpl( enversConfiguration ) ); listenerRegistry.appendListeners( EventType.POST_UPDATE, new EnversPostUpdateEventListenerImpl( enversConfiguration ) ); listenerRegistry.appendListeners( EventType.POST_COLLECTION_RECREATE, new EnversPostCollectionRecreateEventListenerImpl( enversConfiguration ) ); listenerRegistry.appendListeners( EventType.PRE_COLLECTION_REMOVE, new EnversPreCollectionRemoveEventListenerImpl( enversConfiguration ) ); listenerRegistry.appendListeners( EventType.PRE_COLLECTION_UPDATE, new EnversPreCollectionUpdateEventListenerImpl( enversConfiguration ) ); } }
/** * Register event listener. * * @param sessionFactory the session factory * @param listener the listener * @param eventTypes the event types */ @SuppressWarnings("unchecked") public static void registerEventListener(SessionFactory sessionFactory, Object listener, EventType... eventTypes) { shouldNotBeNull(sessionFactory, "sessionFactory"); shouldNotBeNull(listener, "listener"); log.trace("sessionFactory에 event listener를 등록합니다... listener=[{}], eventTypes=[{}]", listener, StringTool.listToString(eventTypes)); EventListenerRegistry registry = ((SessionFactoryImpl) sessionFactory) .getServiceRegistry() .getService(EventListenerRegistry.class); for (EventType eventType : eventTypes) { try { registry.getEventListenerGroup(eventType).appendListener(listener); } catch (Exception ignored) {} } }
@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; } // de-activate not-null tracking at the core level when Bean Validation is present unless the user explicitly // asks for it if ( activationContext.getConfiguration().getProperty( Environment.CHECK_NULLABILITY ) == null ) { activationContext.getSessionFactory().getSettings().setCheckNullability( false ); } final BeanValidationEventListener listener = new BeanValidationEventListener( validatorFactory, activationContext.getConfiguration().getProperties() ); 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( activationContext.getConfiguration() ); }
private static Iterable<PersistEventListener> persistEventListeners(SessionImplementor session) { return session .getFactory() .getServiceRegistry() .getService( EventListenerRegistry.class ) .getEventListenerGroup( EventType.PERSIST ) .listeners(); }
private void internalClear() { persistenceContext.clear(); actionQueue.clear(); final ClearEvent event = new ClearEvent( this ); for ( ClearEventListener listener : listeners( EventType.CLEAR ) ) { listener.onClear( event ); } }
private void fireSaveOrUpdate(SaveOrUpdateEvent event) { errorIfClosed(); checkTransactionSynchStatus(); checkNoUnresolvedActionsBeforeOperation(); for ( SaveOrUpdateEventListener listener : listeners( EventType.SAVE_UPDATE ) ) { listener.onSaveOrUpdate( event ); } checkNoUnresolvedActionsAfterOperation(); }
private Serializable fireSave(SaveOrUpdateEvent event) { errorIfClosed(); checkTransactionSynchStatus(); checkNoUnresolvedActionsBeforeOperation(); for ( SaveOrUpdateEventListener listener : listeners( EventType.SAVE ) ) { listener.onSaveOrUpdate( event ); } checkNoUnresolvedActionsAfterOperation(); return event.getResultId(); }
private void fireUpdate(SaveOrUpdateEvent event) { errorIfClosed(); checkTransactionSynchStatus(); checkNoUnresolvedActionsBeforeOperation(); for ( SaveOrUpdateEventListener listener : listeners( EventType.UPDATE ) ) { listener.onSaveOrUpdate( event ); } checkNoUnresolvedActionsAfterOperation(); }
private void fireLock(LockEvent event) { errorIfClosed(); checkTransactionSynchStatus(); for ( LockEventListener listener : listeners( EventType.LOCK ) ) { listener.onLock( event ); } delayedAfterCompletion(); }
private void firePersist(Map copiedAlready, PersistEvent event) { errorIfClosed(); checkTransactionSynchStatus(); for ( PersistEventListener listener : listeners( EventType.PERSIST ) ) { listener.onPersist( event, copiedAlready ); } delayedAfterCompletion(); }
private void firePersist(PersistEvent event) { errorIfClosed(); checkTransactionSynchStatus(); checkNoUnresolvedActionsBeforeOperation(); for ( PersistEventListener listener : listeners( EventType.PERSIST ) ) { listener.onPersist( event ); } checkNoUnresolvedActionsAfterOperation(); }
private void firePersistOnFlush(Map copiedAlready, PersistEvent event) { errorIfClosed(); checkTransactionSynchStatus(); for ( PersistEventListener listener : listeners( EventType.PERSIST_ONFLUSH ) ) { listener.onPersist( event, copiedAlready ); } delayedAfterCompletion(); }
private void firePersistOnFlush(PersistEvent event) { errorIfClosed(); checkTransactionSynchStatus(); checkNoUnresolvedActionsBeforeOperation(); for ( PersistEventListener listener : listeners( EventType.PERSIST_ONFLUSH ) ) { listener.onPersist( event ); } checkNoUnresolvedActionsAfterOperation(); }
private Object fireMerge(MergeEvent event) { errorIfClosed(); checkTransactionSynchStatus(); checkNoUnresolvedActionsBeforeOperation(); for ( MergeEventListener listener : listeners( EventType.MERGE ) ) { listener.onMerge( event ); } checkNoUnresolvedActionsAfterOperation(); return event.getResult(); }
private void fireMerge(Map copiedAlready, MergeEvent event) { errorIfClosed(); checkTransactionSynchStatus(); for ( MergeEventListener listener : listeners( EventType.MERGE ) ) { listener.onMerge( event, copiedAlready ); } delayedAfterCompletion(); }
private void fireDelete(DeleteEvent event) { errorIfClosed(); checkTransactionSynchStatus(); for ( DeleteEventListener listener : listeners( EventType.DELETE ) ) { listener.onDelete( event ); } delayedAfterCompletion(); }
private void fireDelete(DeleteEvent event, Set transientEntities) { errorIfClosed(); checkTransactionSynchStatus(); for ( DeleteEventListener listener : listeners( EventType.DELETE ) ) { listener.onDelete( event, transientEntities ); } delayedAfterCompletion(); }
private void fireLoad(LoadEvent event, LoadType loadType) { errorIfClosed(); checkTransactionSynchStatus(); for ( LoadEventListener listener : listeners( EventType.LOAD ) ) { listener.onLoad( event, loadType ); } delayedAfterCompletion(); }
private void fireResolveNaturalId(ResolveNaturalIdEvent event) { errorIfClosed(); checkTransactionSynchStatus(); for ( ResolveNaturalIdEventListener listener : listeners( EventType.RESOLVE_NATURAL_ID ) ) { listener.onResolveNaturalId( event ); } delayedAfterCompletion(); }
private void fireRefresh(RefreshEvent event) { errorIfClosed(); checkTransactionSynchStatus(); for ( RefreshEventListener listener : listeners( EventType.REFRESH ) ) { listener.onRefresh( event ); } delayedAfterCompletion(); }
private void fireRefresh(Map refreshedAlready, RefreshEvent event) { errorIfClosed(); checkTransactionSynchStatus(); for ( RefreshEventListener listener : listeners( EventType.REFRESH ) ) { listener.onRefresh( event, refreshedAlready ); } delayedAfterCompletion(); }
private void fireReplicate(ReplicateEvent event) { errorIfClosed(); checkTransactionSynchStatus(); for ( ReplicateEventListener listener : listeners( EventType.REPLICATE ) ) { listener.onReplicate( event ); } delayedAfterCompletion(); }
private void fireEvict(EvictEvent event) { errorIfClosed(); checkTransactionSynchStatus(); for ( EvictEventListener listener : listeners( EventType.EVICT ) ) { listener.onEvict( event ); } delayedAfterCompletion(); }
/** * detect in-memory changes, determine if the changes are to tables * named in the query and, if so, complete execution the flush */ protected boolean autoFlushIfRequired(Set querySpaces) throws HibernateException { errorIfClosed(); if ( ! isTransactionInProgress() ) { // do not auto-flush while outside a transaction return false; } AutoFlushEvent event = new AutoFlushEvent( querySpaces, this ); for ( AutoFlushEventListener listener : listeners( EventType.AUTO_FLUSH ) ) { listener.onAutoFlush( event ); } return event.isFlushRequired(); }
@Override public void flush() throws HibernateException { errorIfClosed(); checkTransactionSynchStatus(); if ( persistenceContext.getCascadeLevel() > 0 ) { throw new HibernateException("Flush during cascade is dangerous"); } FlushEvent flushEvent = new FlushEvent( this ); for ( FlushEventListener listener : listeners( EventType.FLUSH ) ) { listener.onFlush( flushEvent ); } delayedAfterCompletion(); }