39

I'm trying to map a json (string format) to an object and I get the following error

com.fasterxml.jackson.databind.JsonMappingException: Multiple back-reference properties with name 'defaultReference'

This is the json string

{"pledge":"74","client":"66","date":"","originId":"1","qualityId":"2","grade":"19","packing":"4","tons":"1000","fromDate":"","toDate":"","type":0,"remark":"","status":0,"area":"1531","id":-1,"refNumber":"","log":"","user":""}

This is the object

@Entity
@Table(name="movement", catalog = "wsmill3")
public class MovementView implements java.io.Serializable {
    private Integer id;
    private Integer originId;
    private Integer qualityId;
    private String refNumber;
    private Integer client;
    private String clientRef;
    private Integer grade;
    private Integer packing;
    private Integer pledge;
    private Integer area;
    private Date date;
    private Double tons;
    private Date fromDate;
    private Date toDate;
    private String remark;
    private User user;
    private Byte status;
    private String log;
    private Byte type;
    //constructor, getter and setter

and this is the code to do mapping

String data = request.getParameter("data");
ObjectMapper mapper = new ObjectMapper();
MovementView movement = mapper.readValue(data, MovementView.class);

I have no idea with this error, I did exact the same way as I read on Jackson homepage. Anyone who knows about it, please help me

Kien Dang Ngoc
  • 1,079
  • 5
  • 15
  • 25

4 Answers4

73

If you use @JsonBackReference on more than one getter/setter method in your project, you should distinguish them with a specific reference name.

Maybe only one 'defaultReference' is allowed in the latest version?

e.g

In MovementView.java

@JsonBackReference(value="user-movement")
public User getUser() {
    return user;
}

In User.java

@JsonManagedReference(value="user-movement")
public MovementView getMovementView() {
    return movementView;
}
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
stephen
  • 731
  • 4
  • 3
  • @ThatGuyGrant maybe You should name all other `JsonManagedReference`s and `JsonBackReference`s with as this example states, since there can be only one connection without the `(value = "something")`. – Aleksandar May 22 '17 at 13:11
  • shouldn't `User getMovementView()` be `MovementView getMovementView()` ? – TecHunter Jun 06 '19 at 13:14
  • Do I have to give custom and unique names on both sides if I have multiple references? – Perkone Jun 21 '22 at 08:58
2

I also faced this issue, and resolved it. You should name all JsonManagedReferences and JsonBackReference in your application.

example : @JsonManagedReference(value="user-person") @JsonBackReference(value="user-person")

Boumadien
  • 21
  • 1
1

I think the best way to handle this is using @JsonIdentityInfo annotation.see the thread which demonstrate this.How to use @JsonIdentityInfo with circular references?

  • 1
    Links to external resources are encouraged, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline. – baduker Mar 20 '18 at 18:39
0

I also faced this issue, but 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.

Resolved the issue raised by using two @JSONBackRefrence in same parent class.

Ignatius
  • 1,167
  • 2
  • 21
  • 30