我想通過One to Many and Many to One Bidirectional relationship mapping但同時請求在插入7460行后保留資料時將一些資料存盤到資料庫中。我可以谷歌搜索但沒有找到合適的解決方案。
下面是我的代碼:
物體
public class Author {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer author_id;
private String name;
private String language;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "author")
private Set<Book> book;
// getter setter
}
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String title;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "author_id")
private Author author;
// getter setter
}
服務
@Override
public Author insertAuthor(Author author) {
Set<Book> bookList = new HashSet<>();
Book book = new Book();
book.setTitle(book.getTitle());
bookList.add(book);
book.setAuthor(author);
author.setBook(bookList);
return authorRepo.save(author);
}
控制器
@RestController
public class PojoController {
@Autowired
private PojoService pojoService;
@RequestMapping(value="/book", method = RequestMethod.POST)
public Author addBookCourse(@RequestBody Author author) {
return this.pojoService.insertAuthor(author);
}
}
要求
{
"language": "english",
"name": "Banjamin franklin",
"book": [{
"title": "Theory Of Everything"
},
{
"title": "A Brief Story Of Time"
}]
}
輸出
{
"author_id": 1,
"name": "Banjamin franklin",
"language": "english",
"book": [
{
"id": 1,
"title": null,
"author": {
"author_id": 1,
"name": "Banjamin franklin",
"language": "english",
"book": [
{
"id": 1,
"title": null,
"author": {
"author_id": 1,
"name": "Banjamin franklin",
"language": "english",
"book": [
{
"id": 1,
"title": null,
"author": {
"author_id": 1,
"name": "Banjamin franklin",
"language": "english",
"book": [
{
"id": 1,
"title": null,
more 7460 line
.......
.......
.......
{
"timestamp": "2021-11-30T10:25:03.957 00:00",
"status": 200,
"error": "OK",
"trace": "org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Infinite recursion (StackOverflowError); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: com.rest.RestApiPojo.Entity.Author[\"book\"]->org.hibernate.collection.internal.PersistentSet[0]->com.rest.RestApiPojo.Entity.Book[\"author\"]->com.rest.RestApiPojo.Entity.Author[\"book\"]->org.hibernate.collection.internal.PersistentSet[0]->com.rest.RestApiPojo.Entity.Book[\"author\"]
}
uj5u.com熱心網友回復:
您需要使用@JsonManagedReference,并@JsonBackReference允許杰克遜更好地處理之間的關系Author和Book:
public class Author {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer author_id;
private String name;
private String language;
@JsonManagedReference
@OneToMany(cascade = CascadeType.ALL, mappedBy = "author")
private Set<Book> book;
// getter setter
}
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String title;
@JsonBackReference
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "author_id")
private Author author;
// getter setter
}
您還有其他選擇(例如使用@JsonIdentityInfo)來處理這個無限遞回問題,但這是最常見的解決方案。您可以在以下在線資源https://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion查看所有其他可能的方法。
此外,在您的服務中,您正在創建一個全新的服務Book并將其標題設定為book.setTitle(book.getTitle());,這基本上什么都不做。事實上,你甚至不需要做你在那里做的大部分事情,因為Book實體已經在Author,你只需要按如下方式設定Author每個Book實體:
@Override
public Author insertAuthor(Author author) {
for (Book book : author.getBook()) {
book.setAuthor(author);
}
return authorRepo.save(author);
}
最后,考慮將book屬性更改為Author,books因為它包含多本書(之后您需要調整代碼)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/370632.html
