Search in sources :

Example 1 with IrrelevantEntity

use of org.hibernate.IrrelevantEntity in project hibernate-orm by hibernate.

the class SessionWithSharedConnectionTest method testSharedTransactionContextFlushBeforeCompletion.

//	@Test
//	@TestForIssue( jiraKey = "HHH-7090" )
//	public void testSharedTransactionContextAutoJoining() {
//		Session session = sessionFactory().openSession();
//		session.getTransaction().begin();
//
//		Session secondSession = session.sessionWithOptions()
//				.transactionContext()
//				.autoJoinTransactions( true )
//				.openSession();
//
//		// directly assert state of the second session
//		assertFalse( ((SessionImplementor) secondSession).shouldAutoJoinTransaction() );
//
//		secondSession.close();
//		session.close();
//	}
@Test
@TestForIssue(jiraKey = "HHH-7090")
public void testSharedTransactionContextFlushBeforeCompletion() {
    Session session = sessionFactory().openSession();
    session.getTransaction().begin();
    Session secondSession = session.sessionWithOptions().transactionContext().flushBeforeCompletion(true).autoClose(true).openSession();
    // directly assert state of the second session
    //		assertTrue( ((SessionImplementor) secondSession).isFlushBeforeCompletionEnabled() );
    // now try it out
    Integer id = (Integer) secondSession.save(new IrrelevantEntity());
    session.getTransaction().commit();
    assertFalse(((SessionImplementor) session).isClosed());
    assertTrue(((SessionImplementor) secondSession).isClosed());
    session.close();
    assertTrue(((SessionImplementor) session).isClosed());
    assertTrue(((SessionImplementor) secondSession).isClosed());
    session = sessionFactory().openSession();
    session.getTransaction().begin();
    IrrelevantEntity it = (IrrelevantEntity) session.byId(IrrelevantEntity.class).load(id);
    assertNotNull(it);
    session.delete(it);
    session.getTransaction().commit();
    session.close();
}
Also used : IrrelevantEntity(org.hibernate.IrrelevantEntity) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 2 with IrrelevantEntity

use of org.hibernate.IrrelevantEntity 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());
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) PostInsertEventListener(org.hibernate.event.spi.PostInsertEventListener) IrrelevantEntity(org.hibernate.IrrelevantEntity) PostInsertEvent(org.hibernate.event.spi.PostInsertEvent) Session(org.hibernate.Session) EventListenerRegistry(org.hibernate.event.service.spi.EventListenerRegistry) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 3 with IrrelevantEntity

use of org.hibernate.IrrelevantEntity in project hibernate-orm by hibernate.

the class LegacyPostCommitListenerTest method testPostCommitUpdateListenerSuccess.

@Test
@TestForIssue(jiraKey = "HHH-1582")
public void testPostCommitUpdateListenerSuccess() {
    Session session = openSession();
    Transaction transaction = session.beginTransaction();
    IrrelevantEntity irrelevantEntity = new IrrelevantEntity();
    irrelevantEntity.setName("Irrelevant");
    session.save(irrelevantEntity);
    session.flush();
    transaction.commit();
    session = openSession();
    transaction = session.beginTransaction();
    irrelevantEntity.setName("Irrelevant 2");
    session.update(irrelevantEntity);
    session.flush();
    transaction.commit();
    session.close();
    Assert.assertEquals(1, ((LegacyPostCommitUpdateEventListener) postCommitUpdateEventListener).fired);
}
Also used : Transaction(org.hibernate.Transaction) IrrelevantEntity(org.hibernate.IrrelevantEntity) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 4 with IrrelevantEntity

use of org.hibernate.IrrelevantEntity in project hibernate-orm by hibernate.

the class PostCommitListenerTest method testPostCommitUpdateListenerRollback.

@Test
@TestForIssue(jiraKey = "HHH-1582")
public void testPostCommitUpdateListenerRollback() {
    Session session = openSession();
    Transaction transaction = session.beginTransaction();
    IrrelevantEntity irrelevantEntity = new IrrelevantEntity();
    irrelevantEntity.setName("Irrelevant");
    session.save(irrelevantEntity);
    session.flush();
    transaction.commit();
    session.close();
    session = openSession();
    transaction = session.beginTransaction();
    irrelevantEntity.setName("Irrelevant 2");
    session.update(irrelevantEntity);
    session.flush();
    transaction.rollback();
    session.close();
    Assert.assertEquals(0, ((TestPostCommitUpdateEventListener) postCommitUpdateEventListener).sucess);
    Assert.assertEquals(1, ((TestPostCommitUpdateEventListener) postCommitUpdateEventListener).failed);
}
Also used : Transaction(org.hibernate.Transaction) IrrelevantEntity(org.hibernate.IrrelevantEntity) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 5 with IrrelevantEntity

use of org.hibernate.IrrelevantEntity in project hibernate-orm by hibernate.

the class PostCommitListenerTest method testPostCommitDeleteListenerRollback.

@Test
@TestForIssue(jiraKey = "HHH-1582")
public void testPostCommitDeleteListenerRollback() {
    Session session = openSession();
    Transaction transaction = session.beginTransaction();
    IrrelevantEntity irrelevantEntity = new IrrelevantEntity();
    irrelevantEntity.setName("Irrelevant");
    session.save(irrelevantEntity);
    session.flush();
    transaction.commit();
    session.close();
    session = openSession();
    transaction = session.beginTransaction();
    session.delete(irrelevantEntity);
    session.flush();
    transaction.rollback();
    session.close();
    Assert.assertEquals(0, ((TestPostCommitDeleteEventListener) postCommitDeleteEventListener).success);
    Assert.assertEquals(1, ((TestPostCommitDeleteEventListener) postCommitDeleteEventListener).failed);
}
Also used : Transaction(org.hibernate.Transaction) IrrelevantEntity(org.hibernate.IrrelevantEntity) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

IrrelevantEntity (org.hibernate.IrrelevantEntity)14 Session (org.hibernate.Session)14 TestForIssue (org.hibernate.testing.TestForIssue)14 Test (org.junit.Test)14 Transaction (org.hibernate.Transaction)12 EventListenerRegistry (org.hibernate.event.service.spi.EventListenerRegistry)1 PostInsertEvent (org.hibernate.event.spi.PostInsertEvent)1 PostInsertEventListener (org.hibernate.event.spi.PostInsertEventListener)1 EntityPersister (org.hibernate.persister.entity.EntityPersister)1