簡體   English   中英

如何正確保存多對一實體?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM