Search in sources :

Example 1 with Parameter

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

the class QueryTest method testNullNamedParameterParameterIncompatible.

@Test
@SkipForDialect(value = SybaseDialect.class, comment = "Null == null on Sybase")
public void testNullNamedParameterParameterIncompatible() 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=:iVal");
        Parameter p = new Parameter() {

            @Override
            public String getName() {
                return "iVal";
            }

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

            @Override
            public Class getParameterType() {
                return Long.class;
            }
        };
        q.setParameter(p, null);
        List results = q.getResultList();
        // null != null
        assertEquals(0, results.size());
        q = em.createQuery("from Item i where i.intVal is null and :iVal 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 = :iVal");
        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 2 with Parameter

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

the class H2StoreProcedureTest method testStoreProcedureGetParameterByPosition.

@Test
public void testStoreProcedureGetParameterByPosition() {
    final EntityManager entityManager = getOrCreateEntityManager();
    try {
        StoredProcedureQuery query = entityManager.createStoredProcedureQuery("by_Id", MyEntity.class);
        query.registerStoredProcedureParameter(1, Long.class, ParameterMode.IN);
        query.setParameter(1, 1L);
        final List resultList = query.getResultList();
        assertThat(resultList.size(), is(1));
        final Set<Parameter<?>> parameters = query.getParameters();
        assertThat(parameters.size(), is(1));
        final Parameter<?> parameter = query.getParameter(1);
        assertThat(parameter, not(nullValue()));
        try {
            query.getParameter(2);
            fail("IllegalArgumentException expected, parameter at position 2 does not exist");
        } catch (IllegalArgumentException iae) {
        //expected
        }
    } 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 3 with Parameter

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

the class HSQLStoreProcedureTest method testGetPositionalParameters.

@Test
@TestForIssue(jiraKey = "HHH-10915")
public void testGetPositionalParameters() {
    EntityManager em = entityManagerFactory.createEntityManager();
    try {
        StoredProcedureQuery query = em.createNamedStoredProcedureQuery("User.inoutproc");
        final Set<Parameter<?>> parameters = query.getParameters();
        assertThat(parameters.size(), is(2));
        try {
            query.getParameter(1);
            fail("An IllegalArgumentException is expected, The stored procedure has named parameters not positional");
        } catch (IllegalArgumentException iae) {
        //expected
        }
        try {
            query.getParameter(1, String.class);
            fail("An IllegalArgumentException is expected, The stored procedure has named parameters not positional");
        } 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 4 with Parameter

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

the class QueryTest method testPositionalParameterWithUserError.

@Test
public void testPositionalParameterWithUserError() throws Exception {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    try {
        Wallet w = new Wallet();
        w.setBrand("Lacoste");
        w.setModel("Minimic");
        w.setSerial("0100202002");
        em.persist(w);
        em.flush();
        // using jpa-style, position index should match syntax '?<position'.
        Query jpaQuery = em.createQuery("select w from Wallet w where w.brand = ?1 and w.model = ?3");
        jpaQuery.setParameter(1, "Lacoste");
        try {
            jpaQuery.setParameter(2, "Expensive");
            fail("Should fail due to a user error in parameters");
        } catch (Exception e) {
            assertTyping(IllegalArgumentException.class, e);
        }
        // using jpa-style, position index specified not in query - test exception type
        jpaQuery = em.createQuery("select w from Wallet w ");
        try {
            Parameter parameter = jpaQuery.getParameter(1);
            fail("Should fail due to a user error in parameters");
        } catch (Exception e) {
            assertTyping(IllegalArgumentException.class, e);
        }
        // using jpa-style, position index specified not in query - test exception type
        jpaQuery = em.createQuery("select w from Wallet w");
        try {
            Parameter<Integer> parameter = jpaQuery.getParameter(1, Integer.class);
            fail("Should fail due to user error in parameters");
        } catch (Exception e) {
            assertTyping(IllegalArgumentException.class, e);
        }
        // using hql-style, should be 0-based
        Query hqlQuery = em.createQuery("select w from Wallet w where w.brand = ? and w.model = ?");
        try {
            hqlQuery.setParameter(1, "Lacoste");
            hqlQuery.setParameter(2, "Expensive");
            fail("Should fail due to a user error in parameters");
        } catch (Exception e) {
            assertTyping(IllegalArgumentException.class, e);
        }
    } finally {
        if (em.getTransaction() != null && em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) Wallet(org.hibernate.jpa.test.Wallet) Parameter(javax.persistence.Parameter) NoResultException(javax.persistence.NoResultException) PersistenceException(javax.persistence.PersistenceException) Test(org.junit.Test)

Example 5 with Parameter

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

the class QueryTest method testNullNamedParameterParameter.

@Test
@SkipForDialect(value = SybaseDialect.class, comment = "Null == null on Sybase")
public void testNullNamedParameterParameter() 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=:iVal");
        Parameter p = new Parameter() {

            @Override
            public String getName() {
                return "iVal";
            }

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

            @Override
            public Class getParameterType() {
                return Integer.class;
            }
        };
        q.setParameter(p, null);
        List results = q.getResultList();
        // null != null
        assertEquals(0, results.size());
        q = em.createQuery("from Item i where i.intVal is null and :iVal 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 = :iVal");
        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)

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