相关疑难解决方法(0)

如何向Spring Data JPA添加自定义方法

我正在研究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添加一个完整的自定义方法及其实现?由于它的接口我无法在那里实现该方法.

java spring-data spring-data-jpa

144
推荐指数
8
解决办法
15万
查看次数

无法在Spring Data Repository中创建自定义查询方法

我想创建自定义存储库:

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)。

java hibernate spring-data-jpa spring-boot jhipster

6
推荐指数
1
解决办法
2462
查看次数