我有两个实体,调查和信息。
调查单位:
@RooJavaBean
@RooToString
@RooJpaActiveRecord(table = "information")
@JsonPropertyOrder({ "seq"})
public class Information {
@NotNull
private String title;
@ManyToOne(fetch = FetchType.LAZY)
@JsonBackReference
private Survey survey;
private int seq;
}
信息实体:
@RooJavaBean
@RooToString
@RooJpaActiveRecord(table = "survey")
public class Survey {
@NotNull
@Size(min = 3, max = 50)
private String title;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy="survey")
@JsonManagedReference
private Set<Information> informations = new HashSet<Information>();
}
我用杰克逊来序列化。
我的期望:
{
"survey" : {
"title" : "Medical Survey",
"informations" : [ {
"id" : 1,
"seq" : 0,
"title" : "Name:",
"version" : 0
}, {
"id" : 2,
"seq" : 1,
"title" : "Age:",
"version" : 0
}, {
"id" : 3,
"seq" : 2,
"title" : "test",
"version" : 0
}, {
"id" : 4,
"seq" : 3,
"title" : "test",
"version" : 0
} ],
"id" : 1,
"version" : 134
}
}
但结果是:
{
"survey" : {
"title" : "Medical Survey",
"informations" : [ {
"id" : 2,
"seq" : 1,
"title" : "Age:",
"version" : 0
}, {
"id" : 4,
"seq" : 3,
"title" : "test",
"version" : 0
}, {
"id" : 3,
"seq" : 2,
"title" : "test",
"version" : 0
}, {
"id" : 1,
"seq" : 0,
"title" : "Name:",
"version" : 0
} ],
"id" : 1,
"version" : 134
}
}
- 我知道在这种情况下使用 list 应该更合适,但 Spring Roo 不支持脚手架中的列表。因此,我使用了带有 seq 编号的 HashSet。
- 我也知道我可以创建一个信息数组列表作为@transient,然后在业务层对其进行克隆和排序。
但是我想知道有没有更干净的解决方案,即序列化时排序。谢谢。