我想將一個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);
感謝您的回覆。但它還沒有工作! – faraa