无法在Spring Data Repository中创建自定义查询方法 [英] Can't create custom query method in Spring Data Repository

查看:119
本文介绍了无法在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屋!

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