搜到很多类似的问题,但是没有看到一般的解决方案,返回级联对象的json格式咋这个费劲呢
大家怎么解决的呢
@ResponseBody
public ResponseEntity<HttpEntity> apiCourseAdd(@Valid Course course, Model model) {
if (course.getId() == null || course.getId() == 0) {
for (CourseTag courseTag :course.getCourseTags()) {
courseTag.setCourse(course);
}
courseService.addCourse(course);
}
model.addAttribute("course", course);
return new ResponseEntity(model, HttpStatus.CREATED);
}
一方实体
@JsonIgnoreProperties(ignoreUnknown=true)
public class Course extends IdEntity {
private static final long serialVersionUID = 2118439396249691137L;
private String name;
private User createBy;
private List<CourseTag> courseTags;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "course",cascade = CascadeType.ALL)
public List<CourseTag> getCourseTags() {
return courseTags;
}
public void setCourseTags(List<CourseTag> courseTags) {
this.courseTags = courseTags;
}
@ManyToOne(fetch = FetchType.LAZY)
public User getCreateBy() {
return createBy;
}
public void setCreateBy(User createBy) {
this.createBy = createBy;
}
}
多方实体
@JsonIgnoreProperties(ignoreUnknown=true)
public class CourseTag extends IdEntity {
private static final long serialVersionUID = 7790694935170125722L;
private Integer region;
private Integer tagId;
private Course course;
public Integer getRegion() {
return region;
}
public void setRegion(Integer region) {
this.region = region;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "courseId")
public Course getCourse() {
return course;
}
public void setCourse(Course course) {
this.course = course;
}
public Integer getTagId() {
return tagId;
}
public void setTagId(Integer tagId) {
this.tagId = tagId;
}
}
1 个解决方案
#1
露珠 解决了没 怎么解决的
#1
露珠 解决了没 怎么解决的