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);
Pedro Dusso
  • 2,100
  • 9
  • 34
  • 64

2 Answers2

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.

Oliver Drotbohm
  • 80,157
  • 18
  • 225
  • 211
  • 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 :) – Pedro Dusso Sep 22 '14 at 18:06
  • 3
    Fix your return type. You put in a `Pageable` so the result has to be a page of results not a single result. – M. Deinum Sep 22 '14 at 18:39
  • @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. – Robert Niestroj Jan 27 '16 at 14:17
  • Because you refer to interfaces, not implementation. – Oliver Drotbohm Jan 28 '16 at 09:55
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;
tk_
  • 16,415
  • 8
  • 80
  • 90