This question already has an answer here:
- How to add custom method to Spring Data JPA 11 answers
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
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