Search in sources :

Example 11 with Parameter

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

the class H2StoreProcedureTest method testStoreProcedureGetParameters.

@Test
public void testStoreProcedureGetParameters() {
    final EntityManager entityManager = getOrCreateEntityManager();
    try {
        StoredProcedureQuery query = entityManager.createStoredProcedureQuery("get_all_entities", MyEntity.class);
        final Set<Parameter<?>> parameters = query.getParameters();
        assertThat(parameters.size(), is(0));
        final List resultList = query.getResultList();
        assertThat(resultList.size(), is(1));
    } finally {
        entityManager.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) StoredProcedureQuery(javax.persistence.StoredProcedureQuery) Parameter(javax.persistence.Parameter) List(java.util.List) Test(org.junit.Test)

Example 12 with Parameter

use of javax.persistence.Parameter 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 13 with Parameter

use of javax.persistence.Parameter 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 14 with Parameter

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

the class QueryTest method testNullPositionalParameterParameterIncompatible.

@Test
@SkipForDialect(value = SybaseDialect.class, comment = "Null == null on Sybase")
public void testNullPositionalParameterParameterIncompatible() throws Exception {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    try {
        Item item = new Item("Mouse", "Micro$oft mouse");
        em.persist(item);
        Query q = em.createQuery("from Item i where i.intVal=?");
        Parameter p = new Parameter() {

            @Override
            public String getName() {
                return null;
            }

            @Override
            public Integer getPosition() {
                return 0;
            }

            @Override
            public Class getParameterType() {
                return Long.class;
            }
        };
        q.setParameter(p, null);
        Parameter pGotten = q.getParameter(p.getPosition());
        List results = q.getResultList();
        // null != null
        assertEquals(0, results.size());
        q = em.createQuery("from Item i where i.intVal is null and ? is null");
        q.setParameter(p, null);
        results = q.getResultList();
        assertEquals(1, results.size());
        q = em.createQuery("from Item i where i.intVal is null or i.intVal = ?");
        q.setParameter(p, null);
        results = q.getResultList();
        assertEquals(1, results.size());
    } finally {
        if (em.getTransaction() != null && em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }
}
Also used : Item(org.hibernate.jpa.test.Item) EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) Parameter(javax.persistence.Parameter) ArrayList(java.util.ArrayList) List(java.util.List) SkipForDialect(org.hibernate.testing.SkipForDialect) Test(org.junit.Test)

Example 15 with Parameter

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

the class CriteriaQueryTypeQueryAdapter method getParameter.

@Override
@SuppressWarnings({ "unchecked" })
public <T> Parameter<T> getParameter(String name, Class<T> type) {
    entityManager.checkOpen(false);
    Parameter parameter = locateParameterByName(name);
    if (type.isAssignableFrom(parameter.getParameterType())) {
        return parameter;
    }
    throw new IllegalArgumentException("Named parameter [" + name + "] type is not assignanle to request type [" + type.getName() + "]");
}
Also used : Parameter(javax.persistence.Parameter) QueryParameter(org.hibernate.query.QueryParameter)

Aggregations

Parameter (javax.persistence.Parameter)18 EntityManager (javax.persistence.EntityManager)16 Test (org.junit.Test)15 List (java.util.List)9 Query (javax.persistence.Query)9 ArrayList (java.util.ArrayList)7 Item (org.hibernate.jpa.test.Item)6 SkipForDialect (org.hibernate.testing.SkipForDialect)6 StoredProcedureQuery (javax.persistence.StoredProcedureQuery)5 TestForIssue (org.hibernate.testing.TestForIssue)5 NamedStoredProcedureQuery (javax.persistence.NamedStoredProcedureQuery)3 NoResultException (javax.persistence.NoResultException)3 PersistenceException (javax.persistence.PersistenceException)3 StoredProcedureParameter (javax.persistence.StoredProcedureParameter)3 Map (java.util.Map)2 Wallet (org.hibernate.jpa.test.Wallet)2 ParamNotSetException (com.querydsl.core.types.ParamNotSetException)1 Date (java.util.Date)1 GregorianCalendar (java.util.GregorianCalendar)1 LinkedHashMap (java.util.LinkedHashMap)1