2017-05-09 48 views
0

我在玩Spring Data examples。我已經定義了一個實體Parent,它有一組關聯的子實體。用Spring Data在一個請求中發佈子實體

@Entity 
@Table 
@Data 
public class Parent { 

    @Id 
    @GeneratedValue 
    private Integer id; 

    @NotNull 
    private String name; 

    @Fetch(FetchMode.SUBSELECT) 
    @OneToMany(fetch = EAGER, cascade = {ALL}, orphanRemoval = true) 
    private Set<Child> childs; 

} 

@Entity 
@Table 
@Data 
public class Item { 

    @Id 
    @GeneratedValue 
    private Integer id; 

    @NotNull 
    private String name; 

} 

和相應的存儲庫。我的問題是,當使用curl發佈帶有Child的新Parent時,我遇到了也描述爲here的錯誤Could not read JSON document: Failed to convert from type [java.net.URI] to type。該問題的答案指出,子實體需要在之前發佈,然後使用返回的URL。這與Oliver Gierke在this answer中描述的過程相同。

有什麼辦法配置Spring數據來反序列化完整的子實體嗎?

回答

1

如果您不導出Item的存儲庫(通過使用@RepositoryRestResource(exported = false)註釋存儲庫),您將始終獲得子級序列化,並且您還可以在父級上爲其提供POST

但是也不會有Item的頂級REST API端點,也不會有關於父級的關聯資源。

您已經有cascade=ALL關係 - 所以這種方法應該工作。