【发布时间】:2014-05-05 18:25:13
【问题描述】:
我有以下问题
我有一个 spring data rest 的基本配置(没什么花哨的,没什么自定义的)。
使用 spring-data-rest-webmvc 2.0.0 RELEASE 和 spring-data-jpa 1.5.0 RELEASE
A类
@Entity
public class A {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id
private String name;
@ManyToMany
private List<B> b;
// getters setters
}
B类
@Entity
public class B {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id
private String nameb;
@ManyToMany(mappedBy = "b")
private List<A> a;
// getters setters
}
存储库 A
@Repository
@RestResource(rel = "a", path = "a")
public interface ARepository extends PagingAndSortingRepository<A, Integer> {
}
存储库 B
@Repository
@RestResource(rel = "b", path = "b")
public interface BRepository extends PagingAndSortingRepository<B, Integer> {
}
当我保存实体时工作正常,但我不知道如何保存关系
例如使用 http 将“A”保存在“B”中
这是我从这个答案https://stackoverflow.com/a/13031580/651948尝试的最后一件事
POST http://localhost:8080/api/a
{
"name": "Name of A",
"b": {
"rel": "b",
"href": "http://localhost:8080/api/b/1"
}
}
我得到一个 201 http 代码,但没有保存实体。
有人试过了吗?
【问题讨论】:
-
你解决过这个问题吗?我也有类似的问题。
标签: json spring rest hateoas hal-json