Java 带有CONTAINS函数的JPA标准api

Java 带有CONTAINS函数的JPA标准api,java,jpa,Java,Jpa,我正在尝试使用CONTAINS函数(MS SQL)创建API查询: 从com.t\u person中选择*,其中包含(姓氏'xxx') CriteriaBuilder cb=em.getCriteriaBuilder(); CriteriaQuery cq=cb.createQuery(Person.class); Root=cq.from(Person.class); Expression function=cb.function(“包含”),Boolean.class, root.get(“

我正在尝试使用CONTAINS函数(MS SQL)创建API查询:

从com.t\u person中选择*,其中包含(姓氏'xxx')

CriteriaBuilder cb=em.getCriteriaBuilder();
CriteriaQuery cq=cb.createQuery(Person.class);
Root=cq.from(Person.class);
Expression function=cb.function(“包含”),Boolean.class,
root.get(“lastName”)、cb.parameter(String.class、“containsCondition”);
cq.where(函数);
TypedQuery=em.createQuery(cq);
setParameter(“containsCondition”,lastName);
返回query.getResultList();
但有一个例外: org.hibernate.hql.internal.ast.QuerySyntaxException:意外的ast节点:


有什么帮助吗?

您可以尝试使用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();

如果您想继续使用
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();
//获取条件生成器
CriteriaBuilder cb=em.getCriteriaBuilder();
//为Person对象创建CriteriaQuery
CriteriaQuery=cb.createQuery(Person.class);
//From子句
Root personRoot=query.from(Person.class);
//Where子句
请问,在哪里(
cb.功能(
“包含”,Boolean.class,
//假设“lastName”是映射到Person表上last_name列的Person Java对象的属性。
personRoot.get(“lastName”),
//添加名为containsCondition的命名参数
参数(String.class,“containsCondition”);
TypedQuery tq=em.createQuery(查询);
tq.setParameter(“containsCondition”,“näh%”);
List people=tq.getResultList();

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

您使用的hibernate版本是什么?与JBOSS捆绑的版本。准确地说,不使用HibernateAPI,只使用JPA。谢谢。但我希望能够实现高级搜索,比如不区分重音的搜索(näh-will find-nah)。它只支持周围的通配符,即:%doe%,但不支持%doeI Get QuerySyntaxException:意外的AST节点:(靠近第1行第109列[从sk.insdata.cbn.business.client.entities.Person中选择GenerateDias0作为GenerateDias0,其中包含(GenerateDias0.lastName,:param0)]@petersaly检查我的编辑…我想你实际上也需要一个cb.equal。我一开始尝试过的:org.hibernate.exception.sqlgrammareexception:如果你只是在数据库上运行该查询,那么“=”附近的语法不正确:
从sk.insdata.cbn.business.client.entications.Person中选择generatedAlias0作为generatedAlias0,其中包含(generatedAlias0.lastName,:param0)
它能工作吗?是运行本机查询工作:从com.t_person中选择*from contains(姓氏,“%saly%”)
select p from PERSON p where p.last_name like '%Doe%';
//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();