Pedro Dusso
Pedro Dusso

Reputation: 2140

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

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

Upvotes: 15

Views: 21537

Answers (3)

PKS
PKS

Reputation: 763

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

Upvotes: 0

tk_
tk_

Reputation: 17328

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;

Upvotes: 3

Oliver Drotbohm
Oliver Drotbohm

Reputation: 83071

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.

Upvotes: 26

Related Questions