Search in sources :

Example 1 with StoredProcedureQuery

use of javax.persistence.StoredProcedureQuery in project hibernate-orm by hibernate.

the class MySQLStoredProcedureTest method testStoredProcedureRefCursor.

@Test
public void testStoredProcedureRefCursor() {
    try {
        doInJPA(this::entityManagerFactory, entityManager -> {
            StoredProcedureQuery query = entityManager.createStoredProcedureQuery("sp_phones");
            query.registerStoredProcedureParameter(1, void.class, ParameterMode.REF_CURSOR);
            query.registerStoredProcedureParameter(2, Long.class, ParameterMode.IN);
            query.setParameter(2, 1L);
            List<Object[]> personComments = query.getResultList();
            assertEquals(2, personComments.size());
        });
    } catch (Exception e) {
        assertTrue(Pattern.compile("Dialect .*? not known to support REF_CURSOR parameters").matcher(e.getCause().getMessage()).matches());
    }
}
Also used : StoredProcedureQuery(javax.persistence.StoredProcedureQuery) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 2 with StoredProcedureQuery

use of javax.persistence.StoredProcedureQuery in project hibernate-orm by hibernate.

the class OracleStoredProcedureTest method testStoredProcedureOutParameter.

@Test
public void testStoredProcedureOutParameter() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        StoredProcedureQuery query = entityManager.createStoredProcedureQuery("sp_count_phones");
        query.registerStoredProcedureParameter(1, Long.class, ParameterMode.IN);
        query.registerStoredProcedureParameter(2, Long.class, ParameterMode.OUT);
        query.setParameter(1, 1L);
        query.execute();
        Long phoneCount = (Long) query.getOutputParameterValue(2);
        assertEquals(Long.valueOf(2), phoneCount);
    });
}
Also used : StoredProcedureQuery(javax.persistence.StoredProcedureQuery) Test(org.junit.Test)

Example 3 with StoredProcedureQuery

use of javax.persistence.StoredProcedureQuery in project hibernate-orm by hibernate.

the class StoreProcedureOutParameterByPositionTest method testOneBasicOutParameter.

@Test
public void testOneBasicOutParameter() {
    EntityManager em = entityManagerFactory.createEntityManager();
    em.getTransaction().begin();
    User user = new User();
    user.id = 1;
    user.name = "aName";
    em.persist(user);
    em.getTransaction().commit();
    em.clear();
    try {
        StoredProcedureQuery query = em.createNamedStoredProcedureQuery("User.findNameById");
        query.setParameter(1, 1);
        assertEquals("aName", query.getOutputParameterValue(2));
    } finally {
        em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) StoredProcedureQuery(javax.persistence.StoredProcedureQuery) NamedStoredProcedureQuery(javax.persistence.NamedStoredProcedureQuery) Test(org.junit.Test)

Example 4 with StoredProcedureQuery

use of javax.persistence.StoredProcedureQuery in project hibernate-orm by hibernate.

the class StoreProcedureOutParameterByPositionTest method testTwoBasicOutParameters.

@Test
public void testTwoBasicOutParameters() {
    EntityManager em = entityManagerFactory.createEntityManager();
    em.getTransaction().begin();
    User user = new User();
    user.id = 1;
    user.name = "aName";
    user.age = 29;
    em.persist(user);
    em.getTransaction().commit();
    em.clear();
    try {
        StoredProcedureQuery query = em.createNamedStoredProcedureQuery("User.findNameAndAgeById");
        query.setParameter(1, 1);
        assertEquals("aName", query.getOutputParameterValue(2));
        assertEquals(29, query.getOutputParameterValue(3));
    } finally {
        em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) StoredProcedureQuery(javax.persistence.StoredProcedureQuery) NamedStoredProcedureQuery(javax.persistence.NamedStoredProcedureQuery) Test(org.junit.Test)

Example 5 with StoredProcedureQuery

use of javax.persistence.StoredProcedureQuery in project hibernate-orm by hibernate.

the class StoreProcedureRefCursorOutParameterByNameTest method testNamedStoredProcedureExecution.

@Test
public void testNamedStoredProcedureExecution() {
    EntityManager em = entityManagerFactory.createEntityManager();
    try {
        StoredProcedureQuery query = em.createNamedStoredProcedureQuery("User.findByName");
        query.setParameter("USER_NAME_PARAM", "my_name");
        query.getResultList();
    } finally {
        em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) StoredProcedureQuery(javax.persistence.StoredProcedureQuery) NamedStoredProcedureQuery(javax.persistence.NamedStoredProcedureQuery) 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