@Data
@Entity
@Table(name="course")
public class Course {
@Id
@GeneratedValue
private Long id;
private String title;
private int credit;
private String location;
private String session;
@ManyToMany(fetch = FetchType.LAZY,cascade = CascadeType.ALL, mappedBy="courses")
@JsonIgnore
private List<Student> students = new ArrayList<>();
public void addStudent(Student student) {
students.add(student);
}
public void removeStudent(Student student) {
students.remove(student);
}
}
@Data
@Entity
@Table(name="student")
public class Student {
@Id
@GeneratedValue
private Long id;
private String firstName;
private String lastName;
private String contactNumber;
private int GPA;
private String email;
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "student_course",
joinColumns = {@JoinColumn(name= "student_id")},
inverseJoinColumns = {@JoinColumn(name="course_id")})
private List<Course> courses = new ArrayList<>();
public void addCourse(Course course) {
courses.add(course);
}
public void removeCourse(Course course) {
courses.remove(course);
}
}
To evade the loop I added the @JsonIgnore on the COURSE side which made the output for the STUDENT looks like this:
{
"id": 7,
"firstName": "Lil",
"lastName": "Dogg",
"contactNumber": "002002",
"email": "lil@yahoo.com",
"courses": [
{
"id": 2,
"title": "M103",
"credit": 3,
"location": "Lab30",
"session": "10:00"
}
],
"gpa": 2
}
that's Ok. But on the COURSE the output looks like this:
{
"id": 1,
"title": "M105",
"credit": 2,
"location": "Lab50",
"session": "08:00"
},
{
"id": 2,
"title": "M103",
"credit": 3,
"location": "Lab30",
"session": "10:00"
},
It is omitting the private List<Student> students = new ArrayList<>();
!
It is not showing in the Json output!... when I add the @JsonIgnore on the Student side... It omits the private List<Course> courses = new ArrayList<>();
as shown above!
How to solve this ?