【问题标题】:The method findAll(Sort) in the type JpaRepository<Telefonbuch,Long> is not applicable for the arguments (Specification<Telefonbuch>)JpaRepository<Telefonbuch,Long> 类型中的方法 findAll(Sort) 不适用于参数 (Specification<Telefonbuch>)
【发布时间】:2019-01-09 12:32:46
【问题描述】:

我想实现Specifications 并想使用findAll(Specification&lt;T&gt; spec) 方法,但是当我插入规范时,Eclipse 总是告诉我:

The method findAll(Sort) in the type JpaRepository<Telefonbuch,Long> is not applicable for the arguments (Specification<Telefonbuch>)

我不想使用Sort。我交出了一个规范,为什么它总是尝试使用带有排序的方法?

您可以在这里看到该方法是 Eclipse 的建议: https://imgur.com/a/LuF6ZGK

规格:

public interface Specification<T> {
    Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder);
}

Telefonbuch 规格:

public static Specification<Telefonbuch> hasVorname(String vorname) {
    return (root, query, cb) -> {
        return cb.equal(root.get(Telefonbuch_.vorname), "%"+vorname.toLowerCase()+"%");
    };
}

TelefonbuchRepository:

public interface TelefonbuchRepository extends JpaRepository<Telefonbuch, Long>, JpaSpecificationExecutor<Telefonbuch> {

搜索控制器:

public void search(String vorname, String nachname, String telefonnummer, String handynummer) {  
    if (!vorname.isEmpty()) {   
        List<Telefonbuch> list = telefonbuchRepository.findAll(TelefonbuchSpecifications.hasVorname(vorname));
    }

这里是

List<Telefonbuch> list = telefonbuchRepository.findAll(TelefonbuchSpecifications.hasVorname(vorname));
        }

它告诉我: The method findAll(Sort) in the type JpaRepository&lt;Telefonbuch,Long&gt; is not applicable for the arguments (Specification&lt;Telefonbuch&gt;)

【问题讨论】:

    标签: java spring-data-jpa spring-data specifications


    【解决方案1】:

    findAll 完全按照它所说的去做:它找到了所有种可能性。因此,您尝试做的事情没有意义(按名字过滤)。

    findAll 基本上翻译成SELECT * FROM TABLE

    你需要在你的存储库中声明一些东西来执行你正在寻找的查询,我假设它是SELECT * FROM TABLE WHERE &lt;CONDITION&gt;

    作为引用自the spring docs 的示例,将以下内容描述为指导方针:

    存储库:

    public interface PersonRepository extends Repository<User, Long> { 
        List<Person> findByLastname(String lastname);
    }
    

    应用:

    public class SomeClient {
    
      @Autowired
      private PersonRepository repository;
    
      public void doSomething() {
        List<Person> persons = repository.findByLastname("Matthews");
      }
    }
    

    您需要定义一个符合您所需条件的查询,而不是尝试在findAll 快捷方式上强制一个条件。

    【讨论】:

    • 你肯定不对。 findAll(Specification) 为我提供了给定规范的所有条目,但不知何故它不起作用。如果 Spring 本身提供了一个教程,你不能告诉我我在做什么是错误的:spring.io/blog/2011/04/26/… 是的,可以创建自己的 findByMethod,但是这个给了我一个 IllegalArgumentException:stackoverflow.com/questions/54077381/…
    【解决方案2】:

    您的存储库还需要实现JpaSpecificationExecutor 才能看到findAll(Specification&lt;T&gt;) 方法。

    另外,请注意,我们不建议一开始就扩展JpaRepository,而是使用不那么显眼的接口CrudRepositoryPagingAndSortingRepository。参考this answer

    【讨论】:

    • 没有必要冒犯。声明在可见区域之外。如果你有它,你需要检查你的 IDE 设置。我已经看到 Eclipse 提出了已添加到不同文件中的类型的类型方法,但随后抱怨该方法不可用,因为其他文件尚未保存。可能值得检查。除此之外:¯\_(ツ)_/¯
    • 我应该如何找到或知道在 Eclipse 中设置什么?我没有改变任何东西。刚刚安装好了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-26
    • 2020-12-31
    • 2018-04-15
    相关资源
    最近更新 更多