gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:28:54 29 4
gpt4 key购买 nike

我正在尝试使用 Spring Boot 和 Spring 数据 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 文档在 Save 部分不清楚。 Spring Data JPA Async Support

最佳答案

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

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

您不能从 JpaRepository 扩展但是,因为这会与它从 CrudRepository 继承的那个冲突。 .扩展自 Repository而是从 CrudRepository 复制您需要的任何方法, PagingAndSortingRepository , 或 JpaRepository

关于asynchronous - 为什么 Spring-Data-JPA 异步不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34973306/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com