Java 带有 CONTAINS 函数的 JPA Criteria api

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18488351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 02:06:12  来源:igfitidea点击:

JPA Criteria api with CONTAINS function

javajpa

提问by Peter ?ály

I'm trying to crete Criteria API query with CONTAINS function(MS SQL):

我正在尝试使用 CONTAINS 函数(MS SQL)来确定 Criteria API 查询:

select * from com.t_person where contains(last_name,'xxx')

select * from com.t_person where contains(last_name,'xxx')

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Person> cq = cb.createQuery(Person.class);
Root<Person> root = cq.from(Person.class);

Expression<Boolean> function = cb.function("CONTAINS", Boolean.class, 
root.<String>get("lastName"),cb.parameter(String.class, "containsCondition"));
cq.where(function);
TypedQuery<Person> query = em.createQuery(cq);
query.setParameter("containsCondition", lastName);
return query.getResultList();

But getting exception: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected AST node:

但得到异常:org.hibernate.hql.internal.ast.QuerySyntaxException:意外的AST节点:

Any help?

有什么帮助吗?

采纳答案by FGreg

If you want to stick with using CONTAINS, it should be something like this:

如果你想坚持使用CONTAINS,它应该是这样的:

//Get criteria builder
CriteriaBuilder cb = em.getCriteriaBuilder();
//Create the CriteriaQuery for Person object
CriteriaQuery<Person> query = cb.createQuery(Person.class);

//From clause
Root<Person> personRoot = query.from(Person.class);

//Where clause
query.where(
    cb.function(
        "CONTAINS", Boolean.class, 
        //assuming 'lastName' is the property on the Person Java object that is mapped to the last_name column on the Person table.
        personRoot.<String>get("lastName"), 
        //Add a named parameter called containsCondition
        cb.parameter(String.class, "containsCondition")));

TypedQuery<Person> tq = em.createQuery(query);
tq.setParameter("containsCondition", "%n?h%");
List<Person> people = tq.getResultList();

It seems like some of your code is missing from your question so I'm making a few assumptions in this snippet.

您的问题中似乎缺少您的某些代码,因此我在此代码段中进行了一些假设。

回答by FGreg

You could try using the CriteriaBuilder likefunction instead of the CONTAINSfunction:

您可以尝试使用 CriteriaBuilder 之类的函数而不是CONTAINS函数:

//Get criteria builder
CriteriaBuilder cb = em.getCriteriaBuilder();
//Create the CriteriaQuery for Person object
CriteriaQuery<Person> query = cb.createQuery(Person.class);

//From clause
Root<Person> personRoot = query.from(Person.class);

//Where clause
query.where(
    //Like predicate
    cb.like(
        //assuming 'lastName' is the property on the Person Java object that is mapped to the last_name column on the Person table.
        personRoot.<String>get("lastName"),
        //Add a named parameter called likeCondition
        cb.parameter(String.class, "likeCondition")));

TypedQuery<Person> tq = em.createQuery(query);
tq.setParameter("likeCondition", "%Doe%");
List<Person> people = tq.getResultList();

This should result in a query similar to:

这应该会产生类似于以下内容的查询:

select p from PERSON p where p.last_name like '%Doe%';