Why Jackson PropertyGenerator prevents a recursive loop

I have two objects that display bi-directionally. A vehicle with a collection of registrations and registration. These objects are displayed as REST services.

@Entity
@XmlRootElement
public class Vehicle implements Serializable {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;
  private String brand;
  private String type;

  @OneToMany(mappedBy = "vehicle", fetch = FetchType.EAGER)
  private List<Registration> registrations;
}

The problem is that FetchType.EAGER creates infinite recursion .

@Entity
@XmlRootElement
public class Registration implements Serializable {
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;

  private String name;

  @ManyToOne
  private Vehicle vehicle;
}

After some research, it turned out that in order to solve the problem, I need to add the @JsonIdentityInfo annotation (generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") to the Vehicle class.

My question is: what does the PropertyGenerator class do?

+4
source share
1

- FetchType.EAGER. FetchType.EAGER JPA , Jackson.

, , , , , .. .

, @JsonIdentityInfo , - PropertyGenerator, . JavaDoc @JsonIdentityInfo , :

@JsonIdentityInfo , , , ( ), , , . .

, JSON/XML. , , .

. Jackson JSON Hibernate JPA ( , , ). @JsonIgnore @JsonManagedReference @JsonBackReference, .

, , PropertyGenerator: . "id" , JSON/XML , 342, , , , 342 .

+4

Source: https://habr.com/ru/post/1613172/


All Articles