2017-02-16 108 views
6

我有一個使用Spring Boot 1.5.1和Spring Data Rest的數據庫服務。我將我的實體存儲在MySQL數據庫中,並使用Spring的PagingAndSortingRepository通過REST訪問它們。我發現this其中規定支持通過嵌套參數進行排序,但我找不到通過嵌套字段進行排序的方法。Spring Data Rest - 按嵌套屬性排序

我有這些類:

@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. 
} 

例如,在使用該方法時:

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

並調用URI http://localhost:8080/people/search/findByAddress_Id?id=1&sort=name_lastName,desc,排序參數是完全由Spring忽略。

參數 sort = name.lastName and sort = nameLastName也沒有工作。

我是否在形成Rest請求錯誤或缺少一些配置?

謝謝!

+2

name.lastName將是要使用的屬性。在Hopper版本中,通過嵌套屬性進行排序對我來說效果不錯,但我在Ingalls版本的RC版本中遇到了以下錯誤。據報道,這是固定的,但我沒有嘗試過。 https://jira.spring.io/browse/DATAREST-976?jql=text%20~%20%22sort%20nested%22%20ORDER%20BY%20created%20DESC –

+0

@AlanHay你是'人',與我在降級到Hopper後發佈了' 1.10.10.RELEASE 2.5.10.RELEASE < spring.data.rest.webmvc.version>' –

+0

@AlanHay順便說一句,我試過['v3.0.0.M3'](http://docs.spring.io/spring-data/rest/docs/3.0.0。 M3/changelog.txt)報告已修復但未與我合作。 –

回答

1

我通過調試,它看起來像艾倫提到的問題。

我找到解決方法,可以幫助:

創建自己的控制器,注入你的回購和可選投影工廠(如果你需要預測)。實現get方法委託調用您的存儲庫

@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; 
    } 

} 

這適用於我與2.6.8.RELEASE;這個問題似乎在所有版本中。

+0

此解決方案工作。謝謝。 –

0

我發現的解決方法是爲排序目的創建一個額外的只讀屬性。建立在上面的例子:

@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; 

    ... 
} 

所提出的解決方法的缺點是,我們現在必須明確地複製所有爲此我們要支持嵌套排序的屬性。

後期編輯:另一個缺點是,我們無法隱藏客戶端的嵌入式屬性。在我最初的回答中,我建議我們可以添加@JsonIgnore,但顯然這是違反了這一規定的。