为什么 Spring-Data-JPA Async 不工作?
Why is Spring-Data-JPA Async not working?
我正在尝试使用 Spring 引导和 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 文档在其中的保存部分并不清楚。 Spring Data JPA Async Support
你试过只 return CompletableFuture
吗?像这样(未经测试):
@Async
<S extends Loan> CompletableFuture<S> save(S loan);
不过您不能从 JpaRepository
扩展,因为这会与它从 CrudRepository
继承的那个发生冲突。相反,从 Repository
扩展并从 CrudRepository
、PagingAndSortingRepository
或 JpaRepository
复制您需要的任何方法
只是一个猜测:也许保存需要交易,而查询则不需要。尝试 @Transactional
...
我自己也遇到过这个问题,在尝试了一些变体之后,这似乎有效:
@Async
@Override
<S extends Loan> CompletableFuture<S> save(S loan);
我正在尝试使用 Spring 引导和 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 文档在其中的保存部分并不清楚。 Spring Data JPA Async Support
你试过只 return CompletableFuture
吗?像这样(未经测试):
@Async
<S extends Loan> CompletableFuture<S> save(S loan);
不过您不能从 JpaRepository
扩展,因为这会与它从 CrudRepository
继承的那个发生冲突。相反,从 Repository
扩展并从 CrudRepository
、PagingAndSortingRepository
或 JpaRepository
只是一个猜测:也许保存需要交易,而查询则不需要。尝试 @Transactional
...
我自己也遇到过这个问题,在尝试了一些变体之后,这似乎有效:
@Async
@Override
<S extends Loan> CompletableFuture<S> save(S loan);