use of javax.persistence.RollbackException in project hibernate-orm by hibernate.
the class FlushAndTransactionTest method testRollbackExceptionOnOptimisticLockException.
@Test
public void testRollbackExceptionOnOptimisticLockException() throws Exception {
Book book = new Book();
book.name = "Stolen keys";
//new Integer( 50 );
book.id = null;
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
em.persist(book);
em.flush();
em.clear();
book.setName("kitty kid");
em.merge(book);
em.flush();
em.clear();
//non updated version
book.setName("kitty kid2");
em.unwrap(Session.class).update(book);
try {
em.getTransaction().commit();
fail("Commit should be rollbacked");
} catch (RollbackException e) {
assertTrue("During flush a StateStateException is wrapped into a OptimisticLockException", e.getCause() instanceof OptimisticLockException);
} finally {
em.close();
}
}
use of javax.persistence.RollbackException in project hibernate-orm by hibernate.
the class TransactionCommitFailureTest method testConfiguredInterceptor.
@Test
public void testConfiguredInterceptor() {
Map settings = basicSettings();
EntityManagerFactory emf = Bootstrap.getEntityManagerFactoryBuilder(new PersistenceUnitDescriptorAdapter(), settings).build();
EntityManager em = emf.createEntityManager();
try {
em.getTransaction().begin();
transactionFailureTrigger.set(true);
em.getTransaction().commit();
} catch (RollbackException e) {
assertEquals(COMMIT_FAILURE, e.getCause().getMessage());
} finally {
if (em.getTransaction() != null && em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
em.close();
emf.close();
}
}
use of javax.persistence.RollbackException in project hibernate-orm by hibernate.
the class BeanValidationTest method testBeanValidationIntegrationOnCommit.
@Test
public void testBeanValidationIntegrationOnCommit() {
CupHolder ch = new CupHolder();
ch.setRadius(new BigDecimal("9"));
ch.setTitle("foo");
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
em.persist(ch);
em.flush();
try {
ch.setRadius(new BigDecimal("12"));
em.getTransaction().commit();
fail("invalid object should not be persisted");
} catch (RollbackException e) {
final Throwable cve = e.getCause();
assertTrue(cve instanceof ConstraintViolationException);
assertEquals(1, ((ConstraintViolationException) cve).getConstraintViolations().size());
}
em.close();
}
use of javax.persistence.RollbackException in project hibernate-orm by hibernate.
the class FlushAndTransactionTest method testRollbackOnlyOnPersistenceException.
@Test
public void testRollbackOnlyOnPersistenceException() throws Exception {
Book book = new Book();
book.name = "Stolen keys";
//new Integer( 50 );
book.id = null;
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
try {
em.persist(book);
em.flush();
em.clear();
book.setName("kitty kid");
em.merge(book);
em.flush();
em.clear();
//non updated version
book.setName("kitty kid2");
em.merge(book);
em.flush();
fail("optimistic locking exception");
} catch (PersistenceException e) {
//success
}
try {
em.getTransaction().commit();
fail("Commit should be rollbacked");
} catch (RollbackException e) {
//success
} finally {
em.close();
}
}
Aggregations