简体   繁体   English

如何在 JPA 中使用规范

[英]How to use Specification in JPA

I am implementing search functionality in my application.我正在我的应用程序中实现搜索功能。 I am using Specification in findAll() and it is working perfectly.我在findAll()使用 Specification 并且它运行良好。 But when ever i am trying to achive in other methods like findByFirstName() it is not working但是当我尝试使用findByFirstName()等其他方法时,它不起作用

I am including what i did so far.我包括我到目前为止所做的。

AircraftSpecification.java飞机规格.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);
              }
        };
    }
}

When i am calling当我打电话时

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

it giving me proper data.它给了我正确的数据。

But when i am calling但是当我打电话时

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

It throwing Exception.它抛出异常。

Exception Is:例外是:

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.

Can any one help me how to use Specification other than findAll method.任何人都可以帮助我如何使用除 findAll 方法以外的规范。

You can't combine derived queries where Spring Data derives the query to execute from the method name with Specification .您不能将 Spring Data 从方法名称派生出要执行的查询与Specification结合使用的派生查询。 Just make the name part of a query a Specification as well and combine the two with and.只需将查询的名称部分也设为Specification ,并将两者与和结合起来。

The resulting call could look like this or similar:结果调用可能如下所示或类似:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM