Search in sources :

Example 1 with TransactionRequiredException

use of javax.persistence.TransactionRequiredException in project spring-framework by spring-projects.

the class ApplicationManagedEntityManagerIntegrationTests method testCannotFlushWithoutGettingTransaction.

@Test
public void testCannotFlushWithoutGettingTransaction() {
    EntityManager em = entityManagerFactory.createEntityManager();
    try {
        doInstantiateAndSave(em);
        fail("Should have thrown TransactionRequiredException");
    } catch (TransactionRequiredException ex) {
    // expected
    }
    // TODO following lines are a workaround for Hibernate bug
    // If Hibernate throws an exception due to flush(),
    // it actually HAS flushed, meaning that the database
    // was updated outside the transaction
    deleteAllPeopleUsingEntityManager(sharedEntityManager);
    setComplete();
}
Also used : EntityManager(javax.persistence.EntityManager) TransactionRequiredException(javax.persistence.TransactionRequiredException) Test(org.junit.Test)

Example 2 with TransactionRequiredException

use of javax.persistence.TransactionRequiredException in project hibernate-orm by hibernate.

the class FlushAndTransactionTest method testAlwaysTransactionalOperations.

@Test
public void testAlwaysTransactionalOperations() throws Exception {
    Book book = new Book();
    book.name = "Le petit prince";
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    em.persist(book);
    em.getTransaction().commit();
    try {
        em.flush();
        fail("flush has to be inside a Tx");
    } catch (TransactionRequiredException e) {
    //success
    }
    try {
        em.lock(book, LockModeType.READ);
        fail("lock has to be inside a Tx");
    } catch (TransactionRequiredException e) {
    //success
    }
    em.getTransaction().begin();
    em.remove(em.find(Book.class, book.id));
    em.getTransaction().commit();
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) TransactionRequiredException(javax.persistence.TransactionRequiredException) Test(org.junit.Test)

Example 3 with TransactionRequiredException

use of javax.persistence.TransactionRequiredException in project hibernate-orm by hibernate.

the class TransactionJoiningTest method testExplicitJoiningTransactionRequiredException.

@Test
@SuppressWarnings("EmptyCatchBlock")
public void testExplicitJoiningTransactionRequiredException() throws Exception {
    // explicitly calling EntityManager#joinTransaction outside of an active transaction should cause
    // a TransactionRequiredException to be thrown
    EntityManager entityManager = entityManagerFactory().createEntityManager();
    assertFalse("setup problem", JtaStatusHelper.isActive(TestingJtaPlatformImpl.INSTANCE.getTransactionManager()));
    try {
        entityManager.joinTransaction();
        fail("Expected joinTransaction() to fail since there is no active JTA transaction");
    } catch (TransactionRequiredException expected) {
    }
}
Also used : EntityManager(javax.persistence.EntityManager) TransactionRequiredException(javax.persistence.TransactionRequiredException) Test(org.junit.Test)

Example 4 with TransactionRequiredException

use of javax.persistence.TransactionRequiredException in project hibernate-orm by hibernate.

the class IdentityGeneratedKeysTest method testPersistOutsideTransactionCascadedToManyToOne.

@Test
public void testPersistOutsideTransactionCascadedToManyToOne() {
    long initialInsertCount = sessionFactory().getStatistics().getEntityInsertCount();
    Session s = openSession();
    try {
        MyEntity myEntity = new MyEntity("test-persist");
        myEntity.setSibling(new MySibling("test-persist-sibling-out"));
        s.persist(myEntity);
        assertEquals("persist on identity column not delayed", initialInsertCount, sessionFactory().getStatistics().getEntityInsertCount());
        assertNull(myEntity.getId());
        s.flush();
        fail("TransactionRequiredException expected upon flush.");
    } catch (PersistenceException ex) {
        // expected
        assertTyping(TransactionRequiredException.class, ex);
    } finally {
        s.close();
    }
}
Also used : TransactionRequiredException(javax.persistence.TransactionRequiredException) PersistenceException(javax.persistence.PersistenceException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 5 with TransactionRequiredException

use of javax.persistence.TransactionRequiredException in project hibernate-orm by hibernate.

the class IdentityGeneratedKeysTest method testPersistOutsideTransactionCascadedToInverseCollection.

@Test
@SuppressWarnings({ "unchecked" })
public void testPersistOutsideTransactionCascadedToInverseCollection() {
    long initialInsertCount = sessionFactory().getStatistics().getEntityInsertCount();
    Session s = openSession();
    try {
        MyEntity myEntity2 = new MyEntity("test-persist-2");
        MyChild child = new MyChild("test-child-persist-inverse");
        myEntity2.getInverseChildren().add(child);
        child.setInverseParent(myEntity2);
        s.persist(myEntity2);
        assertEquals("persist on identity column not delayed", initialInsertCount, sessionFactory().getStatistics().getEntityInsertCount());
        assertNull(myEntity2.getId());
        s.flush();
        fail("TransactionRequiredException expected upon flush.");
    } catch (PersistenceException ex) {
        // expected
        assertTyping(TransactionRequiredException.class, ex);
    } finally {
        s.close();
    }
}
Also used : TransactionRequiredException(javax.persistence.TransactionRequiredException) PersistenceException(javax.persistence.PersistenceException) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

TransactionRequiredException (javax.persistence.TransactionRequiredException)11 Test (org.junit.Test)10 PersistenceException (javax.persistence.PersistenceException)6 EntityManager (javax.persistence.EntityManager)5 Session (org.hibernate.Session)5 EntityExistsException (javax.persistence.EntityExistsException)1 EntityNotFoundException (javax.persistence.EntityNotFoundException)1 NoResultException (javax.persistence.NoResultException)1 NonUniqueResultException (javax.persistence.NonUniqueResultException)1 OptimisticLockException (javax.persistence.OptimisticLockException)1