1

my class

public class Teacher

has ManyToMany relationship with Students :

public class Student

when i return teachers i want only their student ids , instead of full information about their students.

we have two Dtos (DataTransferObject) : StudentDto and TeacherDto with some variables.

what can i do to solve this problem?

I want Students as well to return only names or ids of Teachers. instead they return full dto list of teachers.

thanks.

giliermo
  • 141
  • 1
  • 8

1 Answers1

2

In your TeacherDto you shouldn't have list of StudentDtos but rather list of Integer (if Integer is type of id). Than you should implement some mapping logic, for example in the constructor, like this:

public class TeacherDto {
    private final Set<Integer> studentIds;

    public TeacherDto(Teacher teacher) {
        this.studentIds = teacher.getStudents().stream()
                .map(Student::getId)
                .collect(Collectors.toSet());
    }

    public Set<Integer> getStudentIds() {
        return studentIds;
    }
}

If you have many different DTOs, there is a lot of libraries which help you automate mapping between them.

Tuom
  • 596
  • 5
  • 19