use of org.hibernate.event.service.spi.EventListenerRegistry in project hibernate-orm by hibernate.
the class EnversIntegrator method integrate.
public void integrate(Metadata metadata, SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) {
final EnversService enversService = serviceRegistry.getService(EnversService.class);
// Opt-out of registration if EnversService is disabled
if (!enversService.isEnabled()) {
log.debug("Skipping Envers listener registrations : EnversService disabled");
return;
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Opt-out of registration if asked to not register
final boolean autoRegister = serviceRegistry.getService(ConfigurationService.class).getSetting(AUTO_REGISTER, StandardConverters.BOOLEAN, true);
if (!autoRegister) {
log.debug("Skipping Envers listener registrations : Listener auto-registration disabled");
return;
}
// Verify that the EnversService is fully initialized and ready to go.
if (!enversService.isInitialized()) {
throw new HibernateException("Expecting EnversService to have been initialized prior to call to EnversIntegrator#integrate");
}
// Opt-out of registration if no audited entities found
if (!enversService.getEntitiesConfigurations().hasAuditedEntities()) {
log.debug("Skipping Envers listener registrations : No audited entities found");
return;
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Do the registrations
final EventListenerRegistry listenerRegistry = serviceRegistry.getService(EventListenerRegistry.class);
listenerRegistry.addDuplicationStrategy(EnversListenerDuplicationStrategy.INSTANCE);
if (enversService.getEntitiesConfigurations().hasAuditedEntities()) {
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));
}
}
use of org.hibernate.event.service.spi.EventListenerRegistry in project hibernate-orm by hibernate.
the class CollectionCacheInvalidator method integrate.
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);
}
use of org.hibernate.event.service.spi.EventListenerRegistry in project hibernate-orm by hibernate.
the class LazyProxyOnEnhancedEntityTestTask method execute.
public void execute() {
EventListenerRegistry registry = getFactory().unwrap(SessionFactoryImplementor.class).getServiceRegistry().getService(EventListenerRegistry.class);
registry.prependListeners(EventType.FLUSH, new JpaFlushEventListener());
registry.prependListeners(EventType.LOAD, new ImmediateLoadTrap());
EntityManager em = getFactory().createEntityManager();
em.getTransaction().begin();
Parent p = em.find(Parent.class, parentID);
// unwanted lazy load occurs here
em.flush();
em.getTransaction().commit();
em.close();
}
use of org.hibernate.event.service.spi.EventListenerRegistry in project hibernate-orm by hibernate.
the class MergeListPreAndPostPersistTest method addEntityListeners.
private void addEntityListeners(final Order order) {
EventListenerRegistry registry = sessionFactory().getServiceRegistry().getService(EventListenerRegistry.class);
registry.setListeners(EventType.PRE_INSERT, new PreInsertEventListener() {
@Override
public boolean onPreInsert(PreInsertEvent event) {
if (Order.class.isInstance(event.getEntity())) {
assertEquals(order, event.getEntity());
assertEquals(order.items, ((Order) event.getEntity()).items);
}
return false;
}
});
registry.setListeners(EventType.POST_INSERT, new PostInsertEventListener() {
public void onPostInsert(PostInsertEvent event) {
if (Order.class.isInstance(event.getEntity())) {
assertEquals(order, event.getEntity());
assertEquals(order.items, ((Order) event.getEntity()).items);
}
}
public boolean requiresPostCommitHanding(EntityPersister persister) {
return false;
}
});
}
use of org.hibernate.event.service.spi.EventListenerRegistry in project hibernate-orm by hibernate.
the class MergeListPreAndPostPersistWithIdentityTest method addEntityListeners.
private void addEntityListeners(final Order order) {
EventListenerRegistry registry = sessionFactory().getServiceRegistry().getService(EventListenerRegistry.class);
registry.setListeners(EventType.PRE_INSERT, new PreInsertEventListener() {
@Override
public boolean onPreInsert(PreInsertEvent event) {
if (Order.class.isInstance(event.getEntity())) {
assertEquals(order, event.getEntity());
assertEquals(order.items, ((Order) event.getEntity()).items);
}
return false;
}
});
registry.setListeners(EventType.POST_INSERT, new PostInsertEventListener() {
public void onPostInsert(PostInsertEvent event) {
if (Order.class.isInstance(event.getEntity())) {
assertEquals(order, event.getEntity());
assertEquals(order.items, ((Order) event.getEntity()).items);
}
}
public boolean requiresPostCommitHanding(EntityPersister persister) {
return false;
}
});
}
Aggregations