0

I'm implementing HATEOAS in my spring boot java application, and one of the things to do in HATEOAS is to not return the children objects in the json response. Instead, you have links that can fetch the children of that object.

An example is a company can have many employees. If you do a GET call for a specific company, the usual response would be a JSON object of company that contains a list of employees. I want the JSON to not have that employees collection. In theory, the only query hibernate should have to run is a select statement on the company table.

Any help is much appreciated.

Thanks in advance

Toast
  • 111
  • 1
  • 9

3 Answers3

0

Can use fetch Lazy in order to not show childs, here is an example:

@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
private List<Team> teams;
Jonathan JOhx
  • 5,784
  • 2
  • 17
  • 33
0

View the code for type association you are using,

for example in ManyToOne.class you will notice line FetchType fetch() default EAGER. This means associated entity will be eagerly fetched. similarly, for OneToMany and for ManyToMany.class the default strategy is Lazy. Depending on the type of association you are using there will be always some default strategy but you can change this strategy

If you don't want this behavior then you need to change @ManyToOne(fetch=FetchType.LAZY)

Satya
  • 328
  • 1
  • 7
  • 19
0

Realized Jackson was the one making the extra call when it was serializing the objects before sending it back. Was able to use @JsonProperty(access = JsonProperty.Access.WRITE_ONLY) on the employee property on the company object. This way I can save it as a nested object if I want to, but on retrieval it will not fetch the nested objects (thus not making the extra sql call).

Toast
  • 111
  • 1
  • 9