Relationship between Resources Rest with Spring boot

2

I'm learning Spring Boot Rest and with a question I can not solve on my own, could you help me?

I created the following mapping between the Launch and Person entities:

Person Entity:

   @Entity
   public class Pessoa {

    ...

    @JsonIgnore
    @OneToMany(mappedBy="pessoa")
    private List<Lancamento> lancamentos;
    //getters setters
    }

Entity Launch:

@Entity
@Table(name="lancamento")
public class Lancamento {
...
@NotNull
@ManyToOne
@JoinColumn(name="id_pessoa")
private Pessoa pessoa;
//getters setters
}

Expected Result:

{
  "nome":"Pessoa1",
   lancamentos:[
         {
           "id":"1",
           "descricao":"Educacao"
         },
         {
          "id":"2",
          "descricao":"Alimentacao"
         },
       ]
}

Result Obtained:

{
  "nome":"Pessoa1"
}

What am I doing wrong?

    
asked by anonymous 17.08.2018 / 03:48

1 answer

2

I found the solution in the Post link , which gave me the expected result. Just use annotations @JsonManagedReference and @JsonBackReference of Jackson .

The code looks like this:

Person Entity:

   @Entity
   public class Pessoa {

    ...
    @JsonManagedReference
    @JsonIgnore
    @OneToMany(mappedBy="pessoa")
    private List<Lancamento> lancamentos;
    //getters setters
    }

Entity Launch:

@Entity
@Table(name="lancamento")
public class Lancamento {
...
@JsonBackReference
@NotNull
@ManyToOne
@JoinColumn(name="id_pessoa")
private Pessoa pessoa;
//getters setters
   }
    
17.08.2018 / 18:54