use of org.hibernate.event.spi.PostInsertEvent in project hibernate-orm by hibernate.
the class EntityIdentityInsertAction method postInsert.
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);
}
}
use of org.hibernate.event.spi.PostInsertEvent 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.spi.PostInsertEvent 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;
}
});
}
use of org.hibernate.event.spi.PostInsertEvent in project hibernate-orm by hibernate.
the class SessionWithSharedConnectionTest method testChildSessionCallsAfterTransactionAction.
@Test
@TestForIssue(jiraKey = "HHH-7239")
public void testChildSessionCallsAfterTransactionAction() throws Exception {
Session session = openSession();
final String postCommitMessage = "post commit was called";
EventListenerRegistry eventListenerRegistry = sessionFactory().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 requiresPostCommitHanding(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().flushBeforeCompletion(true).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.spi.PostInsertEvent in project hibernate-orm by hibernate.
the class EntityInsertAction method postInsert.
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);
}
}
Aggregations