我有一个有趣的问题。我的数据模型如下:
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;
}
Embeddable 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)。
答案 0 :(得分:4)
我非常肯定你正在寻找的是Projections。我不认为Spring会在没有它的情况下序列化引用的集合。
有关详情,请参阅我的回答:Spring Data-Rest POST to sub-resource