无法在 Spring Data Repository 中创建自定义查询方法 [英] Can't create custom query method in Spring Data Repository
问题描述
我想创建自定义存储库:
I wanted to create custom repository :
public interface FriendRepositoryCustom {
Page<Friend> findFriends(FriendCriteria friendCriteria, Pageable pageable);
}
及其实现:
@Repository
@Transactional(readOnly = true)
public class FriendRepositoryCustomImpl implements FriendRepositoryCustom {
@PersistenceContext
EntityManager entityManager;
@Override
public Page<Friend> findFriends(FriendCriteria friendCriteria, Pageable pageable) {
...
}
并将其添加到主存储库:
And added it to main repository :
@Repository
public interface FriendRepository extends JpaRepository<Friend, Long>, JpaSpecificationExecutor<Friend>, FriendRepositoryCustom {
}
当我启动应用程序时出现此错误:
When i start application i get this error :
原因:org.springframework.data.mapping.PropertyReferenceException:否为 Friend 类型找到属性 findFriends!在org.springframework.data.mapping.PropertyPath.(PropertyPath.java:77)在org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:329)在org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:309)在org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:272)在org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:243)在org.springframework.data.repository.query.parser.Part.(Part.java:76)在org.springframework.data.repository.query.parser.PartTree$OrPart.(PartTree.java:247)在org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:398)在org.springframework.data.repository.query.parser.PartTree$Predicate.(PartTree.java:378)在org.springframework.data.repository.query.parser.PartTree.(PartTree.java:86)在org.springframework.data.jpa.repository.query.PartTreeJpaQuery.(PartTreeJpaQuery.java:70)...省略了43个常用帧
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property findFriends found for type Friend! at org.springframework.data.mapping.PropertyPath.(PropertyPath.java:77) at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:329) at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:309) at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:272) at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:243) at org.springframework.data.repository.query.parser.Part.(Part.java:76) at org.springframework.data.repository.query.parser.PartTree$OrPart.(PartTree.java:247) at org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:398) at org.springframework.data.repository.query.parser.PartTree$Predicate.(PartTree.java:378) at org.springframework.data.repository.query.parser.PartTree.(PartTree.java:86) at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.(PartTreeJpaQuery.java:70) ... 43 common frames omitted
推荐答案
您的实现类可能命名错误.
You are probably naming your implementation class wrong.
请注意,命名期望随着 Spring Data 2.0 发生变化.
Note that the naming expectations changed with Spring Data 2.0.
对于 <2.0 必须将实现命名为带有附加 Impl
后缀的最终存储库接口.参见示例的匹配参考文档.
For < 2.0 the implementation had to be named as the final repository interface with an additional Impl
suffix. See the matching reference documentation for an example.
对于 >= 2.0,必须将实现命名为带有附加 Impl
后缀的自定义接口.查看当前参考文档以获取示例.
For >= 2.0 the implementation has to be named as the custom interface with an additional Impl
suffix. See the current reference documentation for an example.
注意:您不需要任何 @Repository
注释.
Note: You don't need any of the @Repository
annotations.
这篇关于无法在 Spring Data Repository 中创建自定义查询方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!