Spring Data Rest - sort by nested property

后端 未结 3 1471
[愿得一人]
[愿得一人] 2021-01-08 00:56

I have a database service using Spring Boot 1.5.1 and Spring Data Rest. I am storing my entities in a MySQL database, and accessing them over REST using Spring\'s PagingAndS

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-08 01:51

    I debugged through that and it looks like the issue that Alan mentioned.

    I found workaround that could help:

    Create own controller, inject your repo and optionally projection factory (if you need projections). Implement get method to delegate call to your repository

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

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

提交回复
热议问题