我无法弄清楚如何使用@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;
}
Run Code Online (Sandbox Code Playgroud)
我的存储库正在使用vanilla 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> {
}
Run Code Online (Sandbox Code Playgroud)
我已成功使用POST来创建单独的ParentEntity和ChildEntity,但我似乎无法弄清楚如何使用内置接口PUT/PATCH两者之间的关系.
看起来我应该能够使用PUT将JSON发送到类似的东西http://localhost:8080/api/parents/1/children,但到目前为止,我找不到一个有效的结构.