Hello, I ran into a problem: after converting an object into JSON, I do not see in it a list of dependent objects read from the database. I have two classes AutoService and Service . One service center can have several services, that is, the relation OneToMany. As a result, a list of services is stored in the AutoService class.

 @OneToMany(fetch = FetchType.EAGER) @JoinColumn(name = "autoServiceId") private List<Service> services; 

To convert to JSON I use MappingJackson2HttpMessageConverter .

Below is the code for these classes.

Class AutoService

 @Entity @Table(name = "AutoRate") public class AutoService { public AutoService() { } @Id @GeneratedValue(generator = "increment") @GenericGenerator(name = "increment", strategy = "increment") private long id; @Column(name = "serviceName", nullable = false) private String serviceName; @Column(name = "imageURL", nullable = false) private String imageURL; @Column(name = "mapCoordinate", nullable = false) private String mapCoordinate; @Column(name = "websiteURL", nullable = false) private String websiteURL; @Column(name = "phoneNumber", nullable = false) private String phoneNumber; @OneToMany(fetch = FetchType.EAGER) @JoinColumn(name = "autoServiceId") private List<Service> services; public long getId() { return id; } public String getServiceName() { return serviceName; } public String getImageURL() { return imageURL; } public String getMapCoordinate() { return mapCoordinate; } public String getWebsiteURL() { return websiteURL; } public String getPhoneNumber() { return phoneNumber; } } 

Class Service

 @Entity @Table(name = "Service") public class Service { public Service() { } @Id @GeneratedValue(generator = "increment") @GenericGenerator(name = "increment", strategy = "increment") @Column(name = "serviceId", unique = true, nullable = false) private long serviceId; @Column(name = "serviceName", nullable = false) private String serviceName; @Column(name = "category", nullable = false) private String category; @Column(name = "price", nullable = false) private int price; @Column(name = "autoServiceId", nullable = false) private long autoServiceId; public long getId() { return serviceId; } public String getCategory() { return category; } public int getPrice() { return price; } public String getServiceName() { return serviceName; } public long getAutoServiceId() { return autoServiceId; } } 

As a result, I get the following JSON:

 [ { "id": 1, "serviceName": "SpeedyName", "imageURL": "Url for speedy", "mapCoordinate": "123123 44121 ", "websiteURL": "speedy.com", "phoneNumber": "1231251" }, { "id": 2, "serviceName": "Другой сервис", "imageURL": "Урл для второго сервиса", "mapCoordinate": "123 12фывфы", "websiteURL": "другойсервис.ком", "phoneNumber": "12312333" } ] 

There is no list of dependent services for each service. Maybe I do not have enough annotations? I ask for help with this problem.

  • Where do you get this JSON? - Mikhail Vaysman
  • I use for testing - Andrew
  • so you also have a Controller ? why is it not visible here? - Mikhail Vaysman
  • one
    Getter something for services do, Jackson only through black reflection can now look in the field - etki

3 answers 3

You need to add @JsonManagedReference to the services field in the AutoService class

  • I did as you said, but for some reason JSON has not changed (( - Andrew
  • @JsonManagedReference @OneToMany (fetch = FetchType.EAGER) @JoinColumn (name = "autoServiceId") private List <Service> services; - Andrew

Try as in this example.

 public class User { public int id; public String name; @JsonBackReference public List<Item> userItems; } public class Item { public int id; public String itemName; @JsonManagedReference public User owner; } 

    The problem was in the absence of a getter on List<Services>