Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Direct self-reference leading to cycle Superclass issue JSON

I have tried several things I found while searching but nothing helped or I did not implement it correctly.

Error I'm getting

Direct self-reference leading to cycle (through reference chain: io.test.entity.bone.Special["appInstance"]->io.test.entity.platform.ApplicationInstance["appInstance"])

Both these extend the base entity and in the base (super class) it has an appInstance as well.

Base entity looks similar to this

@MappedSuperclass
public abstract class BaseEntity implements Comparable, Serializable {

@ManyToOne
protected ApplicationInstance appInstance;

//getter & setter

}

Application entity looks like this

public class ApplicationInstance extends BaseEntity implements Serializable { 
   private List<User> users;
// some other properties (would all have the same base and application instance . User entity will look similar to the Special.)
}

Special entity

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "objectType")
@JsonIgnoreProperties({"createdBy", "appInstance", "lastUpdatedBy"})
public class Special extends BaseEntity implements Serializable {

    @NotNull
    @Column(nullable = false)
    private String name;

    @Column(length = Short.MAX_VALUE)
    private String description;

    @NotNull
    @Column(nullable = false)
    private Double price;

    @OneToOne
    private Attachment image;

    @Enumerated(EnumType.STRING)
    @ElementCollection(targetClass = SpecialTag.class)
    @CollectionTable(name = "special_tags")
    @Column(name = "specialtag")
    private List<SpecialTag> specialTags;

    @Temporal(TemporalType.TIME)
    private Date specialStartTime;

    @Temporal(TemporalType.TIME)
    private Date specialEndTime;

    @Enumerated(EnumType.STRING)
    @ElementCollection(targetClass = WeekDay.class)
    @CollectionTable(name = "available_week_days")
    @Column(name = "weekday")
    private List<WeekDay> availableWeekDays;

    @OneToMany(mappedBy = "special", cascade = CascadeType.REFRESH)
    private List<SpecialStatus> statuses;

    @OneToMany(mappedBy = "special", cascade = CascadeType.REFRESH)
    private List<SpecialReview> specialReviews;

    @Transient
    private Integer viewed;

    private Boolean launched;

    @OneToMany(mappedBy = "special")
    private List<CampaignSpecial> specialCampaigns;


  @Override
  @JsonIgnore
  public ApplicationInstance getAppInstance() {
    return super.getAppInstance(); 
  }
}

All entities in Special inherits from BaseEntity which contains AppInstance

then i have a method to get the special

@GET
@Path("{ref}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(value = MediaType.TEXT_PLAIN)
public Special findByGuestRef(@PathParam("ref") String pRefeference) {
  // find the special and return it
 return special;
}

On the special entity I tried the following

  • Added jsonIgnoreProperties
  • Added an override for appInstance to annotate with @JsonIgnore
  • @JsonIdentityInfo

links for the above

  • https://stackoverflow.com/a/29632358/4712391
  • Jackson serialization: how to ignore superclass properties
  • jackson self reference leading to cycle

none of those solutions works. Am I doing something wrong?

Note: Would it also just be possible to edit special, since the other entities are in a different package and would not like to edit them.

like image 663
Tinus Jackson Avatar asked Aug 16 '17 13:08

Tinus Jackson


1 Answers

To me, problem seems to be in the Special object and the fields being initialized in it. I guess that there is a circular reference detected when serialisation happens. Something similar to:

class A {
    public A child;
    public A parent;
}

A object = new A();
A root = new A();
root.child = object;
object.parent = root;

In the above code, whenever you will try to seralize either of these objects, you will face the same problem. Note that public fields are not recommended.

I'll suggest to peek into your Special object and the references set in it.

like image 123
Sunil Singhal Avatar answered Oct 04 '22 11:10

Sunil Singhal