我正在创建一个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-参考文档:查询创建文档:
Spring Data存储库基础设施中内置的查询生成器机制对于在存储库的实体上构建约束查询非常有用。该机制从方法中剥离前缀
find…By
、read…By
、query…By
、count…By
和get…By
,并开始解析其余部分
getAll()
不适合此命名方案。countAll()
就不那么好了。你可能会问findAll()
为什么有效,甚至getOne(ID)
为什么有效。CrudRepository
中定义了findAll()
、getOne(ID)
(以及其他类似的existsById(ID)
、deleteAll()
(方法,并且可能由其他查询解析器实现来解决。
public interface ExampleRepository extends JpaRepository<Example, Long> {
// valid (resolvable)
List<Example> findAll();
//invalid (un-resolvable)
List<Example> getAll();
// valid
Example getOne(Long id);
// valid
List<Example> getByName(String name);
// invalid
long countAll();
//valid
long countByName(String name);
}