简体   繁体   English

Spring Data Rest嵌套属性与JsonUnwrapped一起排序

[英]Spring Data Rest nested property sort with JsonUnwrapped

So, I have this class, using @EmbeddedId and @JsonUnwrapped annotations 所以,我有了这个类,使用@EmbeddedId@JsonUnwrapped注解

@Entity
class Order {
    @EmbeddedId @JsonUnwrapped
    private OrderPK pk;
    private String field1;
    private String field2;
    //...getters, setters
}

And this is the primary key class 这是主键类

@Embeddable
class OrderPK implements Serializable {
    private String orderNumber;
    private String company;
    //...getters, setters
}

I wanted to make Spring Data REST sorting treat the fields in the PK class as top-level instead of nested. 我想让Spring Data REST排序将PK类中的字段视为顶级而不是嵌套的。 Basically, I want to use this one: .../orders?sort=orderNumber,desc , instead of .../orders?sort=pk.orderNumber,desc . 基本上,我想使用以下代码: .../orders?sort=orderNumber,desc ,而不是.../orders?sort=pk.orderNumber,desc I couldn't find any way to do this in the documentations. 我在文档中找不到任何方法可以做到这一点。

You can no longer achieve your goal, if you are using the @EmbeddedId option to implement your composite key. 如果您正在使用@EmbeddedId选项来实现您的复合密钥,您将无法再实现目标。 The reason is that the attributes under the hood of EmbeddedId class cannot be taken out as flat parameters. 原因是EmbeddedId类的属性不能作为平面参数取出。

Note that @JsonUnwrapped is not intended to perform that task. 请注意, @JsonUnwrapped 不是要执行该任务的。

You can use the following option instead of @EmbeddedId to achieve your goal. 您可以使用以下选项代替@EmbeddedId来实现您的目标。

  • Use @IdClass . 使用@IdClass Follow this example . 请遵循此示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM