为什么 Spring-Data-JPA 异步不起作用?

2024-03-13

我正在尝试使用 Spring Boot 和 Spring data JPA 创建一个非阻塞休息服务。

如何使用 Spring Data JPA @Async 支持对实体进行异步保存。尽管其他选择似乎在同一实体上工作,但下面的代码对我不起作用。

我正在尝试在 JPA 存储库中执行此操作。这是完整的存储库:除了保存。这些方法运行良好,我可以测试它们

public interface LoanRepository extends JpaRepository<Loan,Long> {

@Query("select distinct loan from Loan loan left join fetch loan.collaterals left join fetch loan.partys")
@Async
CompletableFuture<List<Loan>> findAllWithEagerRelationships();

@Query("select loan from Loan loan left join fetch loan.collaterals left join fetch loan.partys where loan.id =:id")
@Async
CompletableFuture<Loan> findOneWithEagerRelationships(@Param("id") Long id);

@Async
void delete(Long id);

}

但是,当我尝试添加以下保存方法时:

    @Async
    <S extends CompletableFuture<Loan>> S save(Loan loan);

我收到一个明显的编译错误,上面写着"The method save(Loan) is ambiguous for the type LoanRepository"

我尝试将其更改为:

    @Async
    <S extends CompletableFuture<Loan>> S save(S loan);

但我在启动时遇到异常java.lang.UnsupportedOperationException: null这是由于:

Caused by: java.lang.NullPointerException: null
at org.springframework.data.repository.query.QueryMethod.potentiallyUnwrapReturnTypeFor(QueryMethod.java:244)

Spring Data JPA 文档中关于异步支持的保存部分并不清楚。Spring Data JPA 异步支持 http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-async


你有没有尝试过直接返回CompletableFuture反而?像这样的东西(未经测试):

@Async
<S extends Loan> CompletableFuture<S> save(S loan);

你不能从JpaRepository不过,因为这会与其继承的冲突CrudRepository。延伸自Repository相反,并复制您需要的任何方法CrudRepository, PagingAndSortingRepository, or JpaRepository

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么 Spring-Data-JPA 异步不起作用? 的相关文章

随机推荐