Jackson Fasterxml - @Jsonmanagedreference and @Jsonbackreference

Asked

Viewed 114 times

1

I’m using the notes @JsonManagedReference and @JsonBackReference to map my bidirectional relationship, with this I was able to eliminate the infinite loop bug, but it led to another problem.

I have two screens for editing simple structure of employee, company and group.

In one of the screens "group editing" I have a group of companies where I can add a list of companies that are part of this group, this screen is working OK.

The second screen of "employee editing" I can link an employee to a company. On this screen there is a grid that displays the data of employees relationship your company and group.

In this screen the grid is filled until company, already in the group the information is null. I’m sure my backend is bringing the correct information, in my debugs before returning the information to the frontend my entities are filled correctly (I have the group entity filled), I believe the problem is in Jackson, at the time of converting my list into a JSON, when it returns from the backend my "group" is already null in JSON.

public class Funcionario {

   private int id;

   private String nome;

   private Empresa empresa;
}



public class Empresa {

   private int id;

   private String nome;

   @JsonBackReference
   private Grupo grupo;
}



public class Grupo {

   private int id;

   private String nome;

   @JsonManagedReference
   private List<Empresa> empresas
}
  • You understand that the annotations you are using will ignore one side?

  • Thank you for the friendly reply, yes I understand, I think I expressed myself badly. I would like to know the correct alternatives for this case, I ended up using the @Jsonidentityinfo notation to map another instance of the company within Funcio (this is because I use only in the grid view). But maybe there’s a more elegant way to do it that I’m not finding. Thank you

  • 1

    Look friend, there are other alternatives yes, but it is more common to see the staff use @Jsonignore, is falling into disuse these two notes that you are using.

No answers

Browser other questions tagged

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