0

I'm new to Spring and wondering how to go about achieving something.

I want to include a Collection of mapped entities with doing a GET on an owning entity.

For example, say I have an entity that looks like:

@Entity
@Table(name="USERS")
public class ApplicationUserEntity {

    private Integer id;
    private String name;
    private String email;
    private String eid;
    private Collection<TaskEntity> tasksById;

and includes a getter for Tasks

@OneToMany(targetEntity = TaskEntity.class, mappedBy = "userByUserId")
public Collection<TaskEntity> getTasksById() {
    return tasksById;
}

When I GET the data for a user entity using the direct path (ie something like /users/1 because i changed the path in my repository) or with a search endpoint (ie /users/search/findById?id=1), it would return to me the properties from the User entity that are not mapped to other entities, but also include a link so I know how to fetch the tasks that this user has.

Aside from making a separate API call from the front end, how could I get the tasks to come back with the rest of the data? My reason for avoiding a projection is that I'm unable to do sorting with the ?sort query param if i also use a projection.

sb9
  • 266
  • 7
  • 22
  • *I'm unable to do sorting with the ?sort query param if i also use a projection.* Why? What do you mean by this? – Alan Hay Feb 17 '20 at 09:53
  • https://stackoverflow.com/questions/42652129/spring-data-rest-projection-sorting – sb9 Feb 17 '20 at 14:18
  • That refers to a very specific case. I don't see that it affects a standard mapping as you have here and don't see any reason you can't use a projection. – Alan Hay Feb 17 '20 at 14:29
  • I tried using a projection, but like i mentioned, the results don't come back sorted in the Collection I'm trying to sort. – sb9 Feb 17 '20 at 14:36
  • You can apply a fixed sort via `@OrderBy` on the mapping but you won't be able to sort dynamically. That is the way JPA works and nothing at all to do with Spring Data or projections. You cannot sort a collection in this way. Sort can only be applied to a resource collection e.g. `users?sort=xyz` – Alan Hay Feb 17 '20 at 14:53

0 Answers0