2016-01-24 108 views
2

我想用Spring Boot和Spring数据JPA创建一个非阻塞休息服务。为什么Spring-Data-JPA Async不起作用?

如何使用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) 

上异步支持春耕数据JPA文档是不是在它的保存部分清晰。 Spring Data JPA Async Support

+0

复制所需的任何方法任何帮助?任何人? – SRK

回答

0

您是否尝试过仅返回CompletableFuture?像这样的(未经测试)的东西:

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

不能从JpaRepository延伸穿过,因为这将它从CrudRepository继承了一个冲突。改为从Repository改为从CrudRepository,PagingAndSortingRepositoryJpaRepository