java – Json does not contain list of dependent objects

Question:

Hello, I ran into a problem: after converting an object to JSON, I do not see in it a list of dependent objects read from the database. I have two classes AutoService and Service . One car service can have multiple services, i.e. OneToMany relationship. As a result, the list of services is stored in the AutoService class in the field

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

To convert to JSON I use MappingJackson2HttpMessageConverter .

Below will be presented the code of the named classes.

AutoService class

@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 am missing some kind of annotation? I ask for help with this problem.

Answer:

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

Scroll to Top