Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use OrderBy with GreaterThan Spring JPA

I want to add in my Repository interface a method which find all the data greater than a long publishdata value and Order it Decreacingly:

I tried this, but it doesn't seems to be working:

@Repository
public interface NoticiaRepository extends CrudRepository<Noticia,Long>{

    Noticia findById(long id);
    List<Noticia> findByOrderPublishdateGreaterThanDesc(long publishdate);

}
like image 824
Alberto Crespo Avatar asked Feb 29 '16 22:02

Alberto Crespo


People also ask

How do you write orderBy in JPA?

With JPA Criteria – the orderBy method is a “one stop” alternative to set all sorting parameters: both the order direction and the attributes to sort by can be set. Following is the method's API: orderBy(CriteriaBuilder. asc): Sorts in ascending order.

Is it possible to define a sort type in @query annotation using Spring JPA?

Using a Sort Object With Spring Data JPA, you can also add a parameter of type Sort to your method definition. Spring Data JPA will then generate the required ORDER BY clause. That is the same approach as you can use in a derived query.

How do you get data in descending order 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.

Which is better CrudRepository or JpaRepository?

Crud Repository doesn't provide methods for implementing pagination and sorting. JpaRepository ties your repositories to the JPA persistence technology so it should be avoided. We should use CrudRepository or PagingAndSortingRepository depending on whether you need sorting and paging or not.


1 Answers

List<Noticia> findByPublishdateGreaterThanOrderByPublishdateDesc(Long publishdate)
like image 132
marok Avatar answered Oct 01 '22 14:10

marok