找不到类型的属性...定制的Spring Data存储库 [英] No property found for type... custom Spring Data repository

查看:139
本文介绍了找不到类型的属性...定制的Spring Data存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现自定义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是 找到.

By default, Spring Boot will enable JPA repository support and look in the package (and its subpackages) where @SpringBootApplication is located.

运行应用程序时,出现此错误:

When I run my application, I get this error:

org.springframework.data.mapping.PropertyReferenceException:未找到针对用户类型的属性过滤器!

org.springframework.data.mapping.PropertyReferenceException: No property filterBy found for type User!

推荐答案

此处的问题是您正在创建 FilterRepositoryImpl ,但是您正在 UserRepository .您需要创建 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-suffix.

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 Data存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆