我正在从事一个教育目的项目,(课程表,课程表=一对多)与(课程表,作业表=一对多)之间存在关系,首先我插入课程info(title,description)然后再插入另一个POST请求,然后插入课程,然后再发出第三个请求,POST插入作业,但是当我尝试使用GET请求获得课程时,发生的事情是我得到了很多像这样重复的JSON格式
我将在春季靴子中首先向您展示模型
课程模型(删除了一些不相关的代码以使其简短):
@Entity
public class Course {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@NotNull
@Column(nullable = false)
private String title;
@Lob
private String description;
@NotNull
@Column(nullable = false)
private String published;
@NotNull
@Column(nullable = false)
private Date publishedOn;
@OneToMany(mappedBy = "course")
private List<Assignment> assignments = new ArrayList<>();
@OneToMany(mappedBy = "course")
private List<Lesson> lessons = new ArrayList<>();
public Course() {}
public Course(String title, String description,
String published, Date publishedOn) {
this.title = title;
this.description = description;
this.published = published;
this.publishedOn = publishedOn;
}
public List<Assignment> getAssignments() {
return assignments;
}
public void addAssignments(Assignment assignment) {
this.assignments.add(assignment);
}
public void removeAssignments(Assignment assignment) {
this.assignments.remove(assignment);
}
public List<Lesson> getLessons() {
return lessons;
}
public void addLessons(Lesson lesson) {
this.lessons.add(lesson);
}
public void removeLessons(Lesson lesson) {
this.lessons.remove(lesson);
}
}
课程模型(删除了一些不相关的代码):
@Entity
public class Lesson {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@NotNull
@Lob
@Column(nullable = false)
private String reading;
@ManyToOne
private Course course;
public Lesson() {}
public Lesson(String reading, Course course) {
this.reading = reading;
this.course = course;
}
public String getReading() {
return reading;
}
public void setReading(String reading) {
this.reading = reading;
}
public Course getCourse() {
return course;
}
public void setCourse(Course course) {
this.course = course;
}
}
与分配模型相同,我将向您展示如何插入课程
pubcli ResponseEntity addLesson(long id, LessonDto lessonDto) {
Course course = courseRepository.findCourseById(id);
Lesson lesson = new Lesson();
lesson.setReading(lessonDto.getReading());
lesson.setCourse(course);
course.addLessons(lesson);
lessonRepository.save(lesson);
return new ResponseEntity("Lesson added to this course -" +
course.getTitle() + "-", HttpStatus.OK);
}