我想将一个Hibernate对象列表添加到JSON对象中,但我得到StackOverflowError
。
我还在@JsonBackReference("mamRequestHeaders.mamRequestLinesSet")
中MAMRequestLine.java
和@JsonManagedReference("mamRequestHeaders.mamRequestLinesSet")
中的MAMRequestHeader.java
尝试过,但问题未解决。
java.lang.StackOverflowError at java.lang.reflect.InvocationTargetException。(InvocationTargetException.java:72) 在sun.reflect.GeneratedMethodAccessor494.invoke(未知来源)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.lang.reflect.Method.invoke(Method.java:498)at org.json.JSONObject.populateMap(JSONObject.java:988)at org.json.JSONObject。(JSONObject.java:272)at org.json.JSONObject.wrap(JSONObject.java:1587)at org.json.JSONArray。(JSONArray.java:158)at org.json.JSONObject.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)at 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 :(得分:0)
在@JsonManagedReference
集合上添加@OneToMany
,在@JsonBackReference
字段上添加@ManyToOne
。请参阅下面的更新示例。
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注释取决于您所需的输出。例如,您也可以将@JsonIgnore
放在getMamRequestHeaders()
课程的MAMRequestLines
上。