use of org.hibernate.query.Query in project jbosstools-hibernate by jbosstools.
the class QueryFacadeTest method beforeEach.
@BeforeEach
public void beforeEach() {
Query<?> queryProxy = (Query<?>) Proxy.newProxyInstance(FACADE_FACTORY.getClassLoader(), new Class[] { Query.class }, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
methodName = method.getName();
arguments = args;
if ("list".equals(method.getName())) {
return LIST;
} else if ("getReturnAliases".equals(method.getName())) {
return RETURN_ALIASES;
} else if ("getReturnTypes".equals(method.getName())) {
return RETURN_TYPES;
} else
return null;
}
});
query = new AbstractQueryFacade(FACADE_FACTORY, queryProxy) {
};
}
use of org.hibernate.query.Query in project hibernate-orm by hibernate.
the class QueryHintTest method testQueryHintAndComment.
@Test
@TestForIssue(jiraKey = "HHH-12362")
public void testQueryHintAndComment() {
connectionProvider.clear();
doInHibernate(this::sessionFactory, s -> {
Query query = s.createQuery("FROM QueryHintTest$Employee e WHERE e.department.name = :departmentName").addQueryHint("ALL_ROWS").setComment("My_Query").setParameter("departmentName", "Sales");
List results = query.list();
assertEquals(results.size(), 2);
});
assertEquals(1, connectionProvider.getPreparedStatements().size());
assertNotNull(connectionProvider.getPreparedSQLStatements().get(0).contains("/* My_Query */ select /*+ ALL_ROWS */"));
connectionProvider.clear();
}
use of org.hibernate.query.Query in project hibernate-orm by hibernate.
the class CollectionMapWithComponentValueTest method testMapKeyExpressionInWhere.
@Test
public void testMapKeyExpressionInWhere() {
doInHibernate(this::sessionFactory, s -> {
// JPA form
Query query = s.createQuery("select te from TestEntity te join te.values v where ?1 in (key(v)) ");
query.setParameter(1, keyValue);
assertThat(query.list().size(), is(1));
// Hibernate additional form
query = s.createQuery("select te from TestEntity te where ?1 in (key(te.values))");
query.setParameter(1, keyValue);
assertThat(query.list().size(), is(1));
// Test key property dereference
query = s.createQuery("select te from TestEntity te join te.values v where key(v).name in :names");
query.setParameterList("names", Arrays.asList(keyValue.name));
assertThat(query.list().size(), is(1));
});
}
use of org.hibernate.query.Query in project hibernate-orm by hibernate.
the class JPAQLComplianceTest method testParametersMixtureNamedCollectionAndPositional.
/**
* Positional collection parameter is expanded to the list of named parameters. In spite of this fact, initial query
* query is wrong in terms of JPA and exception must be thrown
*/
@Test
@TestForIssue(jiraKey = "HHH-12290")
public void testParametersMixtureNamedCollectionAndPositional() {
TransactionUtil2.inTransaction(sessionFactory(), s -> {
try {
Query q = s.createQuery("select item from Item item where item.id in (?1) and item.name = :name");
List<Long> params = new ArrayList<>();
params.add(0L);
params.add(1L);
q.setParameter(1, params);
q.setParameter("name", "name");
q.list();
fail("Expecting QuerySyntaxException because of named and positional parameters mixture");
} catch (IllegalArgumentException e) {
assertNotNull(e.getCause());
assertTyping(QuerySyntaxException.class, e.getCause());
}
});
}
use of org.hibernate.query.Query in project hibernate-orm by hibernate.
the class JPAQLComplianceTest method testReusedNamedCollectionParam.
@Test
@TestForIssue(jiraKey = "HHH-12290")
public void testReusedNamedCollectionParam() {
TransactionUtil2.inTransaction(sessionFactory(), session -> {
Query q = session.createQuery("select e from MyEntity e where e.surname in (:values) or e.name in (:values)");
List<String> params = new ArrayList<>();
params.add("name");
params.add("other");
q.setParameter("values", params);
q.list();
});
}
Aggregations