0

I am trying to generate a JSON

    Gson gson = new Gson();
String json = gson.toJson(item);

But every time i try to I keep getting a stackoverflow error:

java.lang.StackOverflowError: null
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:239)
at com.google.gson.Gson$FutureTypeAdapter.write(Gson.java:968)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:112)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:239)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:112)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:239)
at com.google.gson.Gson$FutureTypeAdapter.write(Gson.java:968)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:112)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:239)

It is caused when i try to convert DataTableResults class which contains a list of Transactions.class as its property field to a string.

The DataTableResult class looks like this :

    public class DataTableResults<T> {

    /** The draw. */
    private String draw;

    /** The records filtered. */
    private String recordsFiltered;

    /** The records total. */

    private String recordsTotal;

    /** The list of data objects. */
    @SerializedName("data")
    List<T> listOfDataObjects;

    /**
     * Gets the draw.
     *
     * @return the draw
     */
    public String getDraw() {
        return draw;
    }

    /**
     * Sets the draw.
     *
     * @param draw the draw to set
     */
    public void setDraw(String draw) {
        this.draw = draw;
    }

    /**
     * Gets the records filtered.
     *
     * @return the recordsFiltered
     */
    public String getRecordsFiltered() {
        return recordsFiltered;
    }

    /**
     * Sets the records filtered.
     *
     * @param recordsFiltered the recordsFiltered to set
     */
    public void setRecordsFiltered(String recordsFiltered) {
        this.recordsFiltered = recordsFiltered;
    }

    /**
     * Gets the records total.
     *
     * @return the recordsTotal
     */
    public String getRecordsTotal() {
        return recordsTotal;
    }

    /**
     * Sets the records total.
     *
     * @param recordsTotal the recordsTotal to set
     */
    public void setRecordsTotal(String recordsTotal) {
        this.recordsTotal = recordsTotal;
    }

    /**
     * Gets the list of data objects.
     *
     * @return the listOfDataObjects
     */
    public List<T> getListOfDataObjects() {
        return listOfDataObjects;
    }

    /**
     * Sets the list of data objects.
     *
     * @param listOfDataObjects the listOfDataObjects to set
     */
    public void setListOfDataObjects(List<T> listOfDataObjects) {
        this.listOfDataObjects = listOfDataObjects;
    }

}

while the Transactions.class looks like this

    @Entity
@Table(name = "TRANS")
public class Transactions extends DefaultEntity {

    public enum TRANSACTIONTYPE{WITHDRAW, DEPOSIT, WIN, LOSS}

    @ManyToOne(targetEntity = User.class)
    @JoinColumn(name="player")
    private User player;

    private double amount;

    @Enumerated(EnumType.STRING)
    private TRANSACTIONTYPE transactiontype;

    private String referenceID;

    private String detail;



    private String token;


    public TRANSACTIONTYPE getTransactiontype() {

        return transactiontype;

    }

    public Transactions setTransactiontype(TRANSACTIONTYPE transactiontype) {

        this.transactiontype = transactiontype;

        return this;
    }



    public double getAmount() {
        return amount;
    }

    public Transactions setAmount(double amount) {
        this.amount = amount;
        return this;
    }

    public String getReferenceID() {

        return referenceID;
    }

    public Transactions setReferenceID(String referenceID) {

        this.referenceID = referenceID;

        return this;

    }

    public String getDetail() {

        return detail;

    }

    public Transactions setDetail(String detail) {

        this.detail = detail;

        return this;
    }

    public String getToken() {

        return token;

    }

    public Transactions setToken(String token) {

        this.token = token;

        return this;

    }


    public User getPlayer() {
        return player;
    }

    public Transactions setPlayer(User player) {
        this.player = player;
        return this;
    }
}

according to this post, it is supposed to be caused by a circular dependency, but in my case it is not cause i dont have one. What else could cause such error ?

Oto-obong Eshiett
  • 1,459
  • 3
  • 18
  • 33
  • You have a @ ManyToOne relationship with User, are you sure User does not have declared @ OneToMany relationship to Transaction? – Adina Rolea Oct 13 '18 at 14:44

1 Answers1

0

You should remove extends DefaultEntity, you don't need it and may bring circular dependency.

Also you have a @ManyToOne relationship with User, that may cause circular dependency if User also have a reference to Transactions. If you have it in both parts, you should exclude it from serialization with transient on one part at least.

Adina Rolea
  • 2,031
  • 1
  • 7
  • 16