Search in sources :

Example 21 with Query

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) {
    };
}
Also used : Query(org.hibernate.query.Query) AbstractQueryFacade(org.jboss.tools.hibernate.runtime.common.AbstractQueryFacade) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 22 with Query

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();
}
Also used : Query(org.hibernate.query.Query) List(java.util.List) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 23 with Query

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));
    });
}
Also used : Query(org.hibernate.query.Query) Test(org.junit.Test)

Example 24 with Query

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());
        }
    });
}
Also used : Query(org.hibernate.query.Query) QuerySyntaxException(org.hibernate.hql.internal.ast.QuerySyntaxException) ArrayList(java.util.ArrayList) Test(org.junit.Test) AbstractJPATest(org.hibernate.test.jpa.AbstractJPATest) TestForIssue(org.hibernate.testing.TestForIssue)

Example 25 with Query

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();
    });
}
Also used : Query(org.hibernate.query.Query) ArrayList(java.util.ArrayList) Test(org.junit.Test) AbstractJPATest(org.hibernate.test.jpa.AbstractJPATest) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

Query (org.hibernate.query.Query)157 Test (org.junit.Test)44 Session (org.hibernate.Session)39 List (java.util.List)24 ArrayList (java.util.ArrayList)19 TestForIssue (org.hibernate.testing.TestForIssue)19 ObjectQuery (com.evolveum.midpoint.prism.query.ObjectQuery)17 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)17 HashMap (java.util.HashMap)15 Map (java.util.Map)14 NativeQuery (org.hibernate.query.NativeQuery)14 AbstractJPATest (org.hibernate.test.jpa.AbstractJPATest)11 SessionFactory (org.hibernate.SessionFactory)9 OperationResult (com.evolveum.midpoint.schema.result.OperationResult)7 InvocationHandler (java.lang.reflect.InvocationHandler)7 Method (java.lang.reflect.Method)7 SQLException (java.sql.SQLException)7 Collectors (java.util.stream.Collectors)7 AbstractQueryFacade (org.jboss.tools.hibernate.runtime.common.AbstractQueryFacade)7 RQuery (com.evolveum.midpoint.repo.sql.query.RQuery)6