小编典典

无法在Spring Data Repository中创建自定义查询方法

spring-boot

我想创建自定义存储库:

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) {
    ...
    }

并将其添加到主存储库:

@Repository
public interface FriendRepository extends JpaRepository<Friend, Long>, JpaSpecificationExecutor<Friend>, FriendRepositoryCustom {

}

当我启动应用程序时,出现此错误:

由以下原因引起: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(
org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:272)上的PropertyPath.java:309)org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:243)上的org.springframework.data
org.springframework.data.repository.query.parser.PartTree $
OrPart。(PartTree.java:247)的org.springframework.data.repository.query的.repository.query.parser.Part。(Part.java:76)
org.springframework.data.repository的.parser.PartTree $
Predicate.buildTree(PartTree.java:398)。


阅读 421

收藏
2020-05-30

共1个答案

小编典典

您可能将实现类命名为错误。

请注意,命名期望随着Spring Data 2.0的改变而改变。

对于<2.0,必须将实现命名为带有附加Impl后缀的最终存储库接口。有关示例,请参见匹配的参考文档

对于> =
2.0,必须将实现命名为带有附加Impl后缀的自定义接口。有关示例,请参见当前参考文档

注意:您不需要任何@Repository注释。

2020-05-30