我有两个实体,即调查和信息。
调查实体:
@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
}
}
但我想知道是否有更清洁的解决方案,即在序列化时进行排序。感谢。
答案 0 :(得分:2)
无法对HashSet进行排序,这只是普通的java行为。你要找的是TreeSet。使用Comparator提供它,或在您的信息类中实现Comparable,并且将订购您的Json。