Change an entity by passing a list

0

Hello, I have a class teacher registered and I want to change it by passing an array of materials.

I have the following code:

class teacher:

//... atributos não importantes ao problema

    @ManyToMany
    @JoinTable(name = "teacher_x_lesson", 
    joinColumns = @JoinColumn(name = "id_teacher"), 
    inverseJoinColumns = @JoinColumn(name = "id_lesson"))
    private List<Lesson> lesson;

In the resource I arrived here but I do not know if I'm on the right path.

@PutMapping("/{id}/lesson")
    @PreAuthorize("hasAuthority('ROLE_PUT_TEACHER') and #oauth2.hasScope('wride')")
    public void putLessonTeacher(@PathVariable Long id, List<Lesson> idLesson) {

        Teacher teacher = repository.findById(id).get();

        teacher.setLesson(idLesson);

        BeanUtils.copyProperties(idLesson, teacher, "id" );

    }

In Postman I'm passing the following:

{
    "lesson": [
        {"id":1}    
    ]
}
    
asked by anonymous 11.11.2018 / 17:06

2 answers

0

Opa Cleriston,

I'm not sure if this is what you want.

But now you have to:

  • consult the materials that are registered in the database, create the array, pass it to your teacher object, and persist using repository.save(teacher).
12.11.2018 / 12:21
0

Try something like:

teacher.getLessons().addAll(asList(lesson1, lesson2, lesson3));
//getLessons() = List<Lessons> da classe Teacher
repositoryTeacher.saveAll();

I recommend you create a DTO to manipulate the inputs and outputs with information that is really needed and decouple your domain layer from the controller. I have a repository here that can help you:

    
13.11.2018 / 00:49