Direct self-reference leading to cycle exception Direct self-reference leading to cycle exception elasticsearch elasticsearch

Direct self-reference leading to cycle exception


In this case you need to annotate the relationships with @JsonManagedReference and @JsonBackReference like this:

 @ManyToOne @JoinColumn(name = "company_id", referencedColumnName = "id") @JsonBackReference private Company company 

And

 @OneToMany(mappedBy="company") @JsonManagedReference private Set<Employee> employee = new HashSet<Employee>();

There is a nice example here


SerializationFeature has a property called FAIL_ON_SELF_REFERENCES default is true but you can set to false to skip this kind of exception.

    ObjectMapper objectMapper = new ObjectMapper();    objectMapper.configure(SerializationFeature.FAIL_ON_SELF_REFERENCES, false);

If you are using SpringBoot you can simply add spring.jackson.serialization.fail-on-self-references=false in your application.properties


The self-reference is here:

public class DailyActivity extends ElasticSearchValue<DailyActivity> {

You're saying DailyActivity is an ElasticSearchValue<DailyActivity>, which is by itself an ElasticSearchValue<ElasticSearchValue<DailyActivity>>, and this goes on infinitely...

Update: I would break that in two classes. Create DailyActivity without subclassing ElasticSearchValue:

public class DailyActivity {  // the same content as your class above

then create another class like:

public class ElacticDailyActivity extends ElasticSearchValue<DailyActivity> {