Hej mam mały problem i nie mam pojęcia gdzie tkwi błąd. Możliwe, że źle relacje określiłem albo coś...
Chcę zrobić tabelki:
**Autor**
id
imię
nazwisko
**Kategoria**
id
nazwa
**Książka**
id
tytuł
id_kategorii
id_autora
Chcę aby książkę można było dodać tylko do istniejącego autora i kategorii czyli np. jeśli w bazie danych nie ma autora o id=1 to nie doda mi filmiku z id_autora=1.
Stwierdziłem, że będzie to relacja Autor OneToMany Książką ManyToOne Kategoria.
Napisałem coś takiego:
@Table
@Entity
public class Author {
@Id
@Column
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@OneToMany(
mappedBy="author",
fetch = FetchType.LAZY,
cascade = CascadeType.ALL
)
private Set<Book> books;
// geters and setters
@Entity
@Table
public class Book {
@Id
@Column
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column
private String title;
@ManyToOne(
fetch = FetchType.LAZY,
cascade = {
CascadeType.DETACH,
CascadeType.MERGE,
CascadeType.PERSIST,
CascadeType.REFRESH
}
)
@JoinColumn(name="author_id", nullable=false)
private Author author;
@ManyToOne(
fetch = FetchType.LAZY,
cascade = {
CascadeType.DETACH,
CascadeType.MERGE,
CascadeType.PERSIST,
CascadeType.REFRESH
}
)
@JoinColumn(name="category_id", nullable=false)
private Category category;
// getters and setters
@Table
@Entity
public class Category {
@Id
@Column
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column
private String name;
@OneToMany(
mappedBy="category",
fetch = FetchType.LAZY,
cascade = CascadeType.ALL
)
private Set<Book> books;
// getters and setters
I jestem wstanie wrzucić do bazy autora oraz kategorie ale nie mogę wrzucić książki, bo dostaję taki oto błąd:
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.http.converter.HttpMessageNotReadableException",
"message": "JSON parse error: null; nested exception is com.fasterxml.jackson.databind.JsonMappingException: N/A\n at [Source: java.io.PushbackInputStream@191f3f96; line: 3, column: 14] (through reference chain: com.bowl.entity.Book["authorId"])",