Search in sources :

Example 6 with StoredProcedureQuery

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

the class StoredProcedureNullParameterByNameTest method testPassNull.

@Test
public void testPassNull() {
    EntityManager em = entityManagerFactory.createEntityManager();
    em.getTransaction().begin();
    User user1 = new User();
    user1.id = 1;
    user1.name = "aName";
    user1.age = 99;
    em.persist(user1);
    em.getTransaction().commit();
    em.clear();
    try {
        StoredProcedureQuery query = em.createNamedStoredProcedureQuery("User.findNameById");
        query.setParameter("ID_PARAM", 1);
        // null is passed for EXTRA_IN_PARAM
        assertEquals("aName", query.getOutputParameterValue("NAME_PARAM"));
        assertEquals(null, query.getOutputParameterValue("EXTRA_OUT_PARAM"));
    } finally {
        em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) StoredProcedureQuery(javax.persistence.StoredProcedureQuery) NamedStoredProcedureQuery(javax.persistence.NamedStoredProcedureQuery) Test(org.junit.Test)

Example 7 with StoredProcedureQuery

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

the class DateTimeParameterTest method testBindingCalendarAsDate.

@Test
public void testBindingCalendarAsDate() {
    EntityManager em = entityManagerFactory.createEntityManager();
    em.getTransaction().begin();
    try {
        StoredProcedureQuery query = em.createStoredProcedureQuery("findMessagesByDate");
        query.registerStoredProcedureParameter(1, Calendar.class, ParameterMode.IN);
        query.setParameter(1, nowCal, TemporalType.DATE);
        List list = query.getResultList();
        assertEquals(1, list.size());
    } finally {
        em.getTransaction().rollback();
        em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) StoredProcedureQuery(javax.persistence.StoredProcedureQuery) List(java.util.List) Test(org.junit.Test)

Example 8 with StoredProcedureQuery

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

the class HSQLStoreProcedureTest method testNamedStoredProcedureExecution.

@Test
@TestForIssue(jiraKey = "HHH-10515")
public void testNamedStoredProcedureExecution() {
    EntityManager em = entityManagerFactory.createEntityManager();
    try {
        StoredProcedureQuery query = em.createNamedStoredProcedureQuery("User.inoutproc");
        query.setParameter("arg1", 1);
        query.execute();
    } finally {
        em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) StoredProcedureQuery(javax.persistence.StoredProcedureQuery) NamedStoredProcedureQuery(javax.persistence.NamedStoredProcedureQuery) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 9 with StoredProcedureQuery

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

the class JpaTckUsageTest method testExecuteUpdate.

@Test
@FailureExpected(jiraKey = "HHH-8395", message = "Out of the frying pan into the fire: https://issues.apache.org/jira/browse/DERBY-211")
public void testExecuteUpdate() {
    EntityManager em = entityManagerFactory.createEntityManager();
    em.getTransaction().begin();
    try {
        StoredProcedureQuery query = em.createStoredProcedureQuery("deleteAllUsers");
        int count = query.executeUpdate();
        // this fails because the Derby EmbeddedDriver is returning zero here rather than the actual updateCount :(
        // https://issues.apache.org/jira/browse/DERBY-211
        assertEquals(1, count);
    } finally {
        em.getTransaction().commit();
        em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) StoredProcedureQuery(javax.persistence.StoredProcedureQuery) Test(org.junit.Test) FailureExpected(org.hibernate.testing.FailureExpected)

Example 10 with StoredProcedureQuery

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

the class JpaTckUsageTest method testSettingNonExistingParams.

@Test
public void testSettingNonExistingParams() {
    EntityManager em = entityManagerFactory.createEntityManager();
    em.getTransaction().begin();
    try {
        // non-existing positional param
        try {
            StoredProcedureQuery query = em.createNamedStoredProcedureQuery("positional-param");
            query.setParameter(99, 1);
            fail("Expecting an exception");
        } catch (IllegalArgumentException expected) {
        // this is the expected condition
        }
        // non-existing named param
        try {
            StoredProcedureQuery query = em.createNamedStoredProcedureQuery("positional-param");
            query.setParameter("does-not-exist", 1);
            fail("Expecting an exception");
        } catch (IllegalArgumentException expected) {
        // this is the expected condition
        }
    } finally {
        em.getTransaction().commit();
        em.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