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