Relationship 1:N in Java

Asked

Viewed 130 times

0

I have an API and I’m creating a relationship between Cliente and AtividadeSecundaria, where a client has several activities and the activities belong to a client, however, when I make the consultation by Postman he returns a error 500 whether it was not possible to write JSON. I noticed that it also looks for a table that does not exist:

Table 'teste.cliente_atividade_secundaria' doesn't exist

but I don’t know why I’m looking for this table.

Class Client:

@OneToMany
private List<AtividadeSecundaria> atividadeSecundaria;

public List<AtividadeSecundaria> getAtividadeSecundaria() 
{
    return atividadeSecundaria;
}

public void setAtividadeSecundaria(List<AtividadeSecundaria> atividadeSecundaria) {
    this.atividadeSecundaria = atividadeSecundaria;
}

Class Activity:

@ManyToOne
@JoinColumn(name = "fk_cliente")
private Cliente cliente;

Console error:

SQL Error: 1146, SQLState: 42S022019-08-06 16:22:16.695 ERROR 17748 --- [nio8080-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper   : Table 'teste.cliente_atividade_secundaria' doesn't exist
2019-08-06 16:22:16.711  WARN 17748 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: could not extract ResultSet; nested exception is com.fasterxml.jackson.databind.JsonMappingException: could not extract ResultSet (through reference chain: java.util.ArrayList[0]->com.teste.agenda.model.Cliente["atividadeSecundaria"])]

1 answer

3


I managed to solve the problem, I’ll leave the answer here if you need.

Class Client:

@OneToMany(mappedBy = "cliente")
private List<AtividadeSecundaria> atividadeSecundaria;

Class Activity:

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "fk_cliente")
private Cliente cliente;
  • 2

    Mark your answer as correct :)

Browser other questions tagged

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