0

I am trying to convert my bean to json via Gson. My bean (Logs) include another bean(Log) and second bean reference to first bean.

@Entity
@Table(name = "t_logs")
public class Logs {

    @Id
    @Column(name = "executionid")
    private String executionId;

    @Column(name = "sentdate")
    @Temporal(TemporalType.TIMESTAMP)
    private Date sentDate;

    @Column(name = "sent")
    private boolean sent;

    @OneToMany(mappedBy = "logs", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    private List<Log> logList;}


@Entity
@Table(name = "t_log")
public class Log {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private long id;

    @ManyToOne
    @JoinColumn(name = "executionid")
    private Logs logs;

    @Column(name = "startdate")
    @Temporal(TemporalType.TIMESTAMP)
    private Date startDate;

    @Column(name = "enddate")
    @Temporal(TemporalType.TIMESTAMP)
    private Date endDate;
    }

So when I try to convert Json format, I getting an error like above; java.lang.StackOverflowError

I think this is a cycle but how can I resolve it, I don't know

        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").create();
        return gson.toJson(logs, Logs.class);
stephan
  • 271
  • 1
  • 4
  • 24

4 Answers4

0

You have circular reference between Logs and log.

When you try to convert Logs as Json, It will convert log as well as its been referred. But log has reference to Logs and Logs has reference to log again and go on. So stack will grow infinitely and overflowed at one point.

Either remove the Logs reference from log or add some annotations to ignore it

Selvakumar Ponnusamy
  • 5,363
  • 7
  • 40
  • 78
0

You have to nullify the 'logList' of the Logs to avoid the infinity loop. try like this

Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").create();
logs.getLogs.setLogList(null);
return gson.toJson(logs, Logs.class);
janith1024
  • 1,042
  • 3
  • 12
  • 25
  • when I tried your suggest log.executionid setted null, so in the server side db these data executionid would be null. (no reference) – stephan Feb 24 '17 at 11:28
0

You are fetching the Logs and and eagerly you are fetching Log too, obviously its doing the continuous retrieval so it will never end, so i'm suggesting to remove "@ManyToOne" for below relation in Log entity.

@ManyToOne
@JoinColumn(name = "executionid")
private Logs logs;
Suresh Kb
  • 151
  • 1
  • 8
  • when I removed ManyToOne I got an error like above; Invocation of init method failed; nested exception is org.hibernate.MappingException: Could not determine type for: net.mycompany.model.Logs – stephan Feb 24 '17 at 11:30
0

I know that is the worst but I could not find any way!

private String convertToMyJSON(Logs logs) {

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");

    String jsonObject = "{"
            + "\"executionId\":\"" + logs.getExecutionId() + "\","
            + "\"startDate\":\"" + sdf.format(logs.getStartDate()) + "\","
            + "\"logList\":[" + getLogString(logs) + "]"
            + "}";

    return jsonObject;
}

private String getLogString(Logs logs) {

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");

    String logString = "";

    for (Log l : logs.getLogList()) {

        logString = logString
                + "{"
                + "\"id\":" + l.getId() + ","
                + "\"executionId\":\"" + logs.getExecutionId() + "\","
                + "\"startDate\":\"" + sdf.format(l.getStartDate()) + "\","
                + "\"endDate\":\"" + sdf.format(l.getEndDate()) + "\","
                + "},";
    }

    return logString.substring(0, logString.lastIndexOf(","));
}
stephan
  • 271
  • 1
  • 4
  • 24