我正在研究Spring Data JPA.考虑下面的示例,我将默认使用所有crud和finder功能,如果我想自定义查找器,那么也可以在界面本身轻松完成.
@Transactional(readOnly = true)
public interface AccountRepository extends JpaRepository<Account, Long> {
@Query("<JPQ statement here>")
List<Account> findByCustomer(Customer customer);
}
Run Code Online (Sandbox Code Playgroud)
我想知道如何为上述AccountRepository添加一个完整的自定义方法及其实现?由于它的接口我无法在那里实现该方法.
我想创建自定义存储库:
public interface FriendRepositoryCustom {
Page<Friend> findFriends(FriendCriteria friendCriteria, Pageable pageable);
}
Run Code Online (Sandbox Code Playgroud)
及其实现:
@Repository
@Transactional(readOnly = true)
public class FriendRepositoryCustomImpl implements FriendRepositoryCustom {
@PersistenceContext
EntityManager entityManager;
@Override
public Page<Friend> findFriends(FriendCriteria friendCriteria, Pageable pageable) {
...
}
Run Code Online (Sandbox Code Playgroud)
并将其添加到主存储库:
@Repository
public interface FriendRepository extends JpaRepository<Friend, Long>, JpaSpecificationExecutor<Friend>, FriendRepositoryCustom {
}
Run Code Online (Sandbox Code Playgroud)
当我启动应用程序时,出现此错误:
由以下原因引起: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( org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:272)上的PropertyPath.java:309)org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:243)上的org.springframework.data org.springframework.data.repository.query.parser.PartTree $ OrPart。(PartTree.java:247)的org.springframework.data.repository.query的.repository.query.parser.Part。(Part.java:76) org.springframework.data.repository的.parser.PartTree $ Predicate.buildTree(PartTree.java:398)。