// ------------------- ТЕСТ -------------------
@Test
void testUpdate() {
Comment updatedComment = new Comment(...);
commentRepository.save(updatedComment);
Comment actual = commentRepository.findOne(updatedComment.getCommentId());
assertThat(actual).isEqualToIgnoringGivenFields(updatedComment, "book"); // игнорируем book, чтобы программа не вошла в бесконечный цикл book-comment-book-comment-...
// до сюда всё хорошо, теперь отдельно проверяем book, игнорируя comments
assertThat(actual.getBook()).isEqualToIgnoringGivenFields(updatedComment.getBook(), "comments"); // здесь ошибка Cannot locate field $$_hibernate_interceptor on class model.Book
}
// ------------------- КНИЖКА -------------------
@Entity
@Table(name = "book")
@Data
@NoArgsConstructor
public class Book {
@Id
@GeneratedValue(strategy = IDENTITY)
private Long bookId;
private String title;
private Integer year;
@ManyToOne
@JoinColumn(name = "author_id", nullable = false)
private Author author;
@ManyToOne
@JoinColumn(name = "genre_id", nullable = false)
private Genre genre;
@OneToMany(
mappedBy = "book",
cascade = CascadeType.ALL,
orphanRemoval = true
)
private List<Comment> comments = new ArrayList<>();
// конструктор...
}
// ------------------- КОММЕНТ -------------------
@Entity
@Table(name = "comment")
@Data
@NoArgsConstructor
public class Comment {
@Id
@GeneratedValue(strategy = IDENTITY)
private Long commentId;
private String commentator;
private String text;
private LocalDateTime date;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "book_id", nullable = false)
private Book book;
// конструктор...
}
Comments
0 B
|👍
/👎