Hibernate 双向关联转换 JSON 防止死循环
2018-09-19 本文已影响0人
asing1elife
Hibernate 中经常存在双向关联的情况,此处将数据转换为 JSON 格式则可能导致死循环
更多精彩
- 更多技术博客,请移步 asing1elife's blog
获取数据时只需要单向关联
- 这种情况下父类需要子类数据,而子类不需要父类数据
@Entity
@Table(name = "ht_feedback_template")
public class FeedbackTemplate extends BaseModel {
@OneToMany(mappedBy = "feedbackTemplate", cascade = {CascadeType.ALL}, fetch = FetchType.LAZY)
@OrderBy("indexNo asc")
private Set<FeedbackQuestion> questions;
public Set<FeedbackQuestion> getQuestions() {
return questions;
}
public void setQuestions(Set<FeedbackQuestion> questions) {
this.questions = questions;
}
}
- 在子类中对父类的引用字段加上
@JsonIgnore
即可
@Entity
@Table(name = "ht_feedback_question")
public class FeedbackQuestion extends BaseModel {
@ManyToOne
@JsonIgnore
private FeedbackTemplate feedbackTemplate;
public FeedbackTemplate getFeedbackTemplate() {
return feedbackTemplate;
}
public void setFeedbackTemplate(FeedbackTemplate feedbackTemplate) {
this.feedbackTemplate = feedbackTemplate;
}
}
获取数据时确实需要双向关联
- 在父类对子类的引用字段加上
@JsonManagedReference
@Entity
@Table(name = "ht_feedback_template")
public class FeedbackTemplate extends BaseModel {
@OneToMany(mappedBy = "feedbackTemplate", cascade = {CascadeType.ALL}, fetch = FetchType.LAZY)
@OrderBy("indexNo asc")
@JsonManagedReference
private Set<FeedbackQuestion> questions;
public Set<FeedbackQuestion> getQuestions() {
return questions;
}
public void setQuestions(Set<FeedbackQuestion> questions) {
this.questions = questions;
}
}
- 在子类中对父类的引用字段加上
@JsonBackReference
即可
@Entity
@Table(name = "ht_feedback_question")
public class FeedbackQuestion extends BaseModel {
@ManyToOne
@JsonBackReference
private FeedbackTemplate feedbackTemplate;
public FeedbackTemplate getFeedbackTemplate() {
return feedbackTemplate;
}
public void setFeedbackTemplate(FeedbackTemplate feedbackTemplate) {
this.feedbackTemplate = feedbackTemplate;
}
}