将Hibernate对象列表放入JSON对象中

时间:2017-06-10 11:51:43

标签: java json spring hibernate

我想将一个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);

1 个答案:

答案 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上。