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