如何在JPA中使用规范 [英] How to use Specification in JPA

查看:79
本文介绍了如何在JPA中使用规范的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的应用程序中实现搜索功能.我在 findAll()中使用规范,它运行良好.但是,当我尝试使用 findByFirstName()之类的其他方法时,它不起作用

I am implementing search functionality in my application. I am using Specification in findAll() and it is working perfectly. But when ever i am trying to achive in other methods like findByFirstName() it is not working

我将包括我到目前为止所做的事情.

AircraftSpecification.java

public class AircraftSpecification {

    private AircraftSpecification() {}

    public static Specification<Aircraft> textInAllColumns(String text) {

        if (!text.contains("%")) {
            text = "%"+text+"%";
        }
        final String finalText = text;

        return new Specification<Aircraft>() {
            private static final long serialVersionUID = 1L;

            @Override
            public Predicate toPredicate(Root<Aircraft> root, CriteriaQuery<?> cq, CriteriaBuilder builder) {
                  List<SingularAttribute<Aircraft, ?>> tempAttributes = new ArrayList<>();
                  for (SingularAttribute<Aircraft, ?> attribute : root.getModel().getDeclaredSingularAttributes()) {
                      if (attribute.getJavaType().getSimpleName().equalsIgnoreCase("string")) {
                          tempAttributes.add(attribute);
                      }
                  }

                  final Predicate[] predicates = new Predicate[tempAttributes.size()];

                  for (int i = 0; i < tempAttributes.size(); i++) {
                      predicates[i] = builder.like(builder.lower(root.get(tempAttributes.get(i).getName())), finalText.toLowerCase());
                  }
                  return builder.or(predicates);
              }
        };
    }
}

我打电话时

aircraftRepository.findAll(Specification.where(AircraftSpecification.textInAllColumns(searchText)));

它给了我适当的数据.

但是当我打电话

aircraftRepository.findAllByName(name, Specification.where(AircraftSpecification.textInAllColumns(searchText)));

抛出异常.

例外是:

org.springframework.dao.InvalidDataAccessApiUsageException: At least 2 parameter(s) provided but only 1 parameter(s) present in query.; nested exception is java.lang.IllegalArgumentException: At least 2 parameter(s) provided but only 1 parameter(s) present in query.

除了findAll方法之外,任何人都可以帮助我如何使用规范.

Can any one help me how to use Specification other than findAll method.

推荐答案

在Spring Data从带有 Specification 的方法名称派生要执行的查询的情况下,您不能组合派生的查询.只需将查询的名称部分也设置为 Specification ,然后将其与and结合使用即可.

You can't combine derived queries where Spring Data derives the query to execute from the method name with Specification. Just make the name part of a query a Specification as well and combine the two with and.

产生的呼叫可能看起来像这样或类似:

The resulting call could look like this or similar:

aircraftRepository.findAll(
        byName("Alfred")
       .and(textInAllColumns(searchText))
);

这篇关于如何在JPA中使用规范的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆