Advertisement
ILyaCyclone

Untitled

Apr 16th, 2019
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.96 KB | None | 0 0
  1. // ------------------- ТЕСТ -------------------
  2. @Test
  3. void testUpdate() {
  4.         Comment updatedComment = new Comment(...);
  5.         commentRepository.save(updatedComment);
  6.  
  7.         Comment actual = commentRepository.findOne(updatedComment.getCommentId());
  8.  
  9.         assertThat(actual).isEqualToIgnoringGivenFields(updatedComment, "book"); // игнорируем book, чтобы программа не вошла в бесконечный цикл book-comment-book-comment-...
  10.  
  11.        // до сюда всё хорошо, теперь отдельно проверяем book, игнорируя comments
  12.         assertThat(actual.getBook()).isEqualToIgnoringGivenFields(updatedComment.getBook(), "comments"); // здесь ошибка Cannot locate field $$_hibernate_interceptor on class model.Book
  13.     }
  14.  
  15. // ------------------- КНИЖКА -------------------
  16. @Entity
  17. @Table(name = "book")
  18. @Data
  19. @NoArgsConstructor
  20. public class Book {
  21.     @Id
  22.     @GeneratedValue(strategy = IDENTITY)
  23.     private Long bookId;
  24.  
  25.     private String title;
  26.     private Integer year;
  27.  
  28.     @ManyToOne
  29.     @JoinColumn(name = "author_id", nullable = false)
  30.     private Author author;
  31.     @ManyToOne
  32.     @JoinColumn(name = "genre_id", nullable = false)
  33.     private Genre genre;
  34.  
  35.     @OneToMany(
  36.             mappedBy = "book",
  37.             cascade = CascadeType.ALL,
  38.             orphanRemoval = true
  39.     )
  40.     private List<Comment> comments = new ArrayList<>();
  41.     // конструктор...
  42. }
  43.  
  44. // ------------------- КОММЕНТ -------------------
  45. @Entity
  46. @Table(name = "comment")
  47. @Data
  48. @NoArgsConstructor
  49. public class Comment {
  50.     @Id
  51.     @GeneratedValue(strategy = IDENTITY)
  52.     private Long commentId;
  53.  
  54.     private String commentator;
  55.     private String text;
  56.     private LocalDateTime date;
  57.  
  58.     @ManyToOne(fetch = FetchType.LAZY)
  59.     @JoinColumn(name = "book_id", nullable = false)
  60.     private Book book;
  61.  
  62.     // конструктор...
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement