2014-09-22 23 views
8

我知道this問題,但是使用org.springframework.data:spring-data-jpa:1.7.0.RELEASE我仍然有同樣的問題(Either use @Param on all parameters except Pageable and Sort typed once, or none at all!)。我的階級是:Pageable和@Param在彈簧數據JpaRepository方法問題[2]

public interface BalanceHistoryRepository extends JpaRepository<BalanceHistory, Long> { 
    @Query("select bh from BalanceHistory bh where bh.account.id = :idAccount") 
    public BalanceHistory findCurrentBalanceByAccountNumber(PageRequest pageCriteira, @Param("idAccount") long idAccount); 
} 

編輯

電話:

Pageable page = new PageRequest(0, 1, Sort.Direction.DESC, "date"); 
     BalanceHistory bh = balanceHistoryRepository.findCurrentBalanceByAccountNumber(1,page); 

方法:

@Query("select bh from BalanceHistory bh where bh.account.id = :idAccount") 
public BalanceHistory findCurrentBalanceByAccountNumber(@Param("idAccount") long idAccount, Pageable pageCriteira); 
+0

這個錯誤是在spring-data-commons中,如果你依賴於較舊版本的spring-data-commons升級,spring-data-jpa將無濟於事。 – 2014-09-22 13:31:06

+0

那些java.lang人需要冷靜下來...... – muttonUp 2016-07-19 12:18:34

回答

14

確保你使用Pageable代替PageRequest使第一個參數是確認爲不受約束到實際的查詢。此外,您需要將退貨類型更改爲PageList,因爲您會主要返回多個結果。

public interface BalanceHistoryRepository extends CrudRepository<BalanceHistory, Long> { 

    @Query("select bh from BalanceHistory bh where bh.account.id = :idAccount") 
    Page<BalanceHistory> findCurrentBalanceByAccountNumber(@Param("idAccount") long idAccount, Pageable pageable); 
} 

這應該可以做到。請注意,我們通常建議而不是來擴展存儲特定的接口,因爲它們公開特定於商店的API,只有在真正有必要時纔會暴露該API。

+0

感謝您的輸入!這種改變會產生一個java.lang.IllegalStateException:方法必須具有以下返回類型之一! [interface org.springframework.data.domain.Slice,interface org.springframework.data.domain.Page,interface java.util.List]'異常。請參閱我的編輯:) – 2014-09-22 18:06:52

+3

修復您的返回類型。你放入一個'Pageable',所以結果必須是一個結果頁面,而不是單個結果。 – 2014-09-22 18:39:28

+0

@Oliver:爲什麼參數必須是「Pageable」,我把param設爲'PageRequest',它引發了這個問題中提到的異常。 – 2016-01-27 14:17:20