通过在多对多关系中使用@JsonBackReference和@JsonManagedReference,数据丢失

时间:2016-05-07 08:05:00

标签: java json jpa jackson many-to-many

我有两个关系很多的模特。我使用 @JsonManagedReference @JsonBackReference 来避免循环。

当我检索Outage类的JSON时,它工作正常,它有一组实例值,但是当我检索Instance时,它没有设置中断。

这是我的 Outage.java

 public class Outage implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 3688202816610111558L;

    @Id
    @GeneratedValue
    @Column(name = "outage_id")
    private long outageId;

    @JsonManagedReference
    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "ManyToMany", 
    joinColumns = @JoinColumn(name = "outage_id"),
    inverseJoinColumns = @JoinColumn(name  = "instance_id"))
    private Set<Instances> instances;

    @Column(name = "start_date")
    private Date startDate;

    @Column(name = "end_date")
    private Date endDate;

    @Column(name = "start_time")
    private Time startTime;

    @Column(name = "end_time")
    private Time endTime;

    @Column(name = "outage_type")
    private int outageType;

    @Column(name = "jira_number")
    private String jiraNumber;

    @Column(name = "reason")
    private String reason;

    }

这是我的 Instances.java

public class Instances implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = -5280937805735107384L;

@Id
@GeneratedValue
@Column(name = "instance_id")
private long id;

@Column(name = "name")
private String instanceName;

@ManyToOne
@JoinColumn(name = "application_id")
private Applications applications;

@ManyToOne
@JoinColumn(name = "environment_id")
private Environments environments;

@JsonBackReference
@ManyToMany(cascade = CascadeType.ALL, mappedBy = "instances")
private Set<Outage> outages;

}

以下是我各自的 JSON

停机JSON

{
"outageList": [{
    "outageId": 40,
    "instances": [{
        "id": 10,
        "instanceName": null,
        "applications": null,
        "environments": null,
        "version": null
    }, {
        "id": 9,
        "instanceName": "UAT7",
        "applications": null,
        "environments": null,
        "version": null
    }],
    "startDate": "3916-06-06",
    "endDate": "3916-06-06",
    "startTime": "01:00:00",
    "endTime": "12:00:00",
    "outageType": 1,
    "jiraNumber": "123",
    "reason": "checking without applicaion & envi"
}]}

实例

{
"instancesList": [{
    "id": 9,
    "instanceName": "UAT7",
    "applications": null,
    "environments": null,
    "version": null
}, {
    "id": 10,
    "instanceName": null,
    "applications": null,
    "environments": null,
    "version": null
}]}

提前致谢

0 个答案:

没有答案