use of javax.persistence.StoredProcedureQuery in project tests by datanucleus.
the class StoredProcedureTest method testExecuteInputParamAndOutputParam.
public void testExecuteInputParamAndOutputParam() {
if (vendorID == null) {
return;
}
if (storeMgr instanceof RDBMSStoreManager) {
DatastoreAdapter dba = ((RDBMSStoreManager) storeMgr).getDatastoreAdapter();
if (!dba.supportsOption(DatastoreAdapter.STORED_PROCEDURES)) {
LOG.warn("Database doesnt support stored procedures so ignoring the test");
return;
}
}
String procName = "DN_PROC_INPUTPARAM_OUTPUTPARAM";
RDBMSStoreManager rdbmsMgr = (RDBMSStoreManager) storeMgr;
ManagedConnection mc = rdbmsMgr.getConnectionManager().getConnection(-1);
try {
Connection conn = (Connection) mc.getConnection();
Statement stmt = conn.createStatement();
// Drop it first
String dropStmt = "DROP PROCEDURE IF EXISTS " + procName;
stmt.execute(dropStmt);
// Create it
String createStmt = "CREATE PROCEDURE " + procName + "(IN PARAM1 VARCHAR(255), OUT PARAM2 INT) BEGIN " + "SELECT COUNT(*) INTO PARAM2 FROM JPA_AN_PERSON WHERE FIRSTNAME = PARAM1; END";
stmt.execute(createStmt);
} catch (SQLException sqle) {
fail("Exception in drop-create of stored procedure : " + sqle.getMessage());
} finally {
mc.close();
}
try {
JPAEntityManager em = (JPAEntityManager) getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
Person p1 = new Person(101, "Fred", "Flintstone", "fred.flintstone@warnerbros.com");
Person p2 = new Person(102, "Fred", "Gravel", "fred.gravel@warnerbros.com");
em.persist(p1);
em.persist(p2);
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception in test : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
em = (JPAEntityManager) getEM();
tx = em.getTransaction();
try {
tx.begin();
// Get value to compare against
Query q = em.createQuery("SELECT COUNT(p) FROM " + Person.class.getName() + " p WHERE p.firstName = 'Fred'");
Long count = (Long) q.getSingleResult();
// Execute stored proc and compare
StoredProcedureQuery spq = em.createStoredProcedureQuery(procName);
spq.registerStoredProcedureParameter("PARAM1", String.class, ParameterMode.IN);
spq.registerStoredProcedureParameter("PARAM2", Integer.class, ParameterMode.OUT);
spq.setParameter("PARAM1", "Fred");
boolean val = spq.execute();
assertFalse("Flag for result set returned true but should have been false", val);
Object paramVal = spq.getOutputParameterValue("PARAM2");
assertEquals("Output parameter is incorrect", new Integer(count.intValue()), paramVal);
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception in test : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
// Cleanup data
clean(Person.class);
}
}
use of javax.persistence.StoredProcedureQuery in project tests by datanucleus.
the class StoredProcedureTest method testExecuteWithoutParamsReturningResultSet.
public void testExecuteWithoutParamsReturningResultSet() {
if (vendorID == null) {
return;
}
if (storeMgr instanceof RDBMSStoreManager) {
DatastoreAdapter dba = ((RDBMSStoreManager) storeMgr).getDatastoreAdapter();
if (!dba.supportsOption(DatastoreAdapter.STORED_PROCEDURES)) {
LOG.warn("Database doesnt support stored procedures so ignoring the test");
return;
}
}
String procName = "DN_PROC_NOPARAMS_RS";
RDBMSStoreManager rdbmsMgr = (RDBMSStoreManager) storeMgr;
ManagedConnection mc = rdbmsMgr.getConnectionManager().getConnection(-1);
try {
Connection conn = (Connection) mc.getConnection();
Statement stmt = conn.createStatement();
// Drop it first
String dropStmt = "DROP PROCEDURE IF EXISTS " + procName;
stmt.execute(dropStmt);
// Create it
String createStmt = "CREATE PROCEDURE " + procName + "() BEGIN " + "SELECT COUNT(*) FROM JPA_AN_PERSON; END";
stmt.execute(createStmt);
} catch (SQLException sqle) {
fail("Exception in drop-create of stored procedure : " + sqle.getMessage());
} finally {
mc.close();
}
try {
JPAEntityManager em = (JPAEntityManager) getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
Person p = new Person(101, "Fred", "Flintstone", "fred.flintstone@warnerbros.com");
em.persist(p);
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception in test : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
em = (JPAEntityManager) getEM();
tx = em.getTransaction();
try {
tx.begin();
// Get value to compare against
Query q = em.createQuery("SELECT COUNT(p) FROM " + Person.class.getName() + " p");
Long count = (Long) q.getSingleResult();
// Execute stored proc and compare
StoredProcedureQuery spq = em.createStoredProcedureQuery(procName);
boolean val = spq.execute();
assertTrue("Return from execute should have been true", val);
List results = spq.getResultList();
assertNotNull("ResultSet was null!", results);
assertEquals("Number of results was wrong", 1, results.size());
assertEquals("Result set result was wrong", count, results.get(0));
assertFalse("More results present but should be the end", spq.hasMoreResults());
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception in test : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
// Cleanup data
clean(Person.class);
}
}
use of javax.persistence.StoredProcedureQuery in project tests by datanucleus.
the class StoredProcedureTest method testNamedProcWithMultipleResultSet.
public void testNamedProcWithMultipleResultSet() {
if (vendorID == null) {
return;
}
if (storeMgr instanceof RDBMSStoreManager) {
DatastoreAdapter dba = ((RDBMSStoreManager) storeMgr).getDatastoreAdapter();
if (!dba.supportsOption(DatastoreAdapter.STORED_PROCEDURES)) {
LOG.warn("Database doesnt support stored procedures so ignoring the test");
return;
}
}
String procName = "DN_PROC_NAMED_RS2";
RDBMSStoreManager rdbmsMgr = (RDBMSStoreManager) storeMgr;
ManagedConnection mc = rdbmsMgr.getConnectionManager().getConnection(-1);
try {
Connection conn = (Connection) mc.getConnection();
Statement stmt = conn.createStatement();
// Drop it first
String dropStmt = "DROP PROCEDURE IF EXISTS " + procName;
stmt.execute(dropStmt);
// Create it
String createStmt = "CREATE PROCEDURE " + procName + "(IN PARAM1 VARCHAR(255), IN PARAM2 VARCHAR(255)) BEGIN " + "SELECT * FROM JPA_AN_PERSON WHERE FIRSTNAME = PARAM1;" + "SELECT * FROM JPA_AN_ACCOUNT WHERE USERNAME = PARAM2;" + "END";
stmt.execute(createStmt);
} catch (SQLException sqle) {
fail("Exception in drop-create of stored procedure : " + sqle.getMessage());
} finally {
mc.close();
}
try {
JPAEntityManager em = (JPAEntityManager) getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
Person p = new Person(101, "Fred", "Flintstone", "fred.flintstone@warnerbros.com");
em.persist(p);
Account a = new Account();
a.setUsername("Fred");
a.setEnabled(true);
em.persist(a);
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception in test : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
em = (JPAEntityManager) getEM();
tx = em.getTransaction();
try {
tx.begin();
LOG.info(">> Executing stored proc");
// Execute stored proc and compare
StoredProcedureQuery spq = em.createNamedStoredProcedureQuery("myNamedSP2");
spq.setParameter("PARAM1", "Fred");
spq.setParameter("PARAM2", "Fred");
boolean val = spq.execute();
assertTrue("Return from execute should have been true", val);
List results = spq.getResultList();
assertNotNull("ResultSet was null!", results);
assertEquals("Number of results was wrong", 1, results.size());
for (Object result : results) {
LOG.info(">> result=" + result);
}
assertTrue("More results should be present but werent", spq.hasMoreResults());
List results2 = spq.getResultList();
assertNotNull("ResultSet2 was null!", results2);
assertEquals("Number of results2 was wrong", 1, results2.size());
for (Object result : results2) {
LOG.info(">> result=" + result);
}
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception in test : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
// Cleanup data
clean(Account.class);
clean(Person.class);
}
}
use of javax.persistence.StoredProcedureQuery in project tests by datanucleus.
the class StoredProcedureTest method testGetResultListWithoutParamsReturningResultSet.
public void testGetResultListWithoutParamsReturningResultSet() {
if (vendorID == null) {
return;
}
if (storeMgr instanceof RDBMSStoreManager) {
DatastoreAdapter dba = ((RDBMSStoreManager) storeMgr).getDatastoreAdapter();
if (!dba.supportsOption(DatastoreAdapter.STORED_PROCEDURES)) {
LOG.warn("Database doesnt support stored procedures so ignoring the test");
return;
}
}
String procName = "DN_PROC_NOPARAMS_RS";
RDBMSStoreManager rdbmsMgr = (RDBMSStoreManager) storeMgr;
ManagedConnection mc = rdbmsMgr.getConnectionManager().getConnection(-1);
try {
Connection conn = (Connection) mc.getConnection();
Statement stmt = conn.createStatement();
// Drop it first
String dropStmt = "DROP PROCEDURE IF EXISTS " + procName;
stmt.execute(dropStmt);
// Create it
String createStmt = "CREATE PROCEDURE " + procName + "() BEGIN " + "SELECT COUNT(*) FROM JPA_AN_PERSON; END";
stmt.execute(createStmt);
} catch (SQLException sqle) {
fail("Exception in drop-create of stored procedure : " + sqle.getMessage());
} finally {
mc.close();
}
try {
JPAEntityManager em = (JPAEntityManager) getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
Person p = new Person(101, "Fred", "Flintstone", "fred.flintstone@warnerbros.com");
em.persist(p);
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception in test : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
em = (JPAEntityManager) getEM();
tx = em.getTransaction();
try {
tx.begin();
// Get value to compare against
Query q = em.createQuery("SELECT COUNT(p) FROM " + Person.class.getName() + " p");
Long count = (Long) q.getSingleResult();
// Execute stored proc and compare
StoredProcedureQuery spq = em.createStoredProcedureQuery(procName);
List results = spq.getResultList();
assertNotNull("ResultSet was null!", results);
assertEquals("Number of results was wrong", 1, results.size());
assertEquals("Result set result was wrong", count, results.get(0));
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception in test : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
// Cleanup data
clean(Person.class);
}
}
use of javax.persistence.StoredProcedureQuery in project tests by datanucleus.
the class StoredProcedureTest method testNamedProcWithParamReturningResultSet.
public void testNamedProcWithParamReturningResultSet() {
if (vendorID == null) {
return;
}
if (storeMgr instanceof RDBMSStoreManager) {
DatastoreAdapter dba = ((RDBMSStoreManager) storeMgr).getDatastoreAdapter();
if (!dba.supportsOption(DatastoreAdapter.STORED_PROCEDURES)) {
LOG.warn("Database doesnt support stored procedures so ignoring the test");
return;
}
}
String procName = "DN_PROC_NAMED_RS";
RDBMSStoreManager rdbmsMgr = (RDBMSStoreManager) storeMgr;
ManagedConnection mc = rdbmsMgr.getConnectionManager().getConnection(-1);
try {
Connection conn = (Connection) mc.getConnection();
Statement stmt = conn.createStatement();
// Drop it first
String dropStmt = "DROP PROCEDURE IF EXISTS " + procName;
stmt.execute(dropStmt);
// Create it
String createStmt = "CREATE PROCEDURE " + procName + "(IN PARAM1 VARCHAR(255)) BEGIN " + "SELECT COUNT(*) FROM JPA_AN_PERSON WHERE FIRSTNAME = PARAM1; END";
stmt.execute(createStmt);
} catch (SQLException sqle) {
fail("Exception in drop-create of stored procedure : " + sqle.getMessage());
} finally {
mc.close();
}
try {
JPAEntityManager em = (JPAEntityManager) getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
Person p = new Person(101, "Fred", "Flintstone", "fred.flintstone@warnerbros.com");
em.persist(p);
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception in test : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
em = (JPAEntityManager) getEM();
tx = em.getTransaction();
try {
tx.begin();
// Get value to compare against
Query q = em.createQuery("SELECT COUNT(p) FROM " + Person.class.getName() + " p WHERE p.firstName='Fred'");
Long count = (Long) q.getSingleResult();
// Execute stored proc and compare
StoredProcedureQuery spq = em.createNamedStoredProcedureQuery("myNamedSP");
spq.setParameter("PARAM1", "Fred");
boolean val = spq.execute();
assertTrue("Return from execute should have been true", val);
List results = spq.getResultList();
assertNotNull("ResultSet was null!", results);
assertEquals("Number of results was wrong", 1, results.size());
assertEquals("Result set result was wrong", count, results.get(0));
assertFalse("More results present but should be the end", spq.hasMoreResults());
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception in test : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
// Cleanup data
clean(Person.class);
}
}
Aggregations