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