英文:
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<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
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<ServerAttributes, Integer>, JpaSpecificationExecutor<ServerAttributes> {
public ServerAttributes findById(int Id);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论