Search in sources :

Example 86 with TestForIssue

use of org.hibernate.testing.TestForIssue in project hibernate-orm by hibernate.

the class SessionWithSharedConnectionTest method testSharedTransactionContextSessionClosing.

@Test
@TestForIssue(jiraKey = "HHH-7090")
public void testSharedTransactionContextSessionClosing() {
    Session session = sessionFactory().openSession();
    session.getTransaction().begin();
    Session secondSession = session.sessionWithOptions().transactionContext().openSession();
    secondSession.createCriteria(IrrelevantEntity.class).list();
    //the list should have registered and then released a JDBC resource
    assertFalse(((SessionImplementor) secondSession).getJdbcCoordinator().getResourceRegistry().hasRegisteredResources());
    assertTrue(session.isOpen());
    assertTrue(secondSession.isOpen());
    assertSame(session.getTransaction(), secondSession.getTransaction());
    session.getTransaction().commit();
    assertTrue(session.isOpen());
    assertTrue(secondSession.isOpen());
    secondSession.close();
    assertTrue(session.isOpen());
    assertFalse(secondSession.isOpen());
    session.close();
    assertFalse(session.isOpen());
    assertFalse(secondSession.isOpen());
}
Also used : IrrelevantEntity(org.hibernate.IrrelevantEntity) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 87 with TestForIssue

use of org.hibernate.testing.TestForIssue 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 88 with TestForIssue

use of org.hibernate.testing.TestForIssue in project hibernate-orm by hibernate.

the class SessionWithSharedConnectionTest method testChildSessionTwoTransactions.

@Test
@TestForIssue(jiraKey = "HHH-7239")
public void testChildSessionTwoTransactions() throws Exception {
    Session session = openSession();
    session.getTransaction().begin();
    //open secondary session with managed options
    Session secondarySession = session.sessionWithOptions().connection().flushBeforeCompletion(true).autoClose(true).openSession();
    //the secondary session should be automatically closed afterQuery the commit
    session.getTransaction().commit();
    assertFalse(secondarySession.isOpen());
    //should be able to create a new transaction and carry on using the original session
    session.getTransaction().begin();
    session.getTransaction().commit();
}
Also used : Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 89 with TestForIssue

use of org.hibernate.testing.TestForIssue 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 90 with TestForIssue

use of org.hibernate.testing.TestForIssue in project hibernate-orm by hibernate.

the class QueryTest method testMemberOfSyntax.

@Test
@TestForIssue(jiraKey = "HHH-5209")
public void testMemberOfSyntax() {
    // performs syntax checking of the MEMBER OF predicate against a basic collection
    Session s = openSession();
    s.createQuery("from EntityWithAnElementCollection e where 'abc' member of e.someStrings").list();
    s.close();
}
Also used : Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

TestForIssue (org.hibernate.testing.TestForIssue)649 Test (org.junit.Test)647 Session (org.hibernate.Session)357 EntityManager (javax.persistence.EntityManager)97 List (java.util.List)91 Transaction (org.hibernate.Transaction)88 MetadataSources (org.hibernate.boot.MetadataSources)47 ArrayList (java.util.ArrayList)38 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)38 Query (org.hibernate.Query)28 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)25 Metadata (org.hibernate.boot.Metadata)24 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)24 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)23 Map (java.util.Map)22 CollectionEntry (org.hibernate.engine.spi.CollectionEntry)19 HashMap (java.util.HashMap)18 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)18 PersistentClass (org.hibernate.mapping.PersistentClass)18 HibernateException (org.hibernate.HibernateException)16