Spring Data Rest投影排序 [英] Spring Data Rest projection sorting

查看:72
本文介绍了Spring Data Rest投影排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的实体有一个投影,我需要按内部类的字段对其进行排序.这是我的实体的一部分:

I have a projection for my entity and I need to sort it by field of inner class. This is part of my entities:

class Person {
    UUID guid;
    Set<DisabilityHistory> disabilityHistory;
}
class DisabilityHistory {
    Date createdDate;
}

我知道 sort 参数,但像 api/person/search?projection=myProjection&sort=disabilityHistory.createdDate,asc 这样的请求不起作用.我找到的唯一解决方案是在我的实体中使用 @OrderBy 注释,但在这种情况下,它将始终排序,我担心性能.

I know about sort param but request like api/person/search?projection=myProjection&sort=disabilityHistory.createdDate,asc doesn't work. The only solution I have found is using @OrderBy annotation in my entity but in this case it will sorted always and I worried about performance.

推荐答案

当您尝试对实体中的内部字段进行排序时,这将不起作用,因此您无法在 DB 级别执行此操作.我使用它的方式是在 投影 类使用 SpEL.这是如何做到这一点的示例:

This will not work as you are trying to sort an inner field in your entity, so you can't do it on DB level. The way I've used it is by sorting it in the Projection class using SpEL. This is an example of how you can do it:

@Projection(
        name = "sorted",
        types = Person.class
)
public interface PersonProjection {

    @Value("#{@personProjectionHelper.sortedByDisabilityHistory(target.disabilityHistory)}")
    List<DisabilityHistory> getDisabilityHistory();
}

并用Java实现排序:

And implement the sorting in Java:

@Component
public class PersonProjectionHelper {

    public List<DisabilityHistory> sortByDisabilityHistory(final List<DisabilityHistory> list) {
        // do the sorting on Java level
    }
}

这篇关于Spring Data Rest投影排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆