I want to use Spring Data JPA with default sort direction with the latest spring-boot-starter-parent 2.2.1.RELEASE:
@Override
public Page<ProcessingLogs> findAll(int page, int size) {
return dao.findAll(PageRequest.of(page,size, new Sort(Sort.Direction.DESC, "createdAt")));
}
But I get error:
The constructor Sort(Sort.Direction, String) is undefined
This is the latest code: https://github.com/spring-projects/spring-data-commons/blob/master/src/main/java/org/springframework/data/domain/Sort.java
Do you know how I can solve this issue?
We can create a PageRequest object by passing in the requested page number and the page size. In Spring MVC, we can also choose to obtain the Pageable instance in our controller using Spring Data Web Support. The findAll(Pageable pageable) method by default returns a Page<T> object.
One option is to use Spring Data's method derivation, whereby the query is generated from the method name and signature. All we need to do here to sort our data is include the keyword OrderBy in our method name, along with the property name(s) and direction (Asc or Desc) by which we want to sort.
The static of method defined in PageRequest class is used to create a new PageRequest . PageRequest#of method provides multiple overloaded methods in order to provide multiple Pagination strategy. overloaded of methods defined in in PageRequest are. of(int page, int size)
Spring Data JPA or JPA stands for Java Persistence API, so before looking into that, we must know about ORM (Object Relation Mapping). So Object relation mapping is simply the process of persisting any java object directly into a database table.
Use like this PageRequest.of(page, size, Sort.by(Sort.Direction.DESC,"createdAt"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With