我有一个简单的问答应用程序,我正在试验,它有三个类别的测验,问题和答案。基本关系如下:
@Entity
public class Quiz {
@Id
@GeneratedValue
private Long id;
private String description;
@OneToMany(cascade=CascadeType.PERSIST, fetch=FetchType.EAGER)
private List<Question> questions = new ArrayList<>();
@Entity
public class Question {
@Id
@GeneratedValue
private Long id;
private String description;
@ElementCollection(fetch=FetchType.EAGER)
private List<Answer> answers;
@Embeddable
public class Answer {
private String description;
private boolean correct;
JPA/数据库通过包含一个问题和三个答案的单个测验正确加载。但是,返回的相应JSON有很多重复数据。
{
"id" : 1,
"description" : "Intro to Spring 4",
"questions" : [ {
"id" : 1,
"description" : "What is Spring?",
"answers" : [ {
"description" : "A season",
"correct" : false
}, {
"description" : "A coily wire",
"correct" : false
}, {
"description" : "A wonderful framework",
"correct" : true
} ]
}, {
"id" : 1,
"description" : "What is Spring?",
"answers" : [ {
"description" : "A season",
"correct" : false
}, {
"description" : "A coily wire",
"correct" : false
}, {
"description" : "A wonderful framework",
"correct" : true
} ]
}, {
"id" : 1,
"description" : "What is Spring?",
"answers" : [ {
"description" : "A season",
"correct" : false
}, {
"description" : "A coily wire",
"correct" : false
}, {
"description" : "A wonderful framework",
"correct" : true
} ]
} ]
}
有没有人看到什么明显的东西?我用的是Jackson mapper。
https://stackoverflow.com/questions/35412605
复制相似问题