I am working on a Spring Boot using Spring Data JPA and Hibernate and I have the following problem trying to make pageable a repository method.
If I use method such as findAll() it is pretty simple to make it pegaeable. For example I have this service method where I call the findAll() repository method passing to it the Pageable object:
@Override
public Page<WalletDTO> getAllWalletsList(Pageable pageable) throws NotFoundException {
Page<Wallet> walletsList = this.walletRepository.findAll(pageable);
if(walletsList.isEmpty()) {
throw new NotFoundException(String.format("There are no wallet in the whole system !!!"));
}
return walletsList.map(m -> conversionService.convert(m, WalletDTO.class));
}
It works perfectly.
But now I have this repository interface:
public interface WalletRepository extends JpaRepository<Wallet, Integer> {
List<Wallet> findByUser_Id(Integer id);
List<Wallet> findByWalletType_Name(String walletName);
List<Wallet> findByWalletType_WalletType(WalletType walletType, Pageable pageable);
Wallet findByAddress(String address);
}
What can I do to make a method pegeable, I was trying to do something like this:
List<Wallet> findByWalletType_WalletType(WalletType walletType, Pageable pageable);
but I don't know if it could be a solution. What could be the correct way to implement this behavior?
CodePudding user response:
Additionally, instead of returning your type directly, you may either return a Page<T>
or a Slice<T>
. The main difference being that the page will trigger an additional count (useful for displaying a pager for example) while the slice is only aware of being the last (useful for an infinite scroll).
CodePudding user response:
According to spring doc
We also provide persistence technology-specific abstractions, such as JpaRepository or MongoRepository. Those interfaces extend CrudRepository and expose the capabilities of the underlying persistence technology in addition to the rather generic persistence technology-agnostic interfaces such as CrudRepository.
On top of the CrudRepository, there is a PagingAndSortingRepository abstraction that adds additional methods to ease paginated access to entities
So by extending JpaRepository you already have inhereted PagingAndSortingRepository
and you can expose methods with pagination, which will be automatically implemented by Spring.
Also according to spring doc you have the following examples that will be automatically be implemented by Spring data jpa
Example 14. Using Pageable, Slice, and Sort in query methods
Page findByLastname(String lastname, Pageable pageable);
Slice findByLastname(String lastname, Pageable pageable);
List findByLastname(String lastname, Pageable pageable);
So in your case you would have the following options
Page<Wallet> findByWalletType_WalletType(WalletType walletType, Pageable pageable);
Slice<Wallet> findByWalletType_WalletType(WalletType walletType, Pageable pageable);
List<Wallet> findByWalletType_WalletType(WalletType walletType, Pageable pageable);
Read the doc for more details, pick the one you prefer and you should be good!
Just one last tip. For your already declared name WalletType_WalletType
to work it means that Wallet
has a field named WalletType
and then WalletType
has also a field with name WalletType
. This seems a bit strange.
CodePudding user response:
You can simply extend PagingAndSortingRepository instead of JpaRepository:
public interface WalletRepository extends PagingAndSortingRepository<Wallet, Integer> {
List<Wallet> findByUser_Id(Integer id);
List<Wallet> findByWalletType_Name(String walletName);
Page<Wallet> findByWalletType_WalletType(WalletType walletType, Pageable pageable);
Wallet findByAddress(String address);
}
Now you can easily pass the Pageable Object.