Spring data JPA - specification

707 Views Asked by At

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<ServerAttributes, Integer>{
    public ServerAttributes findById(int Id);
}

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

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

Error:

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

and I can not find findAll(Specification<T> spec) in my JpaRepository

enter image description here

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

1

There are 1 best solutions below

0
On BEST ANSWER

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

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