Spring数据JPA – 规范

huangapple go评论96阅读模式
英文:

Spring data JPA - specification

问题

我在调用repository.findAll()时遇到了问题

版本:spring-boot-starter-data-jpa-2.2.4.RELEASE

Repository

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

错误:

无法解析方法'findAll(com.cloud.compareService.specification.ServerAttributeSpecification)'

我在我的JpaRepository中找不到findAll(Specification<T> spec)

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

英文:

I'm facing issues when passing specification to the repository.findAll()

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

Repository

@Repository
public interface ServerAttributesRepository extends JpaRepository&lt;ServerAttributes, Integer&gt;{
    public ServerAttributes findById(int Id);
}

and when I call repository.findAll by passing specification, I get error

ServerAttributeSpecification spec1 = new ServerAttributeSpecification(
            new SearchCriteria(&quot;server&quot;, SearchOperation.EQUALITY, &quot;1&quot;));
    List&lt;ServerAttributes&gt; serverAttributesList = serverAttributesRepository.findAll(spec1);

Error:

Cannot resolve method &#39;findAll(com.cloud.compareService.specification.ServerAttributeSpecification)&#39;

and I can not find findAll(Specification&lt;T&gt; spec) in my JpaRepository

Spring数据JPA – 规范

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

答案1

得分: 1

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

You need to extend JpaSpecificationExecutor interface to get support for Specifications, so try:

@Repository
public interface ServerAttributesRepository extends JpaRepository&lt;ServerAttributes, Integer&gt;, JpaSpecificationExecutor&lt;ServerAttributes&gt; {
    public ServerAttributes findById(int Id);
}

huangapple
  • 本文由 发表于 2020年9月28日 09:55:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/64095023.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定