JPA/Criteria API - 类似 &平等问题

发布于 2024-09-28 21:45:59 字数 1340 浏览 7 评论 0原文

我正在尝试在我的新项目中使用 Criteria API:

public List<Employee> findEmps(String name) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Employee> c = cb.createQuery(Employee.class);
    Root<Employee> emp = c.from(Employee.class);
    c.select(emp);
    c.distinct(emp);
    List<Predicate> criteria = new ArrayList<Predicate>();

    if (name != null) {
        ParameterExpression<String> p = cb.parameter(String.class, "name");
        criteria.add(cb.equal(emp.get("name"), p));
    }

    /* ... */

    if (criteria.size() == 0) {
        throw new RuntimeException("no criteria");
    } else if (criteria.size() == 1) {
        c.where(criteria.get(0));
    } else {
        c.where(cb.and(criteria.toArray(new Predicate[0])));
    }

    TypedQuery<Employee> q = em.createQuery(c);

    if (name != null) {
        q.setParameter("name", name);
    }

    /* ... */

    return q.getResultList();
}

现在,当我将此行更改

            criteria.add(cb.equal(emp.get("name"), p));

为:时,

            criteria.add(cb.like(emp.get("name"), p));

我收到一条错误消息:

CriteriaBuilder类型中的(Expression, Expression)这样的方法不是>适用于参数(Path、ParameterExpression)

有什么问题?

I'm trying to use Criteria API in my new project:

public List<Employee> findEmps(String name) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Employee> c = cb.createQuery(Employee.class);
    Root<Employee> emp = c.from(Employee.class);
    c.select(emp);
    c.distinct(emp);
    List<Predicate> criteria = new ArrayList<Predicate>();

    if (name != null) {
        ParameterExpression<String> p = cb.parameter(String.class, "name");
        criteria.add(cb.equal(emp.get("name"), p));
    }

    /* ... */

    if (criteria.size() == 0) {
        throw new RuntimeException("no criteria");
    } else if (criteria.size() == 1) {
        c.where(criteria.get(0));
    } else {
        c.where(cb.and(criteria.toArray(new Predicate[0])));
    }

    TypedQuery<Employee> q = em.createQuery(c);

    if (name != null) {
        q.setParameter("name", name);
    }

    /* ... */

    return q.getResultList();
}

Now when I change this line:

            criteria.add(cb.equal(emp.get("name"), p));

to:

            criteria.add(cb.like(emp.get("name"), p));

I get an error saying:

The method like(Expression, Expression) in the type CriteriaBuilder is not > applicable for the arguments (Path, ParameterExpression)

What's the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

还在原地等你 2024-10-05 21:45:59

也许您需要

criteria.add(cb.like(emp.<String>get("name"), p));

,因为 like() 的第一个参数是 Expression,而不是 Expressionequal( )

另一种方法是启用静态元模型的生成(请参阅 JPA 实现的文档)并使用类型安全的 Criteria API:(

criteria.add(cb.like(emp.get(Employee_.name), p));

请注意,您无法从 em.getMetamodel() 获取静态元模型,您需要通过外部工具生成它)。

Perhaps you need

criteria.add(cb.like(emp.<String>get("name"), p));

because first argument of like() is Expression<String>, not Expression<?> as in equal().

Another approach is to enable generation of the static metamodel (see docs of your JPA implementation) and use typesafe Criteria API:

criteria.add(cb.like(emp.get(Employee_.name), p));

(Note that you can't get static metamodel from em.getMetamodel(), you need to generate it by external tools).

徒留西风 2024-10-05 21:45:59

更好:谓词(不是 ParameterExpression),如下所示:

List<Predicate> predicates = new ArrayList<Predicate>();
if(reference!=null){
    Predicate condition = builder.like(root.<String>get("reference"),"%"+reference+"%");
    predicates.add(condition);
}

Better: predicate (not ParameterExpression), like this :

List<Predicate> predicates = new ArrayList<Predicate>();
if(reference!=null){
    Predicate condition = builder.like(root.<String>get("reference"),"%"+reference+"%");
    predicates.add(condition);
}
小镇女孩 2024-10-05 21:45:59

它将与少量添加 .as(String.class) 一起使用:

criteria.add(cb.like(emp.get("name").as(String.class), p));

It will work with a small addition of .as(String.class):

criteria.add(cb.like(emp.get("name").as(String.class), p));
青衫儰鉨ミ守葔 2024-10-05 21:45:59

使用 :

personCriteriaQuery.where(criteriaBuilder.like(
criteriaBuilder.upper(personRoot.get(Person_.description)), 
"%"+filter.getDescription().toUpperCase()+"%")); 

Use :

personCriteriaQuery.where(criteriaBuilder.like(
criteriaBuilder.upper(personRoot.get(Person_.description)), 
"%"+filter.getDescription().toUpperCase()+"%")); 
握住我的手 2024-10-05 21:45:59

我尝试了所有的建议,虽然它没有给出错误,但如果我没有输入所有预期的文本,它不会恢复任何内容,最后 user3077341 提出的内容对我有用,但使用我使用“%”的变体而不是“%”,类似的效果很完美。

List<Predicate> predicates = new ArrayList<Predicate>();
if(reference!=null){
    Predicate condition = builder.like(root.<String>get("reference"),'%'+reference+'%');
    predicates.add(condition);
}

I tried all the proposals and although it did not give an error, it did not recover anything if I did not put all the expected text, in the end what was proposed by user3077341 worked for me but with the variant that I use '%' instead of "%", the like worked perfect.

List<Predicate> predicates = new ArrayList<Predicate>();
if(reference!=null){
    Predicate condition = builder.like(root.<String>get("reference"),'%'+reference+'%');
    predicates.add(condition);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文