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();
}
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();
}
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) {
}
}
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();
}
}
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();
}
}
Aggregations