use of org.eclipse.persistence.exceptions.OptimisticLockException in project eclipselink by eclipse-ee4j.
the class OptimisticConcurrencyJUnitTestSuite method testVersionUpdateWithNullValue.
/**
* test: updating the version field with null value.
* This should throw an exception
*/
public void testVersionUpdateWithNullValue() {
EntityManager em = createEntityManager();
Employee employee;
try {
beginTransaction(em);
employee = ModelExamples.employeeExample1();
em.persist(employee);
commitTransaction(em);
beginTransaction(em);
Employee employee2 = em.find(Employee.class, employee.getId());
employee2.setVersion(null);
commitTransaction(em);
fail("employee2.setVersion(null) didn't throw exception");
} catch (PersistenceException pe) {
// expected behavior
} catch (Exception exception) {
Throwable persistenceException = exception;
// Remove an wrapping exceptions such as rollback, runtime, etc.
while (persistenceException != null && !(persistenceException instanceof OptimisticLockException)) {
// In the server this is always a rollback exception, need to get nested exception.
persistenceException = persistenceException.getCause();
}
if (persistenceException instanceof OptimisticLockException) {
OptimisticLockException oe = (OptimisticLockException) persistenceException;
return;
} else {
fail("employee2.setVersion(null) threw a wrong exception: " + exception.getMessage());
}
} finally {
if (isTransactionActive(em)) {
rollbackTransaction(em);
}
closeEntityManager(em);
}
}
Aggregations