當請求PUT它在子物體中保存記錄但不更新時。我做錯了什么我無法理解。在我的資料庫中,三本書的實??際 id 記錄是,5,6,7但是當通過郵遞員更新資料時,它會用 id 保存新書20,21,22。
物體
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 Course updateCourseBook(Course course) {
return this.courseRepo.save(course);
}
控制器
@RequestMapping(value = "/coursebook", method = RequestMethod.PUT)
public ResponseEntity<Course> updateCourseBook(@RequestBody Course course) {
for(Book book: course.getBooks())
{
book.setCourse(course);
}
Course updateCourse = this.pojoService.updateCourseBook(course);
if(updateCourse != null)
return ResponseEntity.ok(updateCourse);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
要求

輸出

uj5u.com熱心網友回復:
我的問題解決了。插入資料時我無法宣告書籍 ID。
{
"id": 3,
"name": "Maths",
"description": "Books about mathematics",
"books": [
{
"book_id": 5, // forget this
"title": "Infinite Powers"
},
{
"book_id": 6, // forget this
"title": "Mathematics For Machine Learning"
},
{
"book_id": 7, // forget this
"title": "Discrete Mathematics2"
}
]
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/375401.html
