【问题标题】:Spring data JPA - specificationSpring 数据 JPA - 规范
【发布时间】:2020-09-28 01:55:30
【问题描述】:

将规范传递给 repository.findAll() 时遇到问题

Version:spring-boot-starter-data-jpa-2.2.4.RELEASE

存储库

@Repository
public interface ServerAttributesRepository extends JpaRepository<ServerAttributes, Integer>{
    public ServerAttributes findById(int Id);
}

当我通过传递规范调用 repository.findAll 时,我得到错误

ServerAttributeSpecification spec1 = new ServerAttributeSpecification(
            new SearchCriteria("server", SearchOperation.EQUALITY, "1"));
    List<ServerAttributes> serverAttributesList = serverAttributesRepository.findAll(spec1);

错误:

Cannot resolve method 'findAll(com.cloud.compareService.specification.ServerAttributeSpecification)'

我在我的 JpaRepository 中找不到 findAll(Specification&lt;T&gt; spec)

参考:https://www.baeldung.com/rest-api-query-search-language-more-operations

【问题讨论】:

  • 请不要包含代码截图。

标签: java spring spring-data spring-repositories


【解决方案1】:

您需要扩展JpaSpecificationExecutor 接口以获得对规范的支持,因此请尝试:

@Repository
public interface ServerAttributesRepository extends JpaRepository<ServerAttributes, Integer>, JpaSpecificationExecutor<ServerAttributes> {
    public ServerAttributes findById(int Id);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-22
    • 2018-11-01
    • 1970-01-01
    • 2016-02-17
    • 2017-01-12
    • 2019-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多