最近中文字幕高清中文字幕无,亚洲欧美高清一区二区三区,一本色道无码道dvd在线观看 ,一个人看的www免费高清中文字幕

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在Spring Data REST中發(fā)布@OneToMany子資源關(guān)聯(lián)

在Spring Data REST中發(fā)布@OneToMany子資源關(guān)聯(lián)

守候你守候我 2019-09-19 16:29:57
目前我有一個使用Spring Data REST的Spring Boot應(yīng)用程序。我有一個與另一個域?qū)嶓wPost有@OneToMany關(guān)系的域?qū)嶓wComment。這些類的結(jié)構(gòu)如下:Post.java:@Entitypublic class Post {    @Id    @GeneratedValue    private long id;    private String author;    private String content;    private String title;    @OneToMany    private List<Comment> comments;    // Standard getters and setters...}Comment.java:@Entitypublic class Comment {    @Id    @GeneratedValue    private long id;    private String author;    private String content;    @ManyToOne    private Post post;    // Standard getters and setters...}他們的Spring Data REST JPA存儲庫是以下基本實現(xiàn)CrudRepository:PostRepository.java:public interface PostRepository extends CrudRepository<Post, Long> { }CommentRepository.java:public interface CommentRepository extends CrudRepository<Comment, Long> { }應(yīng)用程序入口點是標(biāo)準(zhǔn)的簡單Spring Boot應(yīng)用程序。一切都是配置庫存。Application.java@Configuration@EnableJpaRepositories@Import(RepositoryRestMvcConfiguration.class)@EnableAutoConfigurationpublic class Application {    public static void main(final String[] args) {        SpringApplication.run(Application.class, args);    }}一切似乎都正常。當(dāng)我運(yùn)行應(yīng)用程序時,一切似乎都正常工作。我可以POST一個新的Post對象http://localhost:8080/posts:身體:  {"author":"testAuthor", "title":"test", "content":"hello world"}結(jié)果http://localhost:8080/posts/1:{    "author": "testAuthor",    "content": "hello world",    "title": "test",    "_links": {        "self": {            "href": "http://localhost:8080/posts/1"        },        "comments": {            "href": "http://localhost:8080/posts/1/comments"        }    }}但是,當(dāng)我執(zhí)行GET時,http://localhost:8080/posts/1/comments我得到一個空對象{}返回,如果我嘗試將注釋POST到同一個URI,我得到一個HTTP 405方法不允許。創(chuàng)建Comment資源并將其與此關(guān)聯(lián)的正確方法是什么Post?http://localhost:8080/comments如果可能的話,我想避免直接POST 。
查看完整描述

3 回答

?
狐的傳說

TA貢獻(xiàn)1804條經(jīng)驗 獲得超3個贊

您必須先發(fā)布評論,在發(fā)布評論時,您可以創(chuàng)建一個關(guān)聯(lián)發(fā)布實體。


它應(yīng)該如下所示:


http://{server:port}/comment METHOD:POST


{"author":"abc","content":"PQROHSFHFSHOFSHOSF", "post":"http://{server:port}/post/1"}

它會完美地運(yùn)作。


查看完整回答
反對 回復(fù) 2019-09-19
?
富國滬深

TA貢獻(xiàn)1790條經(jīng)驗 獲得超9個贊

假設(shè)您已經(jīng)發(fā)現(xiàn)了post URI并因此發(fā)現(xiàn)了關(guān)聯(lián)資源的URI(被認(rèn)為是$association_uri在下面),它通常采取以下步驟:


發(fā)現(xiàn)管理評論的館藏資源:


curl -X GET http://localhost:8080


200 OK

{ _links : {

    comments : { href : "…" },

    posts :  { href : "…" }

  }

}

按照comments鏈接和POST您的數(shù)據(jù)到資源:


curl -X POST -H "Content-Type: application/json" $url 

{ … // your payload // … }


201 Created

Location: $comment_url

通過向PUT關(guān)聯(lián)URI 發(fā)出a 來將評論分配給帖子。


curl -X PUT -H "Content-Type: text/uri-list" $association_url

$comment_url


204 No Content

請注意,在最后一步中,根據(jù)規(guī)范text/uri-list,您可以提交多個URI,用于標(biāo)識由換行符分隔的注釋,以便一次分配多個注釋。


關(guān)于一般設(shè)計決策的一些注釋。阿交/評論例如通常是聚集體,這意味著我會避免從背面參考一個很好的例子Comment的Post,并且還避免了CommentRepository完全。如果注釋沒有自己的生命周期(它們通常不是在組合風(fēng)格的關(guān)系中),你寧可直接內(nèi)聯(lián)呈現(xiàn)注釋,而是添加和刪除注釋的整個過程可以通過使用來處理JSON補(bǔ)丁。Spring Data REST 在即將發(fā)布的2.2版本的最新候選版本中增加了對該功能的支持。


查看完整回答
反對 回復(fù) 2019-09-19
?
白板的微信

TA貢獻(xiàn)1883條經(jīng)驗 獲得超3個贊

映射關(guān)聯(lián)和組合有兩種類型。在關(guān)聯(lián)的情況下,我們使用連接表概念


員工 - 1到n->部門


因此,如果是Association Employee,Department,Employee_Department,將創(chuàng)建3個表


您只需要在代碼中創(chuàng)建EmployeeRepository。除此之外,映射應(yīng)該是這樣的:


class EmployeeEntity{


@OnetoMany(CascadeType.ALL)

   private List<Department> depts {


   }


}

Depatment Entity不會包含forign key的任何mappping ...所以現(xiàn)在當(dāng)你嘗試在單個json請求中添加Employee with Department的POST請求時,它將被添加....


查看完整回答
反對 回復(fù) 2019-09-19
  • 3 回答
  • 0 關(guān)注
  • 1128 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號