Java Jackson:多个反向引用属性,名称为“defaultReference”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20119142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:17:26  来源:igfitidea点击:

Hymanson: Multiple back-reference properties with name 'defaultReference'

javaspringhibernateHymanson

提问by Kien Dang Ngoc

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

我正在尝试将 json(字符串格式)映射到一个对象,但出现以下错误

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

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

This is the json string

这是json字符串

{"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 Hymanson homepage. Anyone who knows about it, please help me

我不知道这个错误,我的做法和我在Hyman逊主页上读到的完全一样。哪位知道的请帮帮我

回答by stephen

If you use @JsonBackReference on your getter/setter method in your project more than twice,you should distinguish them with a specific Reference name.Maybe only one 'defaultReference' is allowed in the latest version.

如果你在你的项目中的 getter/setter 方法上使用 @JsonBackReference 超过两次,你应该用一个特定的引用名称来区分它们。也许最新版本中只允许一个“defaultReference”。

e.g

例如

in MovementView.class

在 MovementView.class 中

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

in User.class

在 User.class 中

    @JsonManagedReference(value="user-movement")
    public User getMovementView() {
    return movementView;
}

回答by Vivek Gangwar

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.

通过提及@JsonIgnoreProperties("inspection")@JsonManagedReference

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

解决了@JSONBackRefrence在同一个父类中使用两个引起的问题。

回答by Gayan Prasanna

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?

我认为处理这个问题的最好方法是使用 @JsonIdentityInfo 注释。请参阅演示这一点的线程。如何将@JsonIdentityInfo 与循环引用一起使用?