Changing an entity by passing a list

Asked

Viewed 95 times

0

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

I have the following code:

teacher class:

//... 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;

At Source I got this far but I don’t know if I’m on the right track.

@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 the Postman I am passing the following:

{
    "lesson": [
        {"id":1}    
    ]
}

2 answers

0

Opa Cleriston,

I’m not sure this is what you want.

But now you have to:

  • consult the subjects that are registered in the BD , create the array, pass it to your teacher object, and persist using the repository.save(teacher).
  • Hello Leonardo, let’s say I have the teacher class registered in the bank. However the attribute list of subjects constantly change. I’m not able to make this change by sending for example 3 materials at the same time via post

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 inputs and outputs with information that is really needed and uncouple your Domain layer from the controller. I have here a Restpository that can help you:

https://github.com/diegobiazin/spring-boot-ionic-backend/blob/master/src/main/java/com/diegobiazin/cursomc/resources/CategoriaResource.java

Browser other questions tagged

You are not signed in. Login or sign up in order to post.