Creating online store. I need to generate json that represents category tree, but when Im generating it got StackOverflowError.
Category Entity :
@OneToMany
@JoinColumn(name = "parent_category_id")
private List<Category> subcategories;
@ManyToOne
@JoinColumn(name = "parent_category_id")
private Category parentCategory;
Json generating method, I send List from Controller to represent tree
@Override
public List<Category> getCategoryTree() {
List<Category> categories = categoryDao.findAll();
List<Category> roots = categories.stream()
.filter(category -> category.getParentCategory()!=null)
.collect(Collectors.toList());
return roots;
}
I guess its because child gets parent and parent get child. But i cant place @JsonIgnore annotation because then it will write only list of parents without children or all children without parents, is there must be a way to generate json