JpaRepository&lt;Telefonbuch,Long&gt;类型中的findAll(Sort)方法.不适用于参数(规范<Telefonbuch>) [英] The method findAll(Sort) in the type JpaRepository&lt;Telefonbuch,Long&gt; is not applicable for the arguments (Specification&lt;Telefonbuch&gt;)

查看:13
本文介绍了JpaRepository&lt;Telefonbuch,Long&gt;类型中的findAll(Sort)方法.不适用于参数(规范<Telefonbuch>)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现 Specifications 并想使用 findAll(Specification spec) 方法,但总是在我插入一个规范时 Eclipse 告诉我:>

I want to implement Specifications and want to use the findAll(Specification<T> spec) method, but always when I insert an Specification Eclipse tells me:

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

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

I don't want to use Sort. I hand over a specification so why does it always try to use the method with sort?

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

You can see here that the method is a suggestion by Eclipse: https://imgur.com/a/LuF6ZGK

规格:

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

电话簿规格:

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

TelefonbuchRepository:

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));
        }

它告诉我:JpaRepository类型中的findAll(Sort)方法不适用于参数(规范)

推荐答案

您的存储库还需要实现 JpaSpecificationExecutor 以查看 findAll(Specification) 方法.

Your repository additionally needs to implement JpaSpecificationExecutor to see the findAll(Specification<T>) method.

另外,请注意,我们不建议首先扩展 JpaRepository 而是使用一个不太公开的接口 CrudRepositoryPagingAndSortingRepository.请参阅此答案参考.

Also, note that we don't recommend to extend JpaRepository in the first place but rather use one of the less revealing interfaces CrudRepository or PagingAndSortingRepository. See this answer for reference.

这篇关于JpaRepository&lt;Telefonbuch,Long&gt;类型中的findAll(Sort)方法.不适用于参数(规范<Telefonbuch>)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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