JPA query creation order by

后端 未结 4 1557
孤城傲影
孤城傲影 2021-02-18 22:07

I\'m trying to learn Spring on my own, and I\'m planning to do that by creating a blog web app. I already have the basic blog functionality working, which is a page to display b

相关标签:
4条回答
  • 2021-02-18 22:22

    I know it's a bit late but this may help others.

    Just put BY before ORDER, like this: findAllByOrderByIdDesc

    It should work.

    0 讨论(0)
  • 2021-02-18 22:26

    There is an extension to CrudRepository called PagingAndSortingRepository

     public interface PostRepository extends PagingAndSortingRepository<Post, Integer> {}
    

    Then just call .findAll(new Sort(Sort.Direction.DESC, "id")); instead of findAllOrderByIdDesc();

    0 讨论(0)
  • 2021-02-18 22:27

    May be my answer is stupid but did you try just "from Post order by id desc" instead of "select p from Post p order by p.id desc"

    0 讨论(0)
  • 2021-02-18 22:40

    I know I'm a little late to the game but..

    Another fix would be to correct the syntax on your named query in your repository.

    You have: Iterable<Post> findAllOrderByIdDesc();

    When it should be: Page<Post> findAllByOrderByIdDesc();

    Note the addition of the By keyword after findAll. This marks the beginning of your filtering statements.

    0 讨论(0)
提交回复
热议问题