找不到类型的属性...自定义 Spring Data 存储库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41467894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
No property found for type... custom Spring Data repository
提问by Héctor
I'm trying to implement a custom Spring repository. I have the interface:
我正在尝试实现自定义 Spring 存储库。我有接口:
public interface FilterRepositoryCustom {
List<User> filterBy(String role);
}
the implementation:
实施:
public class FilterRepositoryImpl implements FilterRepositoryCustom {
...
}
and the "main" repository, extending my custom repository:
和“主”存储库,扩展我的自定义存储库:
public interface UserRepository extends JpaRepository<User, String>, FilterRepositoryCustom {
...
}
I'm using Spring Boot and, according to the docs:
我正在使用 Spring Boot,并且根据文档:
By default, Spring Boot will enable JPA repository support and look in the package (and its subpackages) where @SpringBootApplication is located.
默认情况下,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!
org.springframework.data.mapping.PropertyReferenceException:未找到用户类型的属性 filterBy!
回答by Aman Tuladhar
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.
这里的问题是您正在创建FilterRepositoryImpl
但您在UserRepository
. 您需要创建UserRepositoryImpl
才能完成这项工作。
Basically
基本上
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 update
This answer was written for Spring 1.x. As Matt Forsythepointed 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
.
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
。
So in this case, the name of the implementation would be: UserRepositoryCustomImpl
.
所以在这种情况下,实现的名称将是:UserRepositoryCustomImpl
。
回答by user64141
Another way this error can happen if the impl class for FilterRepositoryCustom isn't picked up in your spring configuration:
如果您的 spring 配置中未选择 FilterRepositoryCustom 的 impl 类,则可能发生此错误的另一种方式:
@EnableJpaRepositories(basePackageClasses = {RepoPackageMarker.class, FilterRepositoryCustomImpl.class})
回答by Vivek Vhatkar
Is it a must that the customMethod()
in the CustomRepository can only have parameters defined that are either
是否必须customMethod()
在 CustomRepository 中定义的参数要么是
1.Entity class name - customMethod(User user)
,
1.实体类名 - customMethod(User user)
,
2.Entity class attributes - customMethod(String firstName)
, here firstName is an attribute of User Entity class.
2.Entity类属性- customMethod(String firstName)
,这里firstName是User Entity类的一个属性。
Can I not have something like customMethod(CustomCriteria criteria), the criteria class contain the various attributes that are used to construct a dynamic query.
我不能有像 customMethod(CustomCriteria criteria) 这样的东西吗,标准类包含用于构造动态查询的各种属性。
e.g. getStatusByCriteria(CustomCriteria criteria), CustomCriteria
is a simple pojo annotated with @Component so that spring identifies it.
eggetStatusByCriteria(CustomCriteria criteria), CustomCriteria
是一个用 @Component 注释的简单 pojo,以便 spring 识别它。
When I tried this I get an error:
当我尝试这个时,我收到一个错误:
org.springframework.data.mapping.PropertyReferenceException: No property criteria found for type UserRepository!
org.springframework.data.mapping.PropertyReferenceException:未找到 UserRepository 类型的属性标准!
回答by Bruno 82
I had the same problem in a project of mine. I solved the problem by adding a line in my pom.xml
我在我的一个项目中遇到了同样的问题。我通过在pom.xml 中添加一行解决了这个问题
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<includes>
<include>com/my/package/entities/*.java</include>
<include>com/my/package/repositories/*.java</include>
<include>com/my/package/repositories/impl/*.java</include> <!-- add this -->
</includes>
</configuration>
</plugin>
回答by Java Man
Old way:
旧方式:
Entity aThing = repository.findOne(1L);
New way:
新方法:
Optional<Entity> aThing = repository.findById(1L);