どのように適切にManyToOneエンティティ保存しますか?

skrskr:

私はポストに私のエンティティ(「章」)を保存しようとすると、私はこの例外を取得します:

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)

私が使用している場合@JsonIgnoreには@ManyToOne、フィールドの他のデータは、罰金が保存されます。私はこの問題は、私が「章」保存方法であると思いますが、私は正しい道にそれを変更する方法は考えています。

POSTのためのJSON:

{
  "name": "testBookName",
  "chapters": [
    {
      "name": "testChapterName",
      "number": 1
    }
  ]
}

結果:

{
  "id": 99,
  "book": null,
  "number": 1,
  "name": "testChapterName",
  "pages": null
}

第2回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));
    }
}
Gnana jeyam95:

この問題はあなたにある第二JSONバリアント

それが動作しますこれにあなたのJSONを変更します。

{
 "name": "testBookName",
  "id": 99, 
  "chapters": [ 
    {
       "book": {
           "id": 99
        },
        "name": "testChapterName", 
        "number": 1
     } 
    ] 
}

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=32634&siteId=1