1. 程式人生 > >JPA一堆多迴圈引用錯誤 HttpMessageNotWritableException

JPA一堆多迴圈引用錯誤 HttpMessageNotWritableException

發生了一個這樣的錯誤

Resolved exception caused by Handler execution: org.springframework.http.converter.HttpMessageNotWritableException:
Could not write JSON document: Infinite recursion (StackOverflowError)

解決參照:

注意:ManyToOne屬性上註解:

@JsonBackReference

沒有起作用,在get方法上註解成功解決無線引用導致堆疊溢位的問題。

對onetomany的一方沒有加任何的註解也可以從json轉換到物件

packagecom.troy.keeper.monomer.demo.domain;
importcom.fasterxml.jackson.annotation.JsonBackReference;
importcom.fasterxml.jackson.annotation.JsonManagedReference;
importjavax.persistence.*;
importjava.util.List;
importjava.util.Set;
/**
 * 建立者: apple
 * 建立時間:2017/9/21.
 * 功能模組:
*/
@Entity
@Table(name = 
"testdazbibiao") publicclassTESTDazhibiao { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) privateLong id; @Column(name = "NAME") privateString name; @Column(name = "SCORE") privateString score; @OneToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER) @JoinTable(name = "zhibao_xifenzhibiao",
joinColumns ={@JoinColumn(name = "testdazbibiaoid")},inverseJoinColumns = {@JoinColumn(name ="testxifenzhibiaoid" )}) privateList<TESTXifenzhibiao> children; publicList<TESTXifenzhibiao> getChildren() { returnchildren; } publicvoidsetChildren(List<TESTXifenzhibiao> children) { this.children = children; } publicLong getId() { returnid; } publicvoidsetId(Long id) { this.id = id; } publicString getName() { returnname; } publicvoidsetName(String name) { this.name = name; } publicString getScore() { returnscore; } publicvoidsetScore(String score) { this.score = score; } }
packagecom.troy.keeper.monomer.demo.domain;
importcom.fasterxml.jackson.annotation.JsonBackReference;
importcom.fasterxml.jackson.annotation.JsonIgnore;
importcom.fasterxml.jackson.annotation.JsonManagedReference;
importjavax.persistence.*;
importjava.util.List;
importjava.util.Set;
/**
 * 建立者: apple
 * 建立時間:2017/9/21.
 * 功能模組:
*/
@Entity
@Table(name = "testxifenzhibiao")
publicclassTESTXifenzhibiao {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
privateLong id;
@Column(name = "NAME")
privateString name;
@Column(name = "SCORE")
privateString score;
@Column(name = "GETMETHOD")
privateString method;
@ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "testdazbibiaoid")
//生成JSON時候忽略
//
privateTESTDazhibiao TESTDazhibiao;
@OneToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
    @JoinTable(name = "xifenzhibiao_parameter",joinColumns ={@JoinColumn(name = "testxifendazbibiaoid")},inverseJoinColumns = {@JoinColumn(name ="testparameterid" )})
privateList<TESTParameter> parameterList;
@JsonBackReference
publiccom.troy.keeper.monomer.demo.domain.TESTDazhibiao getTESTDazhibiao() {
returnTESTDazhibiao;
}
publicList<TESTParameter> getParameterList() {
returnparameterList;
}
publicvoidsetParameterList(List<TESTParameter> parameterList) {
this.parameterList = parameterList;
}
publicvoidsetTESTDazhibiao(com.troy.keeper.monomer.demo.domain.TESTDazhibiao TESTDazhibiao) {
this.TESTDazhibiao = TESTDazhibiao;
}
publicLong getId() {
returnid;
}
publicvoidsetId(Long id) {
this.id = id;
}
publicString getName() {
returnname;
}
publicvoidsetName(String name) {
this.name = name;
}
publicString getScore() {
returnscore;
}
publicvoidsetScore(String score) {
this.score = score;
}
publicString getMethod() {
returnmethod;
}
publicvoidsetMethod(String method) {
this.method = method;
}
}

服務中新增大指標,同時會級聯儲存

publicvoidaddDazhibiao(TESTDazhibiao dazhibiao){
List<TESTXifenzhibiao> xifenzhibiaoList =dazhibiao.getChildren();
if(xifenzhibiaoList !=null) {
for(TESTXifenzhibiao xifen : xifenzhibiaoList
                ) {
List<TESTParameter> parameters = xifen.getParameterList();
if(parameters != null) {
for(TESTParameter p : parameters
                        ) {
                    p.setTESTxifenzhibiao(xifen);
}
            }
            xifen.setTESTDazhibiao(dazhibiao);
}
    }
tesTzhibiaoRepository.save(dazhibiao);
}
查詢大指標的同時也會級聯獲取
@Override
publicList<TESTXifenzhibiao> queryAllXIFENDazhibiao(intfatherid){
List<TESTXifenzhibiao> t = newArrayList<>();
returnt;
}