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 requiresPostCommitHandling(EntityPersister persister) {
return false;
}
});
}
use of org.hibernate.event.service.spi.EventListenerRegistry in project hibernate-orm by hibernate.
the class SessionWithSharedConnectionTest method testChildSessionCallsAfterTransactionAction.
@Test
@TestForIssue(jiraKey = "HHH-7239")
public void testChildSessionCallsAfterTransactionAction(SessionFactoryScope scope) throws Exception {
Session session = scope.getSessionFactory().openSession();
final String postCommitMessage = "post commit was called";
EventListenerRegistry eventListenerRegistry = scope.getSessionFactory().getServiceRegistry().getService(EventListenerRegistry.class);
// register a post commit listener
eventListenerRegistry.appendListeners(EventType.POST_COMMIT_INSERT, new PostInsertEventListener() {
@Override
public void onPostInsert(PostInsertEvent event) {
((IrrelevantEntity) event.getEntity()).setName(postCommitMessage);
}
@Override
public boolean requiresPostCommitHandling(EntityPersister persister) {
return true;
}
});
session.getTransaction().begin();
IrrelevantEntity irrelevantEntityMainSession = new IrrelevantEntity();
irrelevantEntityMainSession.setName("main session");
session.save(irrelevantEntityMainSession);
// open secondary session to also insert an entity
Session secondSession = session.sessionWithOptions().connection().autoClose(true).openSession();
IrrelevantEntity irrelevantEntitySecondarySession = new IrrelevantEntity();
irrelevantEntitySecondarySession.setName("secondary session");
secondSession.save(irrelevantEntitySecondarySession);
session.getTransaction().commit();
// both entities should have their names updated to the postCommitMessage value
assertEquals(postCommitMessage, irrelevantEntityMainSession.getName());
assertEquals(postCommitMessage, irrelevantEntitySecondarySession.getName());
}
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) {
final SessionFactoryOptions sessionFactoryOptions = sessionFactory.getSessionFactoryOptions();
if (!sessionFactoryOptions.isAutoEvictCollectionCache()) {
// feature is disabled
return;
}
if (!sessionFactoryOptions.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 PackagedEntityManagerTest method testListeners.
@Test
public void testListeners() throws Exception {
File testPackage = buildExplicitPar();
addPackageToClasspath(testPackage);
emf = Persistence.createEntityManagerFactory("manager1", new HashMap());
EntityManager em = emf.createEntityManager();
try {
EventListenerRegistry listenerRegistry = em.unwrap(SharedSessionContractImplementor.class).getFactory().getServiceRegistry().getService(EventListenerRegistry.class);
assertEquals(listenerRegistry.getEventListenerGroup(EventType.PRE_INSERT).count(), listenerRegistry.getEventListenerGroup(EventType.PRE_UPDATE).count() + 1, "Explicit pre-insert event through hibernate.ejb.event.pre-insert does not work");
} finally {
em.close();
}
}
use of org.hibernate.event.service.spi.EventListenerRegistry in project openmrs-core by openmrs.
the class ObsPostLoadEventListener method registerListener.
@PostConstruct
public void registerListener() {
EventListenerRegistry registry = ((SessionFactoryImpl) sessionFactory).getServiceRegistry().getService(EventListenerRegistry.class);
registry.getEventListenerGroup(EventType.POST_LOAD).appendListener(this);
}
Aggregations