Request Problems (Java - Spring Boot)

Asked

Viewed 45 times

0

Hey, guys, how’s it going? I have a code for a Rest API that registers courses and students. There is a bi-directional relationship. N Students -> 1 Course mapped as follows:

    @ManyToOne
    @JoinColumn(name = "curso_id")
    private Curso curso;

1 course -> N Students mapped as follows:

    @OneToMany
    @JoinColumn(name = "curso_id")
    private List<Aluno> alunos = new ArrayList<>();

I am using an H2 database in the application to persist the data, however, I feel that I am not able to make the request in the correct way when creating a student. Follow my student creation method:

    @PostMapping("/aluno")
    public void create(@RequestBody Aluno aluno){
        alunoRepository.save(aluno);
    }

Request json to create course(working properly):

{
    "nome": "Nutrição",
    "campus": "Campos Araguari"
}

Request json to create student(not working properly):`

{
    "nome": "Fulaninho",
    "matricula": "745896",
    "curso_id": 1
}

Select result: inserir a descrição da imagem aqui

I’ve been stuck for a while and honestly I’m not finding the problem. I have tried to pass within the student a course object with its respective Id, also does not work :(

1 answer

0

Good afternoon Alexsander, it all depends on the order that your project rotates, whether it is around the user or the course, so I saw you need to have the course already entered in the bank to then be able to insert the student, another point to consider, in your student class you have the course class as an attribute? if that cannot be so, since Springboot solves relations in this way so H2 Hibernate can enter the data in the database, considering [Course 1 -> N Student]:

Pupil:

 public class Aluno {
    
    
    @Id
    @GeneratedValue
    private Integer id;
    @OneToOne
    @JoinColumn(name="curso_id")
    private Curso curso;
    private String nome;
    private String matricula;
    
    
    //devidos metodos
    
    
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public Curso getCurso() {
        return curso;
    }
    public void setCurso(Curso curso) {
        this.curso = curso;
    }
    public String getNome() {
        return nome;
    }
    public void setNome(String nome) {
        this.nome = nome;
    }
    public String getMatricula() {
        return matricula;
    }
    public void setMatricula(String matricula) {
        this.matricula = matricula;
    }
    
    
    
}

Course:

public class Course {

@Id
@GeneratedValue
private Integer id;
private String nome;
private String Campus; // ou private Campus campus;
@OneToMany
private ArrayList<Aluno> alunos;

//devidos metodos


public Integer getId() {
    return id;
}
public void setId(Integer id) {
    this.id = id;
}
public String getNome() {
    return nome;
}
public void setNome(String nome) {
    this.nome = nome;
}
public String getCampus() {
    return Campus;
}
public void setCampus(String campus) {
    Campus = campus;
}
public ArrayList<Aluno> getAlunos() {
    return alunos;
}
public void setAlunos(ArrayList<Aluno> alunos) {
    this.alunos = alunos;
}

}

I believe that solves

Browser other questions tagged

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