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();
}
}
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();
}
}
Aggregations