I am using Spring Data JPA with Spring Data REST and i have created a JpaRepository for my Thing
entity.
@Repository
public interface ThingRepository extends JpaRepository<Thing, Long> {
@RestResource(path = "findByName", rel = "findByName")
Page findByName(@Param("name") String name, Pageable page);
}
I want to apply sorting. I want to sort the Thing
List based on a custom rating algorithm.
List<Thing> sortByRating(List<Thing> things){
// custom logic
return things;
};
I would like if possible to use a custom function in order to sort the results. What is the best approach to this kind of thing in Spring JPA? How can i make my Repository use my function to sort the result set?
Spring Data offers basic sorting as seen here . You can also annotate your query with @Query
and use order by
, but I believe what you need is a more complex sorting logic.
If youre using Java 8 you can use its features to sort your list, this article shows good examples of that, it takes a bit more work but seems like what you're after. Note that in order to convert from stream
to list
you have to include .collect(Collectors.toList());
at the end of the stream
.
Example(taken from the article):
employees.stream().sorted((Employee e1, Employee e2) -> e1.getHireDate()
.compareTo(e2.getHireDate())).collect(Collectors.toList());
Edit : I didn't notice you were using pagination. I had a similar problem where I had to do something with the page contents before returning the Page
object, so I ended up creating an extension of the PageImpl<T>
class:
public class PageImplBean<T> extends PageImpl<T> {
private static final long serialVersionUID = 1L;
private int number;
private int size;
private int totalPages;
private int numberOfElements;
private long totalElements;
private boolean previousPage;
private boolean first;
private boolean nextPage;
private boolean last;
private List<T> content;
@JsonIgnore
private Sort sort;
public PageImplBean() {
super(new ArrayList<T>());
}
public PageImplBean(Page pagina){
super(new ArrayList<T>());
this.number = pagina.getNumber();
this.size = pagina.getSize();
this.totalPages = pagina.getTotalPages();
this.numberOfElements = pagina.getNumberOfElements();
this.totalElements = pagina.getTotalElements();
this.previousPage = pagina.hasPrevious();
this.first = pagina.isFirst();
this.nextPage = pagina.hasNext();
this.last = pagina.isLast();
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public int getTotalPages() {
return totalPages;
}
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
public int getNumberOfElements() {
return numberOfElements;
}
public void setNumberOfElements(int numberOfElements) {
this.numberOfElements = numberOfElements;
}
public long getTotalElements() {
return totalElements;
}
public void setTotalElements(long totalElements) {
this.totalElements = totalElements;
}
public boolean isPreviousPage() {
return previousPage;
}
public void setPreviousPage(boolean previousPage) {
this.previousPage = previousPage;
}
public boolean isFirst() {
return first;
}
public void setFirst(boolean first) {
this.first = first;
}
public boolean isNextPage() {
return nextPage;
}
public void setNextPage(boolean nextPage) {
this.nextPage = nextPage;
}
public boolean isLast() {
return last;
}
public void setLast(boolean last) {
this.last = last;
}
public List<T> getContent() {
return content;
}
public void setContent(List<T> content) {
this.content = content;
}
public Sort getSort() {
return sort;
}
public void setSort(Sort sort) {
this.sort = sort;
}
public PageImpl<T> pageImpl() {
return new PageImpl<T>(getContent(), new PageRequest(getNumber(),
getSize(), getSort()), getTotalElements());
}
}
What's important here in your case is the setContent
method which doesnt exist in the base class. You can use that with your sorting method:
PageImplBean<Thing> page = //call your rest repository
List<Thing> pageContent = page.getContent();
page.setContent(sortByRating(pageContent));
You can customize your object page(Pageable). If you get data of request:
public PageRequest customSort(Args...) {
String[] newParams = {"newParam"};
Sort sort = page.getSort();
Sort.Order dataSort =
sort.iterator().next();
Sort newSort = Sort.by(dataSort.getDirection(), newParams);
return PageRequest.of(page.getPageNumber(), this.getPageSize(), newSort);
}
SQL Before:
order by oldParam ASC/DESC;
SQL After:
order by newParam ASC/DESC;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.