[英]How to properly save ManyToOne Entities?
当我尝试用帖子保存我的实体(“章节”)时,我得到了这个例外:
org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of "domain.Book" (although at least one Creator exists): no int/Int-argument constructor/factory method to deserialize from Number value (81)
当我在@ManyToOne
字段上使用@JsonIgnore
,其他数据保存得很好。 我认为问题在于我保存“章节”的方式,但我不知道如何将其更改为正确的方式。
POST 的 JSON:
{
"name": "testBookName",
"chapters": [
{
"name": "testChapterName",
"number": 1
}
]
}
结果:
{
"id": 99,
"book": null,
"number": 1,
"name": "testChapterName",
"pages": null
}
第二个 JSON 变体:
{
"name": "testBookName",
"id": 99,
"chapters": [
{
"book": 99,
"name": "testChapterName",
"number": 1
}
]
}
结果:
JSON parse error: Cannot construct instance of "domain.Book" (although at least one Creator exists): no int/Int-argument constructor/factory method to deserialize from Number value (99); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of "domain.Book" (although at least one Creator exists): no int/Int-argument constructor/factory method to deserialize from Number value (99)\\n at [Source: (PushbackInputStream); line: 6, column: 16] (through reference chain: domain.Book[\\"chapters\\"]->java.util.ArrayList[0]->domain.Chapter[\\"book\\"])"
图书实体:
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private String author;
private String artist;
private String year;
private int views;
private double rating;
@Lob
private String image;
@Enumerated(EnumType.STRING)
private Status status;
@ElementCollection(targetClass = Genre.class, fetch = FetchType.EAGER)
@CollectionTable(name = "genres", joinColumns = @JoinColumn(name = "book_id"))
@Enumerated(EnumType.STRING)
private List<Genre> genres;
@OneToMany(mappedBy = "book", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Chapter> chapters;
}
章节实体:
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Chapter {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne
@JoinColumn(name = "book_id")
private Book book;
private int number;
private String name;
@OneToMany(targetEntity = Page.class, mappedBy = "chapter", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Page> pages;
}
页面实体:
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Page {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne
@JoinColumn(name = "chapter_id")
private Chapter chapter;
private int number;
@Lob
private String image;
}
POST 的控制器方法:
@PostMapping()
public Chapter createChapter(@RequestBody Book book) {
List<Chapter> chapters = book.getChapters();
return chapterRepository.save(chapters.get(chapters.size() - 1));
}
}
此问题与您的第二个 JSON 变体有关
将您的 JSON 更改为此它将起作用。
{
"name": "testBookName",
"id": 99,
"chapters": [
{
"book": {
"id": 99
},
"name": "testChapterName",
"number": 1
}
]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.