Search in sources :

Example 11 with StoredProcedureQuery

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

the class JpaTckUsageTest method testMultipleGetUpdateCountCalls.

@Test
public void testMultipleGetUpdateCountCalls() {
    EntityManager em = entityManagerFactory.createEntityManager();
    em.getTransaction().begin();
    try {
        StoredProcedureQuery query = em.createStoredProcedureQuery("findOneUser");
        // this is what the TCK attempts to do, don't shoot the messenger...
        query.getUpdateCount();
        // yep, twice
        query.getUpdateCount();
    } finally {
        em.getTransaction().commit();
        em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) StoredProcedureQuery(javax.persistence.StoredProcedureQuery) Test(org.junit.Test)

Example 12 with StoredProcedureQuery

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

the class StoreProcedureOutParameterByNameTest 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("ID_PARAM", 1);
        assertEquals("aName", query.getOutputParameterValue("NAME_PARAM"));
        assertEquals(29, query.getOutputParameterValue("AGE_PARAM"));
    } finally {
        em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) StoredProcedureQuery(javax.persistence.StoredProcedureQuery) NamedStoredProcedureQuery(javax.persistence.NamedStoredProcedureQuery) Test(org.junit.Test)

Example 13 with StoredProcedureQuery

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

the class SQLServerStoredProcedureTest method testStoredProcedureOutParameter.

@Test
public void testStoredProcedureOutParameter() {
    EntityManager entityManager = createEntityManager();
    entityManager.getTransaction().begin();
    try {
        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");
        assertEquals(Long.valueOf(2), phoneCount);
    } finally {
        entityManager.getTransaction().rollback();
        entityManager.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) StoredProcedureQuery(javax.persistence.StoredProcedureQuery) Test(org.junit.Test)

Example 14 with StoredProcedureQuery

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

the class SQLServerStoredProcedureTest method testStoredProcedureRefCursor.

@Test
public void testStoredProcedureRefCursor() {
    EntityManager entityManager = createEntityManager();
    entityManager.getTransaction().begin();
    try {
        StoredProcedureQuery query = entityManager.createStoredProcedureQuery("sp_phones");
        query.registerStoredProcedureParameter(1, Long.class, ParameterMode.IN);
        query.registerStoredProcedureParameter(2, Class.class, ParameterMode.REF_CURSOR);
        query.setParameter(1, 1L);
        query.execute();
        List<Object[]> postComments = query.getResultList();
        assertNotNull(postComments);
    } catch (Exception e) {
        assertTrue(Pattern.compile("Dialect .*? not known to support REF_CURSOR parameters").matcher(e.getCause().getMessage()).matches());
    } finally {
        entityManager.getTransaction().rollback();
        entityManager.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) StoredProcedureQuery(javax.persistence.StoredProcedureQuery) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 15 with StoredProcedureQuery

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

the class MySQLStoredProcedureTest method testStoredProcedureReturnValue.

@Test
public void testStoredProcedureReturnValue() {
    EntityManager entityManager = createEntityManager();
    entityManager.getTransaction().begin();
    try {
        StoredProcedureQuery query = entityManager.createStoredProcedureQuery("sp_phones");
        query.registerStoredProcedureParameter(1, Long.class, ParameterMode.IN);
        query.setParameter(1, 1L);
        List<Object[]> personComments = query.getResultList();
        assertEquals(2, personComments.size());
    } finally {
        entityManager.getTransaction().rollback();
        entityManager.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) 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