Search in sources :

Example 6 with TransactionRequiredException

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

the class IdentityGeneratedKeysTest method testPersistOutsideTransaction.

@Test
public void testPersistOutsideTransaction() {
    Session s = openSession();
    try {
        // first test save() which should force an immediate insert...
        MyEntity myEntity1 = new MyEntity("test-save");
        Long id = (Long) s.save(myEntity1);
        assertNotNull("identity column did not force immediate insert", id);
        assertEquals(id, myEntity1.getId());
        // next test persist() which should cause a delayed insert...
        long initialInsertCount = sessionFactory().getStatistics().getEntityInsertCount();
        MyEntity myEntity2 = new MyEntity("test-persist");
        s.persist(myEntity2);
        assertEquals("persist on identity column not delayed", initialInsertCount, sessionFactory().getStatistics().getEntityInsertCount());
        assertNull(myEntity2.getId());
        // an explicit flush should cause execution of the delayed insertion
        s.flush();
        fail("TransactionRequiredException required 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 7 with TransactionRequiredException

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

the class SynchronizationTypeTest method testDisallowedOperations.

@Test
public void testDisallowedOperations() throws Exception {
    // test calling operations that are disallowed while a UNSYNCHRONIZED persistence context is not
    // yet joined/enlisted
    assertFalse("setup problem", JtaStatusHelper.isActive(TestingJtaPlatformImpl.INSTANCE.getTransactionManager()));
    TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
    assertTrue("setup problem", JtaStatusHelper.isActive(TestingJtaPlatformImpl.INSTANCE.getTransactionManager()));
    EntityManager entityManager = entityManagerFactory().createEntityManager(SynchronizationType.UNSYNCHRONIZED, null);
    // explicit flushing
    try {
        entityManager.flush();
        fail("Expecting flush() call to fail");
    } catch (TransactionRequiredException expected) {
    }
    // bulk operations
    try {
        entityManager.createQuery("delete Book").executeUpdate();
        fail("Expecting executeUpdate() call to fail");
    } catch (TransactionRequiredException expected) {
    }
    try {
        entityManager.createQuery("update Book set name = null").executeUpdate();
        fail("Expecting executeUpdate() call to fail");
    } catch (TransactionRequiredException expected) {
    }
    try {
        CriteriaDelete<Book> deleteCriteria = entityManager.getCriteriaBuilder().createCriteriaDelete(Book.class);
        deleteCriteria.from(Book.class);
        entityManager.createQuery(deleteCriteria).executeUpdate();
        fail("Expecting executeUpdate() call to fail");
    } catch (TransactionRequiredException expected) {
    }
    try {
        CriteriaUpdate<Book> updateCriteria = entityManager.getCriteriaBuilder().createCriteriaUpdate(Book.class);
        updateCriteria.from(Book.class);
        updateCriteria.set(Book_.name, (String) null);
        entityManager.createQuery(updateCriteria).executeUpdate();
        fail("Expecting executeUpdate() call to fail");
    } catch (TransactionRequiredException expected) {
    }
    try {
        entityManager.createQuery("select b from Book b").setLockMode(LockModeType.PESSIMISTIC_WRITE).getResultList();
        fail("Expecting attempted pessimistic lock query to fail");
    } catch (TransactionRequiredException expected) {
    }
    entityManager.close();
    TestingJtaPlatformImpl.INSTANCE.getTransactionManager().rollback();
}
Also used : EntityManager(javax.persistence.EntityManager) TransactionRequiredException(javax.persistence.TransactionRequiredException) Test(org.junit.Test)

Example 8 with TransactionRequiredException

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

the class IdentityGeneratedKeysTest method testPersistOutsideTransactionCascadedToNonInverseCollection.

@Test
@SuppressWarnings({ "unchecked" })
public void testPersistOutsideTransactionCascadedToNonInverseCollection() {
    long initialInsertCount = sessionFactory().getStatistics().getEntityInsertCount();
    Session s = openSession();
    try {
        MyEntity myEntity = new MyEntity("test-persist");
        myEntity.getNonInverseChildren().add(new MyChild("test-child-persist-non-inverse"));
        s.persist(myEntity);
        assertEquals("persist on identity column not delayed", initialInsertCount, sessionFactory().getStatistics().getEntityInsertCount());
        assertNull(myEntity.getId());
        s.flush();
        fail("TransactionRequiredException required 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 9 with TransactionRequiredException

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

the class IdentityGeneratedKeysTest method testPersistOutsideTransactionCascadedFromManyToOne.

@Test
public void testPersistOutsideTransactionCascadedFromManyToOne() {
    long initialInsertCount = sessionFactory().getStatistics().getEntityInsertCount();
    Session s = openSession();
    try {
        MyEntity myEntity2 = new MyEntity("test-persist-2");
        MySibling sibling = new MySibling("test-persist-sibling-in");
        sibling.setEntity(myEntity2);
        s.persist(sibling);
        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)

Example 10 with TransactionRequiredException

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

the class EntityManagerFactoryUtilsTests method testConvertJpaPersistenceException.

/*
	 * Test method for
	 * 'org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessException(PersistenceException)'
	 */
@Test
@SuppressWarnings("serial")
public void testConvertJpaPersistenceException() {
    EntityNotFoundException entityNotFound = new EntityNotFoundException();
    assertSame(JpaObjectRetrievalFailureException.class, EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(entityNotFound).getClass());
    NoResultException noResult = new NoResultException();
    assertSame(EmptyResultDataAccessException.class, EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(noResult).getClass());
    NonUniqueResultException nonUniqueResult = new NonUniqueResultException();
    assertSame(IncorrectResultSizeDataAccessException.class, EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(nonUniqueResult).getClass());
    OptimisticLockException optimisticLock = new OptimisticLockException();
    assertSame(JpaOptimisticLockingFailureException.class, EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(optimisticLock).getClass());
    EntityExistsException entityExists = new EntityExistsException("foo");
    assertSame(DataIntegrityViolationException.class, EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(entityExists).getClass());
    TransactionRequiredException transactionRequired = new TransactionRequiredException("foo");
    assertSame(InvalidDataAccessApiUsageException.class, EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(transactionRequired).getClass());
    PersistenceException unknown = new PersistenceException() {
    };
    assertSame(JpaSystemException.class, EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(unknown).getClass());
}
Also used : NonUniqueResultException(javax.persistence.NonUniqueResultException) TransactionRequiredException(javax.persistence.TransactionRequiredException) PersistenceException(javax.persistence.PersistenceException) OptimisticLockException(javax.persistence.OptimisticLockException) EntityNotFoundException(javax.persistence.EntityNotFoundException) NoResultException(javax.persistence.NoResultException) EntityExistsException(javax.persistence.EntityExistsException) 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