【问题标题】:Posting child entities in a single request with Spring Data使用 Spring Data 在单个请求中发布子实体
【发布时间】:2017-05-09 14:39:27
【问题描述】:

我在玩Spring Data examples。我已经定义了一个实体 Parent,它具有一组关联的 Child 实体。

@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 时,我遇到了错误 Could not read JSON document: Failed to convert from type [java.net.URI] to type 也描述了 here。该问题的答案表明,子实体需要先发布,然后使用返回的 URL。这与 Oliver Gierke 在 this answer 中描述的过程相同。

有没有办法配置 Spring Data 以反序列化完整的子实体?

【问题讨论】:

    标签: java spring-data spring-data-rest


    【解决方案1】:

    如果您不导出Item 的存储库(通过使用@RepositoryRestResource(exported = false) 注释存储库),您将始终序列化子级,并且您可以在父级上为其提供POST

    但也将不再有Item 的顶级 REST API 端点,父节点上也没有关联资源。

    您已经在关系中拥有cascade=ALL - 所以这种方法应该有效。

    【讨论】:

      猜你喜欢
      • 2014-08-25
      • 1970-01-01
      • 1970-01-01
      • 2023-01-10
      • 2017-11-13
      • 2019-03-05
      • 2020-11-22
      • 1970-01-01
      相关资源
      最近更新 更多