I have mysql table like this :
created with spring jpa
@Where(clause = "active =1") @Entity @Table(name = "category", catalog = "businessin") public class Category implements java.io.Serializable { @Id @GeneratedValue(strategy = IDENTITY) @Column(name = "id", unique = true, nullable = false) private Integer id; private String name; private Integer parentId; private Integer active; private String pic; @JsonIgnore @OneToMany(fetch = FetchType.LAZY, mappedBy = "category") private List<Product> products = new ArrayList<Product>(); //setters and getters }
I’m building a RESTful API using spring REST & spring data (JpaRespositories) with spring-boot
when print main Categories(Electronics & Clothes) as JSON response from the controller i want them to have also a list of their sub Categories.
from this
[ { id: 1, name: "Electronics", parentId: 0, active: 1, pic: null } ]
to this
[ { id: 1, name: "Electronics", parentId: 0, active: 1, pic: null, subCategories: [Mobiles, Laptops] } ]
How can i do this?