PUT 在 Spring 数据 Rest 中的 ManyToMany 关联

PUT on ManyToMany association in Spring Data Rest

我正在尝试弄清楚如何直接在我拥有的 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 中间使用实体时,是否可以通过这种方式 PUT 关联?或者我应该直接 POST 关联到 /third?

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

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