Search in sources :

Example 6 with Wallet

use of org.hibernate.jpa.test.Wallet in project hibernate-orm by hibernate.

the class QueryTest method testExplicitPositionalParameter.

@Test
public void testExplicitPositionalParameter() 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.getTransaction().commit();
        em.getTransaction().begin();
        Query query = em.createQuery("select w from " + Wallet.class.getName() + " w where w.brand in ?1");
        List brands = new ArrayList();
        brands.add("Lacoste");
        query.setParameter(1, brands);
        w = (Wallet) query.getSingleResult();
        assertNotNull(w);
        query = em.createQuery("select w from " + Wallet.class.getName() + " w where w.marketEntrance = ?1");
        query.setParameter(1, new Date(), TemporalType.DATE);
        //assertNull( query.getSingleResult() );
        assertEquals(0, query.getResultList().size());
        em.remove(w);
        em.getTransaction().commit();
    } catch (Exception e) {
        if (em.getTransaction() != null && em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        throw e;
    } finally {
        em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) Wallet(org.hibernate.jpa.test.Wallet) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Date(java.util.Date) NoResultException(javax.persistence.NoResultException) PersistenceException(javax.persistence.PersistenceException) Test(org.junit.Test)

Example 7 with Wallet

use of org.hibernate.jpa.test.Wallet in project hibernate-orm by hibernate.

the class QueryTest method testPositionalParameterForms.

@Test
public void testPositionalParameterForms() 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.getTransaction().commit();
        em.getTransaction().begin();
        // first using jpa-style positional parameter
        Query query = em.createQuery("select w from Wallet w where w.brand = ?1");
        query.setParameter(1, "Lacoste");
        w = (Wallet) query.getSingleResult();
        assertNotNull(w);
        // next using jpa-style positional parameter, but as a name (which is how Hibernate core treats these
        query = em.createQuery("select w from Wallet w where w.brand = ?1");
        // deprecated usage of positional parameter by String
        query.setParameter("1", "Lacoste");
        w = (Wallet) query.getSingleResult();
        assertNotNull(w);
        // finally using hql-style positional parameter
        query = em.createQuery("select w from Wallet w where w.brand = ?");
        query.setParameter(0, "Lacoste");
        w = (Wallet) query.getSingleResult();
        assertNotNull(w);
        em.remove(w);
        em.getTransaction().commit();
    } catch (Exception e) {
        if (em.getTransaction() != null && em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        throw e;
    } finally {
        em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) Wallet(org.hibernate.jpa.test.Wallet) NoResultException(javax.persistence.NoResultException) PersistenceException(javax.persistence.PersistenceException) Test(org.junit.Test)

Aggregations

EntityManager (javax.persistence.EntityManager)7 Wallet (org.hibernate.jpa.test.Wallet)7 Test (org.junit.Test)7 NoResultException (javax.persistence.NoResultException)5 PersistenceException (javax.persistence.PersistenceException)5 Query (javax.persistence.Query)5 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 List (java.util.List)1 Parameter (javax.persistence.Parameter)1 TestForIssue (org.hibernate.testing.TestForIssue)1