Pageable and @Param in a spring data JpaRepository method issue [2]
Asked Answered
W

3

14

I'm aware of this question, but using org.springframework.data:spring-data-jpa:1.7.0.RELEASE I'm still having the same issue (Either use @Param on all parameters except Pageable and Sort typed once, or none at all!). My class is:

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);
}

Edit

Call:

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

Method:

@Query("select bh from BalanceHistory bh where bh.account.id = :idAccount")
public BalanceHistory findCurrentBalanceByAccountNumber(@Param("idAccount") long idAccount, Pageable pageCriteira);
Winona answered 22/9, 2014 at 13:18 Comment(2)
The bug is in spring-data-commons if you have a dependency on an older version of spring-data-commons upgrading spring-data-jpa won't help.Creekmore
Those java.lang guys need to chill out....Wound
P
25

Make sure you use Pageable instead of PageRequest so that the first parameter is recognized as one not to be bound to the actual query. Also, you need to change the return type to either Page or List as you'll return multiple results mostly.

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);
}

This should do the trick. Note, that we generally recommend not to extend the store specific interfaces as they expose store-specific API that should only be exposed if really necessary.

Phenix answered 22/9, 2014 at 15:28 Comment(4)
Thanks for the input! This change yields a java.lang.IllegalStateException: Method has to have one of the following return types! [interface org.springframework.data.domain.Slice, interface org.springframework.data.domain.Page, interface java.util.List] exception. See my edit please :)Winona
Fix your return type. You put in a Pageable so the result has to be a page of results not a single result.Creekmore
@Oliver: Why does the parameter have to be Pageable and i i mage the param a PageRequest it throws this exception mentioned in the question.Termagant
Because you refer to interfaces, not implementation.Phenix
S
3

I encountered the same exception when I accidentally imported the wrong Pageable class.

This can also happen if you use PageRequest in the repository as well.

it should be,

import org.springframework.data.domain.Pageable;
Stem answered 5/9, 2019 at 12:34 Comment(0)
P
0

I solved it using the following query

  @Query("SELECT c FROM yourTable c where c.isActive=?1 and c.isDelete='N' ORDER BY c.companyAccId DESC")
        Page<yourTable > findAllByIsActiveAndIsDelete( String isActive,Pageable pageable);
Perjured answered 20/12, 2023 at 10:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.