我在一个类中有多个反向引用类。由于我对它们使用@JsonBackReference,因此会出现错误。我为这些类分配了@JsonIdentityInfo批注,但仍然遇到相同的错误。

public class X implements Serializable {
  ....
  //bi-directional many-to-one association to Booking
  @ManyToOne(fetch = FetchType.EAGER)
  @JoinColumn(name = "xxA", nullable = false)
  @JsonBackReference
  private A a;

  //bi-directional many-to-one association to Client
  @ManyToOne(fetch = FetchType.EAGER)
  @JoinColumn(name = "xxB", nullable = false)
  @JsonBackReference
  private B b;
  ...getters setters
}

@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@id")
public class B implements Serializable {
  ........
  //bi-directional many-to-one association to BookedClient
  @OneToMany(mappedBy = "b", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
  @JsonManagedReference
  private List < X > xxB;
  ........ getters setters
}


@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@id")
public class A implements Serializable {
  ........
  //bi-directional many-to-one association to BookedClient
  @OneToMany(mappedBy = "a", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
  @JsonManagedReference
  private List < X > xxA;
  ........ getters setters
}


错误:


  com.fasterxml.jackson.databind.JsonMappingException:名称为“ defaultReference”的多个反向引用属性


如何解决此错误?我不能在一个类中使用多个反向引用吗?

最佳答案

根据Jackson's javadoc@JsonManagedReference@JsonBackReference都接受将它们绑定在一起的名称值:

  @JsonBackReference("a")
  private A a;

  @JsonManagedReference("a")
  private List < X > xxA;

关于java - 名称为“defaultReference”的多个反向引用属性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43202883/

10-16 00:16