我在一个类中有多个反向引用类。由于我使用@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:具有名称&#39; defaultReference&#39;
的多个反向引用属性
如何解决此错误?我可以不在课堂上使用多个反向引用吗?
答案 0 :(得分:1)
根据Jackson's javadoc,@JsonManagedReference
和@JsonBackReference
都接受将它们绑定在一起的名称值:
@JsonBackReference("a")
private A a;
@JsonManagedReference("a")
private List < X > xxA;
答案 1 :(得分:0)
我也遇到过这个问题,但在最后我解决了这个问题。
//This is parent class
@Entity
@Table(name = "checklist")
@JsonIgnoreProperties("inspection")
public class Checklist implements java.io.Serializable {
@ManyToOne
@JoinColumn(name = "product_id", referencedColumnName = "id")
@JsonBackReference
private Product product;
@OneToMany(mappedBy = "checklists", cascade = CascadeType.ALL)
@JsonManagedReference
private Set<Inspection> inspection = new HashSet<Inspection>();
//Constructor
//Getter and Setter
}
//This is child class
@Entity
@Table(name = "inspections")
public class Inspection {
@ManyToOne
@JoinColumn(name = "chk_id", referencedColumnName = "id")
private Checklist checklists;
//Constructor
//Getter and Setter
}
在Parent class中提及@JsonIgnoreProperties("inspection")
和@JsonManagedReference
解决了在同一个父类中使用两个@JSONBackRefrence
引发的问题。
答案 2 :(得分:0)
所以这确实花了我一段时间......
您可以使用 @JsonIdentityReference(alwaysAsId = true)
相应地注释您的引用,然后将其从主引用中删除
@JsonIdentityReference(alwaysAsId = true)
private Set<PackInstructionGroup> groups = new TreeSet<>();