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.

  • How are the data selected from the database? Where did you get the confidence that the subcategory should be filled? - Roman Danilov
  • What do you mean by "how data is selected from the database"? Do you want to see the table and links in postgresql? - Vitaly Belousov
  • And where, sobsna, in the classroom is the subcategory? I now do not see at range :) - Anton Mukhin
  • @ VitalyBelousov I want to see what the data is there after they are pulled out of the base - Roman Danilov
  • corrected, not subcategory, but category - Vitaly Belousov

1 answer 1

Understood. The problem was that I incorrectly created a table in the database and set up links. Entrust this business to the program itself! Create the database, but do not create the tables, simply cover the server and execute the query by following the link in the controller.