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
In order to define SQL to execute for a Spring Data repository method, we can annotate the method with the @Query annotation — its value attribute contains the JPQL or SQL to execute. The @Query annotation takes precedence over named queries, which are annotated with @NamedQuery or defined in an orm.xml file.
Crud Repository doesn't provide methods for implementing pagination and sorting. JpaRepository ties your repositories to the JPA persistence technology so it should be avoided. We should use CrudRepository or PagingAndSortingRepository depending on whether you need sorting and paging or not.
We can use @Query annotation to specify a query within a repository. Following is an example. In this example, we are using JPQL, Java Persistence Query Language. We've added name query custom methods in Repository in JPA Named Query chapter.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With