I'm sure this should be simple, but I just can't seem to figure out how to do this.
We have an entity with a oneToMany relationship to another entity. Basic parent/child relationship (Using Spring/JPA).
The requirement is to have an api '/parents' which provides all of the parents and another '/parents_with_children' that provides all of the parents and their related child entities.
Now to get the parents with their children, we can specify "@OneToMany(fetch = FetchType.EAGER) and this is working fine.
But, how can I tell my RestController class to return only the top level components without cascading?
// Works fine with eager fetching
@GetMapping("/parents_with_children")
public List<Parent> list() {
return ParentService.getParents();
}
// How can I tell this one to NOT print out the children?
@GetMapping("/parents")
public List<Parent> list() {
return ParentService.getParents();
}
Should I manually print & return JSON data from the entities some how?