在Spring Data Rest中对ManyToMany关联进行PUT



我正试图弄清楚如何直接对我拥有的ManyToMany关联进行PUT。

我的实体示例(名称因额外混淆而更改(:

第一个实体:

@Entity
public class First {
@Id
private Long id;
private String name;
@OneToMany(mappedBy = "first")
private Set<Third> thirds = new HashSet<>();
}

第二个实体:

@Entity
public class Second {
@Id
private Long id;
private String name;
@OneToMany(mappedBy = "second")
private Set<Third> thirds = new HashSet<>();
}

第三实体:

@Entity
public class Third {
@Id
private Long id;
private String type;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "first_id")
private First first;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "second_id")
private Second second;
}

现在我想做的是这样的事情:

PUT /first/1/thirds
{
"second": "/second/1",
"type": "TEST"
}

但什么也没发生。当您在ManyToMany中间使用实体时,有可能以这种方式放置关联吗?或者我应该直接将关联发布到第三方吗?

您首先必须创建一个first实例,如下所示:

curl -i -X POST -d "{"name":"first"}"
-H "Content-Type:application/json" http://localhost:8080/firsts

然后你必须创建一个第三个实例:

curl -i -X POST -H "Content-Type:application/json"
-d '{"type":"third"}' http://localhost:8080/thirds

最后,您可以使用PUT创建一个关联:

curl -i -X PUT -H "Content-Type:text/uri-list"
-d "http://localhost:8080/firsts/1" http://localhost:8080/thirds/1/first

最新更新