问题描述
我正在尝试实现一个自定义 Spring 存储库.我有接口:
I'm trying to implement a custom Spring repository. I have the interface:
public interface FilterRepositoryCustom {
List<User> filterBy(String role);
}
实施:
public class FilterRepositoryImpl implements FilterRepositoryCustom {
...
}
和主"存储库,扩展我的自定义存储库:
and the "main" repository, extending my custom repository:
public interface UserRepository extends JpaRepository<User, String>, FilterRepositoryCustom {
...
}
我正在使用 Spring Boot,并且根据 docs:
I'm using Spring Boot and, according to the docs:
默认情况下,Spring Boot 将启用 JPA 存储库支持并查看@SpringBootApplication 所在的包(及其子包)位于.
当我运行我的应用程序时,我得到了这个错误:
When I run my application, I get this error:
org.springframework.data.mapping.PropertyReferenceException: No property filterBy found for type User!
推荐答案
这里的问题是你正在创建 FilterRepositoryImpl
但是你在 用户存储库.您需要创建 UserRepositoryImpl
来完成这项工作.
The problem here is that you are creating FilterRepositoryImpl
but you are using it in UserRepository
. You need to create UserRepositoryImpl
to make this work.
基本上
public interface UserRepositoryCustom {
List<User> filterBy(String role);
}
public class UserRepositoryImpl implements UserRepositoryCustom {
...
}
public interface UserRepository extends JpaRepository<User, String>, UserRepositoryCustom {
...
}
Spring Data 2.x 更新
这个答案是为 Spring 1.x 编写的.作为 Matt Forsythe 指出,命名期望随着 Spring Data 2.0 而改变.实现从 the-final-repository-interface-name-with-an-additional-Impl-suffix
更改为 the-custom-interface-name-with-an-additional-Impl-后缀
.
Spring Data 2.x update
This answer was written for Spring 1.x. As Matt Forsythe pointed out, the naming expectations changed with Spring Data 2.0. The implementation changed from the-final-repository-interface-name-with-an-additional-Impl-suffix
to the-custom-interface-name-with-an-additional-Impl-suffix
.
所以在这种情况下,实现的名称是:UserRepositoryCustomImpl
.
So in this case, the name of the implementation would be: UserRepositoryCustomImpl
.
这篇关于找不到类型的属性...自定义 Spring 数据存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!