我正在创建一个Spring库并对其进行测试,我需要具有定义的实体和存储库仅用于测试文件夹。
创建存储库时,它可以很好地工作,但是一旦向其添加自定义查询,就会出现错误:
Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List package.ExampleRepository.getAll()! No property getAll found for type Example!
这是我的测试文件夹的结构:
test
package
Application (@SpringBootApplication)
Example (@Entity)
ExampleRepository (@Repository)
Test (@SpringBootTest)
这是存储库的内容:
@Repository
public interface ExampleRepository extends JpaRepository<Example, Long> {
List<Example> getAll();
}
谢谢您的帮助
来自Spring Data JPA - Reference Documentation: Query Creation文档:
Spring数据存储库基础结构中内置的查询构建器机制对于在存储库实体上构建约束查询非常有用。该机制从方法中剥离前缀
find…By
,read…By
,query…By
,count…By
和get…By
,并开始解析其余部分。
getAll()
不适合此命名方案。 countAll()
也不是。然后您可能会问为什么findAll()
起作用,或者甚至是getOne(ID)
。 findAll()
,getOne(ID)
(以及其他类似existsById(ID)
,deleteAll()
)的方法在CrudRepository
中定义,并且可能由附加查询解析器实现解析。
CrudRepository