Spring Data Rest - sort by nested property

I have a database service using Spring Boot 1.5.1 and Spring Data Rest. I save my objects in a MySQL database and access their REST using Spring PagingAndSortingRepository. I found this , which says that sorting by nested parameters is supported, but I cannot find a way to sort by nested fields.

I have the following classes:

@Entity(name = "Person") @Table(name = "PERSON") public class Person { @ManyToOne protected Address address; @ManyToOne(targetEntity = Name.class, cascade = { CascadeType.ALL }) @JoinColumn(name = "NAME_PERSON_ID") protected Name name; @Id protected Long id; // Setter, getters, etc. } @Entity(name = "Name") @Table(name = "NAME") public class Name{ protected String firstName; protected String lastName; @Id protected Long id; // Setter, getters, etc. } 

For example, when using the method:

 Page<Person> findByAddress_Id(@Param("id") String id, Pageable pageable); 

And by calling the URI http: // localhost: 8080 / people / search / findByAddress_Id? Id = 1 & sort = name_lastName, desc , the sort parameter is completely ignored by Spring.

The parameters sort = name.lastName and sort = nameLastName did not work either.

Am I incorrectly generating a vacation request or can’t see any configuration?

Thanks!

+6
source share
2 answers

I debugged this, and it looks like the problem Alan was talking about.

I found a workaround that could help:

Create your own controller, enter your repo and optionally factory projection (if you need forecasts). Deploy a get method to delegate a call to your repository

  @RestController @RequestMapping("/people") public class PeopleController { @Autowired PersonRepository repository; //@Autowired //PagedResourcesAssembler<MyDTO> resourceAssembler; @GetMapping("/by-address/{addressId}") public Page<Person> getByAddress(@PathVariable("addressId") Long addressId, Pageable page) { // spring doesn't spoil your sort here ... Page<Person> page = repository.findByAddress_Id(addressId, page) // optionally, apply projection // to return DTO/specifically loaded Entity objects ... // return type would be then PagedResources<Resource<MyDTO>> // return resourceAssembler.toResource(page.map(...)) return page; } } 

This works for me with 2.6.8.RELEASE; the problem seems to be in all versions.

+1
source

The workaround I found is to create an additional read-only property for sorting purposes only. Based on the above example:

 @Entity(name = "Person") @Table(name = "PERSON") public class Person { // read only, for sorting purposes only // @JsonIgnore // we can hide it from the clients, if needed @RestResource(exported=false) // read only so we can map 2 fields to the same database column @ManyToOne @JoinColumn(name = "address_id", insertable = false, updatable = false) private Address address; // We still want the linkable association created to work as before so we manually override the relation and path @RestResource(exported=true, rel="address", path="address") @ManyToOne private Address addressLink; ... } 

The disadvantage of the proposed solution is that now we must explicitly duplicate all the properties for which we want to support nested sorting.

LATER EDIT: Another drawback is that we cannot hide the inline property from clients. In my original answer, I suggested adding @JsonIgnore, but this seems to break the sorting.

+1
source

Source: https://habr.com/ru/post/1015013/


All Articles