簡體   English   中英

在 Spring Data Rest 中放置多對多關聯

[英]PUT on ManyToMany association in Spring Data Rest

我想弄清楚如何直接在我擁有的多對多關聯上放置。

我的實體示例(名稱因額外混淆而更改):

第一個實體:

@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"
}

但什么也沒有發生。 當您在多對多中間使用實體時,是否可以以這種方式放置關聯? 或者我應該直接將關聯發布到 /third 嗎?

您首先必須創建一個 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM