2016-01-24 67 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