Reputation: 913
I have multiple back-reference classes in a class. Since I use @JsonBackReference
for them, I get an error. I assigned @JsonIdentityInfo
annotation for those classes, but I still get the same error.
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
}
error:
com.fasterxml.jackson.databind.JsonMappingException: Multiple back-reference properties with name 'defaultReference'
How can I resolve this error? Can I not use multiple back-reference in a class?
Upvotes: 1
Views: 7540
Reputation: 848
So this did actually take me a while...
You can annotate your referencse accordingly using @JsonIdentityReference(alwaysAsId = true)
and then leave it off the master reference
@JsonIdentityReference(alwaysAsId = true)
private Set<PackInstructionGroup> groups = new TreeSet<>();
Upvotes: 0
Reputation: 1
I also faced this issue, but in the last I resolved it.
//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
}
By mentioning @JsonIgnoreProperties("inspection")
and @JsonManagedReference
in Parent class
Resolved the issue raised by using two @JSONBackRefrence
in same parent class.
Upvotes: 0
Reputation: 14383
According to Jackson's javadoc, both @JsonManagedReference
and @JsonBackReference
accept a name value that binds them together:
@JsonBackReference("a")
private A a;
@JsonManagedReference("a")
private List < X > xxA;
Upvotes: 1