use of javax.persistence.StoredProcedureQuery in project microservices by pwillhan.
the class CallStoredProcedures method callUpdateCount.
@Test(groups = "MYSQL")
public void callUpdateCount() throws Exception {
TestDataCategoriesItems testDataCategoriesItems = storeTestData();
final Long ITEM_ID = testDataCategoriesItems.items.getFirstId();
UserTransaction tx = TM.getUserTransaction();
try {
tx.begin();
EntityManager em = JPA.createEntityManager();
/*
em.unwrap(Session.class).doWork(
new CallProcedureAndPrintResult() {
@Override
protected CallableStatement prepare(Connection connection) throws SQLException {
CallableStatement statement = connection.prepareCall("{call UPDATE_ITEM(?, ?)}");
statement.setLong(1, ITEM_ID);
statement.setString(2, "New Name");
return statement;
}
@Override
protected ResultSet execute(CallableStatement statement) throws SQLException {
// MySQL executes ROW_COUNT() when executeUpdate() is called, so the
// update count depends on the last INSERT/UPDATE statement in the procedure!
int updateCount = statement.executeUpdate();
System.out.println("### UPDATE COUNT: " + updateCount);
return null;
}
}
);
*/
StoredProcedureQuery query = em.createStoredProcedureQuery("UPDATE_ITEM");
query.registerStoredProcedureParameter("itemId", Long.class, ParameterMode.IN);
query.registerStoredProcedureParameter("name", String.class, ParameterMode.IN);
query.setParameter("itemId", ITEM_ID);
query.setParameter("name", "New Item Name");
// Update count is 1
assertEquals(query.executeUpdate(), 1);
// Alternative:
// assertFalse(query.execute()); // First result is NOT a result set
// assertEquals(query.getUpdateCount(), 1);
tx.commit();
em.close();
} finally {
TM.rollback();
}
}
use of javax.persistence.StoredProcedureQuery in project kylo by Teradata.
the class NifiFeedProcessorStatisticsRepositoryImpl method compactFeedProcessorStats.
@Override
public String compactFeedProcessorStats() {
StoredProcedureQuery query = em.createStoredProcedureQuery("compact_feed_processor_stats");
query.registerStoredProcedureParameter("res", String.class, ParameterMode.OUT);
query.execute();
String result = (String) query.getOutputParameterValue("res");
return result;
}
use of javax.persistence.StoredProcedureQuery in project hibernate-orm by hibernate.
the class HSQLStoreProcedureTest method testGetPositionalParameters2.
@Test
@TestForIssue(jiraKey = "HHH-10915")
public void testGetPositionalParameters2() {
EntityManager em = entityManagerFactory.createEntityManager();
try {
StoredProcedureQuery query = em.createNamedStoredProcedureQuery("User.inoutprocpositional");
final Set<Parameter<?>> parameters = query.getParameters();
assertThat(parameters.size(), is(2));
assertThat(query.getParameter(1), not(nullValue()));
assertThat(query.getParameter(2), not(nullValue()));
assertThat(query.getParameter(1, Integer.class), not(nullValue()));
try {
query.getParameter(3);
fail("An IllegalArgumentException is expected, A parameter at position 3 does not exist");
} catch (IllegalArgumentException iae) {
// expected
}
try {
query.getParameter(1, String.class);
fail("An IllegalArgumentException is expected, The parameter at position 1 is of type Integer not String");
} catch (IllegalArgumentException iae) {
// expected
}
} finally {
em.close();
}
}
use of javax.persistence.StoredProcedureQuery in project hibernate-orm by hibernate.
the class HSQLStoreProcedureTest method testGetNamedParameters.
@Test
@TestForIssue(jiraKey = "HHH-10915")
public void testGetNamedParameters() {
EntityManager em = entityManagerFactory.createEntityManager();
try {
StoredProcedureQuery query = em.createNamedStoredProcedureQuery("User.inoutproc");
final Set<Parameter<?>> parameters = query.getParameters();
assertThat(parameters.size(), is(2));
assertThat(query.getParameter("arg1"), not(nullValue()));
assertThat(query.getParameter("res"), not(nullValue()));
assertThat(query.getParameter("arg1", Integer.class), not(nullValue()));
try {
query.getParameter("arg1", String.class);
fail("An IllegalArgumentException is expected, A parameter with name arg1 and type String does not exist");
} catch (IllegalArgumentException iae) {
// expected
}
try {
query.getParameter("arg2");
fail("An IllegalArgumentException is expected, A parameter with name arg2 does not exist");
} catch (IllegalArgumentException iae) {
// expected
}
} finally {
em.close();
}
}
use of javax.persistence.StoredProcedureQuery in project hibernate-orm by hibernate.
the class MySQLStoredProcedureTest method testStoredProcedureOutParameter.
@Test
public void testStoredProcedureOutParameter() {
doInJPA(this::entityManagerFactory, entityManager -> {
// tag::sql-jpa-call-sp-out-mysql-example[]
StoredProcedureQuery query = entityManager.createStoredProcedureQuery("sp_count_phones");
query.registerStoredProcedureParameter("personId", Long.class, ParameterMode.IN);
query.registerStoredProcedureParameter("phoneCount", Long.class, ParameterMode.OUT);
query.setParameter("personId", 1L);
query.execute();
Long phoneCount = (Long) query.getOutputParameterValue("phoneCount");
// end::sql-jpa-call-sp-out-mysql-example[]
assertEquals(Long.valueOf(2), phoneCount);
});
}
Aggregations