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

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

问题描述

我正在我的应用程序中实现搜索功能.我在 findAll() 中使用 Specification 并且它运行良好.但是,当我尝试使用 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 并将两者与和结合起来.

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天全站免登陆