【问题标题】:Is there a way to sort based on condition?有没有办法根据条件进行排序?
【发布时间】:2019-06-27 04:39:44
【问题描述】:

我将友谊建模为与复合密钥发送者和接收者的实体友谊。

下面我有一个当前查询,我用它来结交某人的朋友。我需要使用他们的姓氏或名字对朋友进行排序。问题是,对于我拥有的模型,我不确定哪一列是朋友,哪一列是用户。

我目前能想到的就是选择所有朋友,并在Java代码中排序。

我的问题是,有没有办法仍然使用 pagable 进行排序?

@Query("SELECT f FROM Friendship f WHERE f.key.to.id = :user OR f.key.from.id = :user")
Page<Friendship> findActiveFriendships(@Param("user") Integer user, Pageable pageable);

【问题讨论】:

    标签: hibernate spring-boot spring-data-jpa hql jpql


    【解决方案1】:

    findAll(Pageable) 方法默认为您提供Page 和查询参数,因此您的存储库需要从PagingAndSortingReposigory 扩展,例如,如果您使用该方法,则可以通过查询定义排序方式,例如:localhost/entity?sort=lastname,desc

    见https://docs.spring.io/spring-data/data-commons/docs/1.6.1.RELEASE/reference/html/repositories.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-10
      • 2019-06-06
      • 2020-03-29
      • 1970-01-01
      • 1970-01-01
      • 2021-12-18
      • 1970-01-01
      • 2021-05-23
      相关资源
      最近更新 更多