Good day to all!
I use: Java + Spring framework + Postgresql . I have 3 tables in the database, I want to display all 3 tables in one big JSON.
Now my JSON is displayed like this:
[{"id": 1, "name": "menu1", "category": []}, {"id": 2, "name": "menu2", "category": []}]
For some reason, the category array is empty, although it must have entries.
Code:
Entities:
Menu.java:
@Entity @Table(name = "menu") public class Menu { @Id @GeneratedValue(generator = "increment") @GenericGenerator(name= "increment", strategy= "increment") @Column(name = "id", length = 6, nullable = false) private long id; @Column(name = "name") private String name; @OneToMany(mappedBy = "menu", fetch = FetchType.EAGER) private List<Categories> category; //GETTERS AND SETTERS Categories.java:
@Entity @Table(name = "categories") public class Categories { @OneToMany(mappedBy = "category", fetch = FetchType.EAGER) private List<Dish> dish; @ManyToOne @JoinColumn(name = "menu") private Menu menu; @Id @GeneratedValue(generator = "increment") @GenericGenerator(name = "increment", strategy = "increment") private long id; @Column(name = "title_ru", nullable = false, length = 50) private String titleRu; @Column(name = "title_eng", nullable = false, length = 50) private String titleEng; @Column(name = "url", nullable = false, length = 150) private String url; @Column(name = "img_url", nullable = false, length = 150) private String imageUrl; @Column(name = "weight", nullable = false, length = 5) private int weight; @Column(name = "status", nullable = false, length = 2) private int status; public List<Dish> getDishes() { return dish; } @JsonIgnore public Menu getMenu() { return menu; } Dish.java
@ManyToOne @JoinColumn(name = "category") private Categories category; @Id @GeneratedValue(generator = "increment") @GenericGenerator(name = "increment", strategy = "increment") @Column(name = "id", length = 6, nullable = false) private long id; @Column(name = "title_ru", nullable = false, length = 50) private String titleRu; @Column(name = "title_eng", nullable = false, length = 50) private String titleEng; @Column(name = "description_ru", nullable = false, length = 500) private String descriptionRu; @Column(name = "description_eng", nullable = false, length = 500) private String descriptionEng; @Column(name = "url", nullable = false, length = 150) private String url; @Column(name = "img_url", nullable = false, length = 150) private String imgUrl; @Column(name = "weight", nullable = false, length = 5) private int weight; @Column(name = "status", nullable = false, length = 2) private int status; @JsonIgnore public Categories getDishCategory() { return category; } Controller:
@RequestMapping(value = "/menu", method = RequestMethod.GET) @ResponseBody public List<Menu> getAllMenu() { return service.getAllMenu(); } Service:
public List<Menu> getAllMenu() { return menuRepository.findAll(); } What is the problem? How to solve it? Please explain in simple terms, for a beginner.