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

9

我想创建自定义仓库:

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! 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

1个回答

22

你可能错误地命名了你的实现类。

请注意,Spring Data 2.0 更改了命名约定。

对于小于 2.0 版本的情况,实现类必须与最终存储库接口相同,额外添加一个 Impl 后缀。 看一下文档中的匹配示例

对于大于等于 2.0 版本的情况,实现类必须与自定义接口相同,并额外添加一个 Impl 后缀。 查看当前文档的示例

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接