Search in sources :

Example 46 with StoredProcedureQuery

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();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) EntityManager(javax.persistence.EntityManager) StoredProcedureQuery(javax.persistence.StoredProcedureQuery) Test(org.testng.annotations.Test) QueryingTest(org.jpwh.test.querying.QueryingTest)

Example 47 with StoredProcedureQuery

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;
}
Also used : StoredProcedureQuery(javax.persistence.StoredProcedureQuery)

Example 48 with StoredProcedureQuery

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();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) StoredProcedureQuery(javax.persistence.StoredProcedureQuery) NamedStoredProcedureQuery(javax.persistence.NamedStoredProcedureQuery) StoredProcedureParameter(javax.persistence.StoredProcedureParameter) Parameter(javax.persistence.Parameter) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 49 with StoredProcedureQuery

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();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) StoredProcedureQuery(javax.persistence.StoredProcedureQuery) NamedStoredProcedureQuery(javax.persistence.NamedStoredProcedureQuery) StoredProcedureParameter(javax.persistence.StoredProcedureParameter) Parameter(javax.persistence.Parameter) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 50 with StoredProcedureQuery

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);
    });
}
Also used : StoredProcedureQuery(javax.persistence.StoredProcedureQuery) Test(org.junit.Test)

Aggregations

StoredProcedureQuery (javax.persistence.StoredProcedureQuery)60 Test (org.junit.Test)47 EntityManager (javax.persistence.EntityManager)38 NamedStoredProcedureQuery (javax.persistence.NamedStoredProcedureQuery)19 SQLException (java.sql.SQLException)12 List (java.util.List)11 TestForIssue (org.hibernate.testing.TestForIssue)11 Connection (java.sql.Connection)6 Statement (java.sql.Statement)6 EntityTransaction (javax.persistence.EntityTransaction)6 JPAEntityManager (org.datanucleus.api.jpa.JPAEntityManager)6 Person (org.datanucleus.samples.annotations.models.company.Person)6 ManagedConnection (org.datanucleus.store.connection.ManagedConnection)6 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)6 DatastoreAdapter (org.datanucleus.store.rdbms.adapter.DatastoreAdapter)6 Parameter (javax.persistence.Parameter)5 Query (javax.persistence.Query)5 UserTransaction (javax.transaction.UserTransaction)5 QueryingTest (org.jpwh.test.querying.QueryingTest)5 Test (org.testng.annotations.Test)5