spring-data – 使用ManyToOne引用嵌入的Spring数据序列化

我有一个有趣的问题.我的数据模型如下:

A型:

@Entity
@JsonIgnoreProperties(ignoreUnknown = true)
public class A {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;
}

B型:

@Entity
@JsonIgnoreProperties(ignoreUnknown = true)
public class B {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;
}

嵌入式C:

@Embeddable
@JsonIgnoreProperties(ignoreUnknown = true)
public class C {
  @ManyToOne
  private A a;
  @ManyToOne
  private B b;
}

和D型:

@Entity
@JsonIgnoreProperties(ignoreUnknown = true)
public class D {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;

  @ElementCollection
  @OrderColumn(name = "ORDER_INDEX")
  @CollectionTable(
    name = "d_c_join",
    joinColumns = @JoinColumn(name = "d_id")
  )
  private List<C> listOfC;
}

反序列化(和存储)实体工作正常.当D类对象被序列化时,结果如下:

{
  "_embedded" : {
    "ds" : [ {
      "id" : 1,
      "listOfC" : [ { }, { } ],
      "_links" : {
        "self" : {
          "href" : "http://localhost:8000/ds/1"
        }
      }
    } ]
  }
}

如何配置Spring-Data以在C中序列化A和B(最好是通过它们的URI).

我非常肯定你正在寻找的是 Projections.我不认为Spring会在没有它的情况下序列化引用的集合.

有关详细信息,请参阅我的答案:Spring Data-Rest POST to sub-resource

相关文章
相关标签/搜索