Post only the Manytomany relationship

Asked

Viewed 177 times

0

I have the school entities and teacher. I mapped the Manytomany relationship. I have no idea how to save only the list by passing an array of ids.

Teacher class

 @Id
 Long id;

 // não importa os demais atributos

Class School:

@Id
Long id;

    @ManyToMany
    @JoinTable(name = "school_x_teacher", 
       joinColumns = @JoinColumn(name = "id_school"), 
       inverseJoinColumns = @JoinColumn(name = "id_teacher"))
private List<Teacher> teacher;

I want send via Postman this:

[
 { "id": 1},
{ "id": 2},
{ "id": 3}
]

what directly in the bank, saving in the school with id = 1 would be:

INSERT INTO school_x_teacher VALUES (1,1), (1,2),(1,3);

1 answer

0

Create this class (from the Join table) fill in the data you want and send it via Json. I think it is the easiest solution, but I do not know if it is the best.

....sua ClasseDaJoinTable
private Long idSchool
private Long idTeacher

.... para enviar
List<ClasseDaJoinTable> valores = new ArrayList<>();
ClasseDaJoinTable classeDaJoinTable1 = new  ClasseDaJoinTable();
classeDaJoinTable1.setIdSchool(1);
classeDaJoinTable1.setIdTeacher(1);
valores.add(classeDaJoinTable1);

And return "values" in your Rest controller method. I hope it helps you!

Browser other questions tagged

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