public void testNoReturnMultiParamProcedure() {
if (procedureList != null) {
EntityManager em = emf.createEntityManager();
Applicant applicant1 = new Applicant();
applicant1.setName("Charlie");
Applicant applicant2 = new Applicant();
applicant2.setName("Snoopy");
em.getTransaction().begin();
em.persist(applicant1);
em.persist(applicant2);
em.getTransaction().commit();
String sql = procedureList.callAddSuffixToName();
// query.getSingleResult() and query.getResultList() both throw an
// exception:
// Statement.executeQuery() cannot be called with a statement that
// returns a row count
try {
em.getTransaction().begin();
Query query = em.createNativeQuery(sql);
query.setParameter(1, "Charlie");
query.setParameter(2, "x");
query.getSingleResult();
em.getTransaction().commit();
fail("Expected exception. getSingleResult() with no returns " +
"should fail.");
} catch (Exception e) {
//Expected exception
em.getTransaction().rollback();
}
try {
em.getTransaction().begin();
Query query = em.createNativeQuery(sql);
query.setParameter(1, "Charlie");
query.setParameter(2, "x");
query.getResultList();
em.getTransaction().commit();
fail("Expected exception. getResultList() with no returns " +
"should fail.");
} catch (Exception e) {
//Expected exception
em.getTransaction().rollback();
}
// This one should work properly
try {
em.getTransaction().begin();
Query query = em.createNativeQuery(sql);
query.setParameter(1, "Charlie");
query.setParameter(2, "x");
query.executeUpdate();
em.getTransaction().commit();
} catch (Exception e) {
em.getTransaction().rollback();
fail("Caught unexpected exception executing stored procedure: "
+ e.getMessage());
}
// refresh the data
em.clear();
em.close();
em = emf.createEntityManager();
applicant1 = em.find(Applicant.class, applicant1.getId());
applicant2 = em.find(Applicant.class, applicant2.getId());
// verify one changed and one didn't
assertEquals("Charliex", applicant1.getName());
assertEquals("Snoopy", applicant2.getName());
em.clear();
em.close();
}
}