【发布时间】:2014-12-03 06:22:05
【问题描述】:
我很难弄清楚如何使用@RepositoryRestResource 接口在两个相当简单的实体之间创建多对多关系。
例如,我有这样一个简单的父子实体关系:
@Entity
public class ParentEntity {
@Id
@GeneratedValue
private Long id;
@ManyToMany
private List<ChildEntity> children;
}
@Entity
public class ChildEntity {
@Id
@GeneratedValue
private Long id;
@ManyToMany(mappedBy="children")
private List<ParentEntity> parents;
}
我的存储库正在使用原生 Spring @RepositoryRestResource HATEOS API:
@RepositoryRestResource(collectionResourceRel = "parents", path = "parents")
public interface ParentRepository extends PagingAndSortingRepository<ParentEntity, Long> {
}
@RepositoryRestResource(collectionResourceRel = "children", path = "children")
public interface ChildRepository extends PagingAndSortingRepository<ChildEntity, Long> {
}
我已经成功地使用 POST 创建了单独的 ParentEntity 和 ChildEntity,但我似乎无法弄清楚如何使用内置接口 PUT/PATCH 两者之间的关系。
似乎我应该能够使用 PUT 将 JSON 发送到 http://localhost:8080/api/parents/1/children 之类的东西,但到目前为止我还没有找到有效的结构。
【问题讨论】:
标签: java spring spring-data-rest