Spring 数据 JPA - 规范
Spring data JPA - specification
我在将规范传递给 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<T> spec)
参考:https://www.baeldung.com/rest-api-query-search-language-more-operations
您需要扩展 JpaSpecificationExecutor
接口以获得对规范的支持,因此尝试:
@Repository
public interface ServerAttributesRepository extends JpaRepository<ServerAttributes, Integer>, JpaSpecificationExecutor<ServerAttributes> {
public ServerAttributes findById(int Id);
}
我在将规范传递给 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<T> spec)
参考:https://www.baeldung.com/rest-api-query-search-language-more-operations
您需要扩展 JpaSpecificationExecutor
接口以获得对规范的支持,因此尝试:
@Repository
public interface ServerAttributesRepository extends JpaRepository<ServerAttributes, Integer>, JpaSpecificationExecutor<ServerAttributes> {
public ServerAttributes findById(int Id);
}