gpt4 book ai didi

spring-boot - 如何在 Spring Data Rest 中的两个实体之间发布和放置关系 @OneToMany/@ManyToOne?

转载 作者:行者123 更新时间:2023-12-05 07:49:18 24 4
gpt4 key购买 nike

我有一个用于 Spring Data Rest 实现的简单 Spring Boot 应用程序。

这是主类:

@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

我有两个简单的实体:书籍和作者。彼此之间的关系是 1 Author -> N Books

这是作者类:

@Entity
@Table
public class Author {

@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(columnDefinition = "BINARY(16)", length = 16)
private UUID id;

@Column
private String name;

@OneToMany(fetch = FetchType.LAZY, targetEntity = Book.class, mappedBy = "author")
private List<Book> books;

// getters and setters

}

这是 Book.class:

@Entity
@Table
public class Book {

@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(columnDefinition = "BINARY(16)", length = 16)
private UUID id;

@Column
private String title;

@Column
private String language;

@ManyToOne(fetch = FetchType.EAGER, targetEntity = Author.class)
private Author author;

// getters and setters

}

这是“AuthorRepository”:

@RestResource(path = "authors", rel = "authors")
public interface AuthorRepository extends JpaRepository<Author, UUID> {
}

这是“BookRepository”:

@RestResource(path = "books", rel = "books")
public interface BookRepository extends JpaRepository<Book, UUID> {
}

应用程序运行完美,在 url http://localhost:8080/我有这个响应页面:

{
"_links" : {
"authors" : {
"href" : "http://localhost:8080/authors{?page,size,sort}",
"templated" : true
},
"books" : {
"href" : "http://localhost:8080/books{?page,size,sort}",
"templated" : true
},
"profile" : {
"href" : "http://localhost:8080/profile"
}
}
}

网址 http://localhost:8080/authors返回此页面:

{
"_embedded" : {
"authors" : [ ]
},
"_links" : {
"self" : {
"href" : "http://localhost:8080/authors"
},
"profile" : {
"href" : "http://localhost:8080/profile/authors"
}
},
"page" : {
"size" : 20,
"totalElements" : 0,
"totalPages" : 0,
"number" : 0
}
}

和 url http://localhost:8080/books返回此页面:

{
"_embedded" : {
"books" : [ ]
},
"_links" : {
"self" : {
"href" : "http://localhost:8080/books"
},
"profile" : {
"href" : "http://localhost:8080/profile/books"
}
},
"page" : {
"size" : 20,
"totalElements" : 0,
"totalPages" : 0,
"number" : 0
}
}

我正在尝试从 Book 类开始进行一些 HTTP POST。

HTTP POST
url: http://localhost:8080/books
header: Content-Type:application/json
payload: { "title": "Book Title" }

Status: 201: Created
Location: http://localhost:8080/books/61311c9b-b33a-463c-9e6e-8e5efc0a7ad1

其实url http://localhost:8080/books/61311c9b-b33a-463c-9e6e-8e5efc0a7ad1返回此页面:

{
"title" : "Book Title",
"language" : null,
"_links" : {
"self" : {
"href" : "http://localhost:8080/books/61311c9b-b33a-463c-9e6e-8e5efc0a7ad1"
},
"book" : {
"href" : "http://localhost:8080/books/61311c9b-b33a-463c-9e6e-8e5efc0a7ad1"
},
"author" : {
"href" : "http://localhost:8080/books/61311c9b-b33a-463c-9e6e-8e5efc0a7ad1/author"
}
}
}

我为作者做了同样的事情。

HTTP POST
url: http://localhost:8080/authors
header: Content-Type:application/json
payload: { "name": "Author Name" }

Status: 201: Created
Location: http://localhost:8080/authors/634d3bd8-abe6-472b-97cd-04a455bdfb11

这是该 url 的响应页面:

{
"name" : "Author Name",
"_links" : {
"self" : {
"href" : "http://localhost:8080/authors/634d3bd8-abe6-472b-97cd-04a455bdfb11"
},
"author" : {
"href" : "http://localhost:8080/authors/634d3bd8-abe6-472b-97cd-04a455bdfb11"
},
"books" : {
"href" : "http://localhost:8080/authors/634d3bd8-abe6-472b-97cd-04a455bdfb11/books"
}
}
}

注意作者和书之间的关系还不存在,所以我尝试发送一个 PUT 请求。

HTTP PUT
url: http://localhost:8080/authors/634d3bd8-abe6-472b-97cd-04a455bdfb11
header: Content-Type:application/json
payload: { "books": ["http://localhost:8080/books/61311c9b-b33a-463c-9e6e-8e5efc0a7ad1"] }

Status: 204: No Content
Location: http://localhost:8080/authors/634d3bd8-abe6-472b-97cd-04a455bdfb11

我有一个 HTTP 204(无内容)作为响应代码。如果我转到 url http://localhost:8080/authors/634d3bd8-abe6-472b-97cd-04a455bdfb11/books我期待这本书的结果,但我得到的却是这个结果:

{
"_embedded" : {
"books" : [ ]
},
"_links" : {
"self" : {
"href" : "http://localhost:8080/authors/634d3bd8-abe6-472b-97cd-04a455bdfb11/books"
}
}
}

“书籍”属性仍然是空的。为什么?

此外,此 URL 返回一个空页面:http://localhost:8080/books/61311c9b-b33a-463c-9e6e-8e5efc0a7ad1/author

关系没有按我预期的方式处理。

如果我在插入书籍之前从作者开始执行相同的过程,则关系存在。

如何保存从 Book 实体开始的两个实体之间的关系?

谢谢

最佳答案

要使这种情况发生相反的情况,您的@OneToMany(fetch = FetchType.LAZY, targetEntity = Book.class, mappedBy = "author") 应该在其中包含级联选项以实际保留书籍实体中的更改。

试试这个:

@OneToMany(fetch = FetchType.LAZY, targetEntity = Book.class, mappedBy = "author", cascade = CascadeType.ALL)

关于spring-boot - 如何在 Spring Data Rest 中的两个实体之间发布和放置关系 @OneToMany/@ManyToOne?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37487642/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com