Can't create custom query method in Spring Data Repository [duplicate]

梦想与她 提交于 2019-11-26 21:01:54

问题


I wanted to create custom repository :

public interface FriendRepositoryCustom {

    Page<Friend> findFriends(FriendCriteria friendCriteria, Pageable pageable);
}

And its implementation :

@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 :

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


回答1:


You are probably naming your implementation class wrong.

Note that the naming expectations changed with Spring Data 2.0.

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.

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.

Note: You don't need any of the @Repository annotations.



来源:https://stackoverflow.com/questions/52624486/cant-create-custom-query-method-in-spring-data-repository

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!