2017-06-10 208 views
-1

我想將一個Hibernate對象列表添加到一個JSON對象中,但我得到StackOverflowError把一個Hibernate對象列表放到一個JSON對象中

我也試過@JsonBackReference("mamRequestHeaders.mamRequestLinesSet")MAMRequestLine.java@JsonManagedReference("mamRequestHeaders.mamRequestLinesSet")MAMRequestHeader.java但是問題沒有解決。

java.lang.StackOverflowError的在 java.lang.reflect.InvocationTargetException。(InvocationTargetException.java:72) 在sun.reflect.GeneratedMethodAccessor494.invoke(未知來源)在 sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43) at org.json.JSONObject.populateMap(JSONObject.java:988)at org.json.JSONObject。((java.lang.reflect.Method.invoke(Method.java:498)at JSONObject.java:272) org.json.JSONObject.wrap(JSONObject.java:1587)at org.json.JSONArray。(JSONArray.java:158)at org.json.JSONO bject.wrap(JSONObject.java:1569)at org.json.JSONObject.populateMap(JSONObject.java:990)at org.json.JSONObject。(JSONObject.java:272)at org.json.JSONObject.wrap (JSONObject.java:1587)在 org.json.JSONObject.populateMap(JSONObject.java:990)

MAMRequestHeaders

@Entity(name="MAMRequestHeaders") 
@Table(name = "MAM_REQUEST_HEADERS", uniqueConstraints = @UniqueConstraint(columnNames = {"REQUEST_TYPE", "NUM_REQUEST_MREQH"})) 
public class MAMRequestHeaders implements Serializable { 

    private long id ; 
    private Set<MAMRequestLines> mamRequestLinesSet; 

    @Id 
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "MAM_REQUEST_HEADERS_SEQ") 
    @SequenceGenerator(name = "MAM_REQUEST_HEADERS_SEQ", sequenceName = "MAM_REQUEST_HEADERS_SEQ") 
    @Column(name = "REQUEST_HEADER_ID") 
    public long getId() { 
     return id; 
    } 

    public void setId(long id) { 
     this.id = id; 
    } 

    @OneToMany(mappedBy = "mamRequestHeaders", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true) 
    @OnDelete(action = OnDeleteAction.CASCADE) 
    public Set<MAMRequestLines> getMamRequestLinesSet() { 
     return mamRequestLinesSet; 
    } 

    public void setMamRequestLinesSet(Set<MAMRequestLines> mamRequestLinesSet) { 
     this.mamRequestLinesSet = mamRequestLinesSet; 
    } 

} 

MAMRequestLines

@Entity(name = "MAMRequestLines") 
@Table(name = "MAM_REQUEST_LINES", uniqueConstraints = @UniqueConstraint(columnNames = {"REQUEST_HEADER_ID", "NUM_LINE_MREQL"})) 
public class MAMRequestLines implements Serializable { 

    private Long id; 
    @JsonIgnore 
    private MAMRequestHeaders mamRequestHeaders; 

    @Id 
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "MAM_REQUEST_Line_SEQ") 
    @SequenceGenerator(name = "MAM_REQUEST_Line_SEQ", sequenceName = "MAM_REQUEST_Line_SEQ") 
    @Column(name = "REQUEST_LINE_ID") 
    public Long getId() { 
     return id; 
    } 

    public void setId(Long id) { 
     this.id = id; 
    } 

    @ManyToOne(fetch = FetchType.EAGER) 
    @JoinColumn(name = "REQUEST_HEADER_ID", nullable = false) 
    public MAMRequestHeaders getMamRequestHeaders() { 
     return mamRequestHeaders; 
    } 

    public void setMamRequestHeaders(MAMRequestHeaders mamRequestHeaders) { 
     this.mamRequestHeaders = mamRequestHeaders; 
    } 

} 

創建結果

List<MAMRequestHeaders> mamRequestHeadersList = manager.createQuery(queryStr, MAMRequestHeaders.class) 
JSONObject result = new JSONObject(); 
result.put("resultList", mamRequestHeadersList); 

回答

0

@ManyToOne場添加@JsonManagedReference@OneToMany收集和@JsonBackReference。請參閱下面的更新示例。

MAMRequestHeaders

@Entity(name="MAMRequestHeaders") 
@Table(name = "MAM_REQUEST_HEADERS", uniqueConstraints = @UniqueConstraint(columnNames = {"REQUEST_TYPE", "NUM_REQUEST_MREQH"})) 
public class MAMRequestHeaders implements Serializable { 

    private long id; 

    @JsonBackReference 
    private Set<MAMRequestLines> mamRequestLinesSet; 

    @Id 
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "MAM_REQUEST_HEADERS_SEQ") 
    @SequenceGenerator(name = "MAM_REQUEST_HEADERS_SEQ", sequenceName = "MAM_REQUEST_HEADERS_SEQ") 
    @Column(name = "REQUEST_HEADER_ID") 
    public long getId() { 
     return id; 
    } 

    public void setId(long id) { 
     this.id = id; 
    } 

    @OneToMany(mappedBy = "mamRequestHeaders", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true) 
    @OnDelete(action = OnDeleteAction.CASCADE) 
    public Set<MAMRequestLines> getMamRequestLinesSet() { 
     return mamRequestLinesSet; 
    } 

    public void setMamRequestLinesSet(Set<MAMRequestLines> mamRequestLinesSet) { 
     this.mamRequestLinesSet = mamRequestLinesSet; 
    } 

} 

MAMRequestLines

@Entity(name = "MAMRequestLines") 
@Table(name = "MAM_REQUEST_LINES", uniqueConstraints = @UniqueConstraint(columnNames = {"REQUEST_HEADER_ID", "NUM_LINE_MREQL"})) 
public class MAMRequestLines implements Serializable { 

    private Long id; 

    @JsonManagedReference 
    private MAMRequestHeaders mamRequestHeaders; 

    @Id 
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "MAM_REQUEST_Line_SEQ") 
    @SequenceGenerator(name = "MAM_REQUEST_Line_SEQ", sequenceName = "MAM_REQUEST_Line_SEQ") 
    @Column(name = "REQUEST_LINE_ID") 
    public Long getId() { 
     return id; 
    } 

    public void setId(Long id) { 
     this.id = id; 
    } 

    @ManyToOne(fetch = FetchType.EAGER) 
    @JoinColumn(name = "REQUEST_HEADER_ID", nullable = false) 
    public MAMRequestHeaders getMamRequestHeaders() { 
     return mamRequestHeaders; 
    } 

    public void setMamRequestHeaders(MAMRequestHeaders mamRequestHeaders) { 
     this.mamRequestHeaders = mamRequestHeaders; 
    } 

} 

但實際上配置JSON註解取決於你所需的輸出。例如,您也可以在MAMRequestLines類中的getMamRequestHeaders()上放置@JsonIgnore

+0

感謝您的回覆。但它還沒有工作! – faraa

相關問題