1
@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 ?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • To be clear, you want this bi directional relationship to be displayed and are just looking at how to handle it so that it isn't endlessly recursive? See https://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion and some others on the topic as there are quite a few options. – Chris Sep 09 '21 at 18:52
  • bi-directional relationship with all object element serialized – AdelLinux80s Sep 10 '21 at 19:23

1 Answers1

0

Do not render the entity class as the JSON response in your controller. Instead, create separate DTO classes CourseDTO and StudentDTO where you only put the data that you need in the response and return them:

@GetMapping
public StudentDTO getStudent() {
M A
  • 71,713
  • 13
  • 134
  • 174
  • in addition to this, you can use some mapping library like model mapper or mapstruct to avoid boiler plate code for mapping your entity to DTO – Sridhar Patnaik Sep 09 '21 at 19:11
  • I know I should use DTO classes,,, But does it solve my issue ? – AdelLinux80s Sep 10 '21 at 12:41
  • Yes, because you wouldn't populate the courses.students list when sending back students, or populate the students.courses list when sending back courses. you get to decide what is relevant and what isn't when building the DTO representation of your courses/student graph based on the usage requirement, before serialization occurs. – Chris Sep 10 '21 at 20:34