Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The constructor Sort(Sort.Direction, String) is undefined

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?

like image 539
Peter Penzov Avatar asked Nov 26 '19 22:11

Peter Penzov


People also ask

How do I use PageRequest?

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.

How do you sort in spring boot?

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.

What is PageRequest?

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)

What is spring data in spring boot?

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.


1 Answers

Use like this PageRequest.of(page, size, Sort.by(Sort.Direction.DESC,"createdAt"))

like image 96
dasunse Avatar answered Sep 18 '22 02:09

dasunse