【问题标题】:Spring data sort by nested propertySpring数据按嵌套属性排序
【发布时间】:2021-02-08 17:56:05
【问题描述】:

我在 Spring Boot 中使用分页。从数据库获取页面时,我想按嵌套属性对数据进行排序。这是我创建页面请求的代码。

Pageable pageOfElements = PageRequest.of(pageNumber, pageSize, Sort.by("customDateObject").descending());

我的自定义对象实现了可比较,但排序不起作用,我该如何解决这个问题?提前谢谢你。

【问题讨论】:

  • 你能用这个特定的“customDateObject”发布一个对象的实体(类)吗?

标签: spring-boot spring-data-jpa pagination spring-data


【解决方案1】:

您可以按参数在排序中嵌套属性:

Pageable pageOfElements = PageRequest.of(pageNumber, pageSize, Sort.by("customDateObject.customProperty").descending());

因此,例如,如果您按idownerCars 进行排序,您将执行以下操作:

Sort.by("owner.id")

【讨论】:

    猜你喜欢
    • 2017-07-04
    • 2017-06-08
    • 1970-01-01
    • 1970-01-01
    • 2010-11-13
    • 1970-01-01
    • 2019-02-28
    • 1970-01-01
    • 2021-02-16
    相关资源
    最近更新 更多